Results 1 to 7 of 7

Thread: Adding variables to the page template

  1. #1
    Experienced user
    Join Date
    01-12-10.
    Posts
    814

    Default 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...

  2. #2
    User winanscreative's Avatar
    Join Date
    06-21-09.
    Location
    Massachusetts, United States
    Posts
    261

    Default 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...

  3. #3
    Experienced user
    Join Date
    01-12-10.
    Posts
    814

    Default 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....

  4. #4
    Experienced user
    Join Date
    08-21-09.
    Posts
    563

    Default 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?
    Brian

  5. #5
    User winanscreative's Avatar
    Join Date
    06-21-09.
    Location
    Massachusetts, United States
    Posts
    261

    Default 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

  6. #6
    Experienced user
    Join Date
    08-21-09.
    Posts
    563

    Default 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?
    Brian

  7. #7
    Experienced user
    Join Date
    01-12-10.
    Posts
    814

    Default 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)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •