Results 1 to 5 of 5

Thread: What Do You Think About a new Template Hook?

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

    Default What Do You Think About a new Template Hook?

    Hi everyone,

    I'm working on this side project, and it's allowed me to REALLY dig in and reacquaint myself with all the various modules, content elements, etc -- some of which I haven't really used up to this point.

    One thing I would love to be able to do is modify certain template variables BEFORE the template is parsed.

    I know I can do this in the template as well, but it would be nice to have one centralized place where I can make some minor modifications (and keep the actual templates clean and minimal). It would also be handy if a developer wanted to add a variable to an existing template.

    As far as I know this can't currently be done with existing hooks. Or am I wrong about that? Part of me thinks I'm thinking about this the wrong way.

    Here's a really simplified example to help explain myself. Let's say there was a new hook that gets called RIGHT BEFORE the template gets parsed. It passed $arrData and $strTemplate and expected $arrData as a return value.

    Then I could do something like..
    Code:
    public function myNewHookMethod($arrData, $strTemplate)
    {
    	$arrData['class'] = str_replace('mod_', 'module mod-', $this['class']);
    	return $arrData;
    }
    Does this seem like a good idea to anyone else? Is there anything I'm missing? If I requested this as a feature, would I make a fool of myself?

    Thanks!
    Brian

  2. #2
    Experienced user
    Join Date
    06-10-09.
    Location
    Cape Town, South Africa
    Posts
    1,387

    Default Re: What Do You Think About a new Template Hook?

    There are already some hooks like that in News, Events, etc. They use the Hook called parseArticles. You can then determine the module you're in, and then do your magic. You also gain access to most raw data by the ->setData() which is called by most Contao modules to set the database data directly to the template.

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

    Default Re: What Do You Think About a new Template Hook?

    Thanks, Thyon.

    I missed the 'parseArticles' hook. Although it looks like it's only available for news items -- I wasn't able to find equivalents for events or the other modules.

    As for setData() -- how could I make use of that in my case? I'm guessing I would somehow need to start with a template object? Then I'd need to grab it's data, make my changes, then run that data back through "setData()"? But how do I get the template object to start with?
    Brian

  4. #4
    Experienced user
    Join Date
    06-10-09.
    Location
    Cape Town, South Africa
    Posts
    1,387

    Default Re: What Do You Think About a new Template Hook?

    I usually write my own modules, by copying and existing one and tinkering with it. Then you're free to do what you like. It's much easier than trying to figure out how to hook into the code, when you can just code yourself.

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

    Default Re: What Do You Think About a new Template Hook?

    SO...

    Today I found out this hook has been implemented and appears it will drop in 2.10. I gave it a test drive.

    You'll be able to call a hook -- 'parseTemplate' -- that passes through the template object. Then you can access its $arrData and do what you wish:

    Code:
    public function parseMyTemplate($objTemplate)
    {
    $objTemplate->arrData['class'] = str_replace('mod_', 'module mod_', $objTemplate->arrData['class']);
    return $objTemplate;
    // Hooray! Now when I do my Greyboxes, I can style all of my modules the same,
    // Without writing 50 selectors or digging into 50 templates. :)
    // And if I want to change it later, it's just one line of code.
    }
    I'm super stoked about this. So may possibilities...
    Brian

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
  •