JayGee Posted August 19, 2023 Share Posted August 19, 2023 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 More sharing options...
szabesz Posted August 19, 2023 Share Posted August 19, 2023 That's strange as it does work for me. Did you double check everything? 1 Link to comment Share on other sites More sharing options...
Jan Romero Posted August 19, 2023 Share Posted August 19, 2023 Yes, that is the way. What are the contents of your DefaultPage.php file? https://processwire.com/blog/posts/pw-3.0.152/#new-ability-to-specify-custom-page-classes If you just want to add a method or property you can also use hooks for that: https://processwire.com/docs/modules/hooks/#how-can-i-add-a-new-method-via-a-hook 1 Link to comment Share on other sites More sharing options...
Charming Troll Posted August 19, 2023 Share Posted August 19, 2023 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. ? 1 Link to comment Share on other sites More sharing options...
Charming Troll Posted August 20, 2023 Share Posted August 20, 2023 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(); ?> 1 1 Link to comment Share on other sites More sharing options...
wbmnfktr Posted August 20, 2023 Share Posted August 20, 2023 Is it enabled in your config.php? Just asking. $config->usePageClasses = true; 1 Link to comment Share on other sites More sharing options...
JayGee Posted August 22, 2023 Author Share Posted August 22, 2023 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 1 Link to comment Share on other sites More sharing options...
iank Posted August 22, 2023 Share Posted August 22, 2023 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. 1 1 Link to comment Share on other sites More sharing options...
JayGee Posted August 22, 2023 Author Share Posted August 22, 2023 (edited) 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 August 22, 2023 by JayGee 3 Link to comment Share on other sites More sharing options...
szabesz Posted August 29, 2023 Share Posted August 29, 2023 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: 1 Link to comment Share on other sites More sharing options...
JayGee Posted August 29, 2023 Author Share Posted August 29, 2023 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. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now