Adding variables to the page template
I wanted to add a single variable to the page object and make it available to the templates. However since the Template object is declared as protected I cannot add the variable in the GeneratePage hook.
The hook is implemented in PageRegular.php line 82: $GLOBALS['TL_HOOKS']['generatePage']
I tried:
Code:
class Subtitle
{
public function onGeneratePage(Database_Result $objPage, Database_Result $objLayout, PageRegular $objPageRegular)
{
$objPageRegular->Template->subTitle = $objPage->subTitle;
}
}
but that generates the errormessage:
Fatal error: Cannot access protected property PageRegular::$Template in C:\xampp\htdocs\domainname.com\system\modules\subt itle\Subtitle.php on line 7 (that is inside the function).
Is there another method to get what I want?
In the meantime I edited the core to just include that one line of the function; problem diverted. I dislike the protected and private statements...
Re: Adding variables to the page template
I've run into this before. Try:
Code:
<?php global $objPage; echo $objPage->subtitle; ?>
in your fe_ template. It's quick and dirty...
Re: Adding variables to the page template
Great! Well, dirty but functional.
Have you placed a ticket for this (feature request)? I'd like to know if this might be changed. Otherwise I'll request it and see how far it gets....
Re: Adding variables to the page template
Forgive my n00biness...I'm learning... :)
But what if your Subtitle class extended Template? Could you access protected properties and methods then?
Re: Adding variables to the page template
Derp. Yeah, good call, Medianomaly. I would actually just make it extend Frontend and it should then work fine. :D
Re: Adding variables to the page template
Ah -- cool. Thanks for the info.
This seems like it would be a REALLY valuable piece of info. Perhaps something for the factory? :)
Re: Adding variables to the page template
Yep, this indeed works as suggested!
One little thing though; I've never used the protected visibility myself because I'm not sure what the advantage is. Can someone show me an example when this is beneficial? (The advantage of a private visibility I can see from the point of view of whoever builds the class)