Jump to content

Is there a PW native way to add a custom page class for all templates?


JayGee
 Share

Recommended Posts

Have been loving working with the custom page classes feature since it was added, but haven't yet figured out if there's meant to be a built in way to add a class in /site/classes that will add methods for every template rather than for a single template type.

I thought initially you could add a function to

/site/classes/DefaultPage.php

and call it as

$page->yourCustomMethod()

within any template but this doesn't seem to work - is it meant to?

Link to comment
Share on other sites

11 hours ago, JayGee said:

Have been loving working with the custom page classes feature since it was added, but haven't yet figured out if there's meant to be a built in way to add a class in /site/classes that will add methods for every template rather than for a single template type.

<cut>

within any template but this doesn't seem to work - is it meant to?

If I'm not mistaken, this is the same thing I'm trying to do right now but it's not working. I'm currently trying to figure out the best way to load the base class and use it with others in classes. 

JayGee, do you mean something like:

/site/classes/CommonTemplate.php

<?php namespace ProcessWire;

class CommonTemplate extends Page {
    function __construct() {
        be("CommonTemplate initialized");
    }

	function somethingAllPagesCanUse() {
		be("common template methods");
	}
}

Then for specific pages:

/site/classes/ContactPage.php

<?php namespace ProcessWire;
    
class ContactPage extends CommonTemplate {
	function __construct() {
		be("Not working");
	}
}

Just typed that on the fly, sorry if there's errors. 🙂

 

  • Like 1
Link to comment
Share on other sites

Following is how I've got it working. I haven't done major testing yet, but it appears to be working.

 

/site/init.php (at end of file)

require_once('./classes/CommonTemplate.php');

 

/site/classes/CommonTemplate.php 

<?php namespace ProcessWire;

class CommonTemplate extends Page {
    function __construct($templates = array(), $parents = array()) {
        parent::__construct($templates, $parents);
    }

    function testClass() {
        be("This is working!");
    }
}

 

/site/classes/[TemplateName]Page.php (eg: AboutUsPage.php)

<?php namespace ProcessWire;
    
class AboutUsPage extends CommonTemplate {
	function test() {
		$this->testClass();			// Works!
	}
}

 

This way I'm able to access methods from "CommonTemplate" in custom page classes as $this->methodName() and in actual template files it works as wire()->methodName(). So, for clarity:

/site/templates/about-us.php

<?php namespace ProcessWire; ?>
<h1>Cool Macho HTML template stuff</h1>

<?php
	page()->testClass();
	// or
	$page->testClass();
	// or
	$this->page()->testClass();
	// or
	$this->page->testClass();
?>

 

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

Hi all, sorry for the delay I was away for a few days and thanks for the responses so far.

So the contents of the class file (DefaultPage.php) are as follows:

class DefaultPage extends Page {

    public function test() {
        return 'test';
    }

}

My understanding (maybe wrongly) is that in this scenario $page->test() should then be callable in any template? However I get a method is not callable in this context error.

 

On 8/21/2023 at 12:15 AM, wbmnfktr said:

Is it enabled in your config.php?
Just asking.

$config->usePageClasses = true;

 

Yes this is enabled. Testing this in a fresh download of latest production PW where is enabled by default. (Other template specific classes and methods work too - e.g. /classes/HomePage.php).

 

Other background info is that I'm using markup regions - I don't know if that could make any difference?

Thanks,
J

  • Like 1
Link to comment
Share on other sites

11 hours ago, iank said:

Just a thought - do you have your template specific classes extending 'DefaultPage' rather than 'Page'  ?

The scenario you're describing has always worked just fine for me. 

Ahhhh thank you - penny drops, aha moment! 🤣

This works - the template-specific classes need to extend DefaultPage and not Page... painfully obvious with hindsight that it should follow usual inheritance rules. I'm not 100% sure it's clear from the docs though, I think I was expecting some kind of PW magic to kick in and inject the DefaultPage methods.

Either way I know now and this awesome PW feature becomes even more useful. Thanks all. 🥳

 

Edit: I now also realise this is exactly what @Charming Troll's reply was demonstrating. - thanks.

Edited by JayGee
  • Like 3
Link to comment
Share on other sites

On 8/22/2023 at 10:56 PM, JayGee said:

I'm not 100% sure it's clear from the docs though

It is definitely not, I agree. I think even the introductory blog page does not make it clear what the purpose of DefaultPage actually is: https://processwire.com/blog/posts/pw-3.0.152/

And a lot more could be documented and supported, see my old thread, for example: 

 

  • Like 1
Link to comment
Share on other sites

1 hour ago, szabesz said:

It is definitely not, I agree. I think even the introductory blog page does not make it clear what the purpose of DefaultPage actually is: https://processwire.com/blog/posts/pw-3.0.152/

And a lot more could be documented and supported, see my old thread, for example: 

 

I completely missed your thread on this when searching this issue - some helpful stuff there thanks. 

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...