We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

pre-render hook during template processing

Development and Programming

matt romaine's avatar
matt romaine
50 posts
14 years ago
matt romaine's avatar matt romaine

Hi, where is the best place to add PHP code I want run before every template? I have a lot of templates already and rather than add an include before all of them, I want a snippet of code to run before the template is processed. I will need to know the name of the template as the pre-processing uses that in a variable.

Is there pre-template hook system?

Thanks, Matt

       
Focus Lab Dev Team's avatar
Focus Lab Dev Team
1,129 posts
14 years ago
Focus Lab Dev Team's avatar Focus Lab Dev Team

Hey Matt,

It depends on which hooks you’re looking at exploring.

You can use an ExpressionEngine hook which would require writing an add-on with an ext.___.php file and use the sessions_end hook. That is executed on every single page load. If you use that you should probably also use a conditional to make sure it only runs on your front-end template requests. It’d start something like this:

<?php

public function sessions_end()
{
    if (REQ == 'PAGE')
    {
        // your code here
    }
}

?>

An alternative would be to use CodeIgniter’s hooks. You could probably hook into the post_controller hook and point it to whatever file, class & method you want.

It just depends on your goals and needs.

Does that help?

-Erik

       
matt romaine's avatar
matt romaine
50 posts
14 years ago
matt romaine's avatar matt romaine

Thanks Erik; given EE2’s migration to relying on CI, would you recommend the latter - a CI hook? Main objective is to run some PHP code before every template is loaded; I will need some specific information about the template to be loaded though (in particular, filename).

Cheers, Matt

Hey Matt, It depends on which hooks you’re looking at exploring. You can use an ExpressionEngine hook which would require writing an add-on with an ext.___.php file and use the sessions_end hook. That is executed on every single page load. If you use that you should probably also use a conditional to make sure it only runs on your front-end template requests. It’d start something like this:
<?php

public function sessions_end()
{
    if (REQ == 'PAGE')
    {
        // your code here
    }
}

?>
An alternative would be to use CodeIgniter’s hooks. You could probably hook into the post_controller hook and point it to whatever file, class & method you want. It just depends on your goals and needs. Does that help? -Erik
       
Focus Lab Dev Team's avatar
Focus Lab Dev Team
1,129 posts
14 years ago
Focus Lab Dev Team's avatar Focus Lab Dev Team
I will need some specific information about the template to be loaded though (in particular, filename).

The filename of the template rendered by ExpressionEngine? EE templates aren’t always flat files and even when they are EE could possibly load multiple templates on any given page load.

       
matt romaine's avatar
matt romaine
50 posts
14 years ago
matt romaine's avatar matt romaine

Let’s say I wanted to put the following PHP snippet at the beginning of every template:

<pre><code><?php if (‘{language_code}’ == ‘ja’) { putenv(‘LC_ALL=’ . “ja_JP.utf8”); setlocale(LC_ALL, “ja_JP.utf8”); } else { putenv(‘LC_ALL=’ . “en_US.utf8”); setlocale(LC_ALL, “en_US.utf8”); } bindtextdomain(“{template-group}”, “./system/expressionengine/i18n/locales/”); bind_textdomain_codeset(“{template-group}”, “UTF-8”); textdomain(“{template_group}”); ?></code></pre>

Can anyone recommend a clean way to do so that doesn’t include copy / pasting the code in every template?

Cheers, Matt

       
Focus Lab Dev Team's avatar
Focus Lab Dev Team
1,129 posts
14 years ago
Focus Lab Dev Team's avatar Focus Lab Dev Team

Are you trying to create a “{template_group}” tag you can use in your templates?

       
matt romaine's avatar
matt romaine
50 posts
14 years ago
matt romaine's avatar matt romaine

Hi Erik,

That ‘{template_group}’ was more just an example of the dynamic information I would need in the code snippet.

At any rate, I’ve decided to just PHP include the chunk as embedding it ran through the PHP code before the parent including page needed it to.

Cheers, Matt

       
Focus Lab Dev Team's avatar
Focus Lab Dev Team
1,129 posts
14 years ago
Focus Lab Dev Team's avatar Focus Lab Dev Team

Glad to hear you got something in place.

Considering your goals, I’d suggest using the extension approach I mentioned above to avoid the PHP parsing on every template. With the sessions_end hook you can add global variables to the global variable array before the templates get rendered. If you end up going that route, feel free to post other questions 😊

Erik

       
lexusgs430's avatar
lexusgs430
49 posts
14 years ago
lexusgs430's avatar lexusgs430

A better way is to use a CI application hook. Here is an example of how.

First, create a file named

system/third_party/preapp/Preapp.php Paste your code in said file, and wrap it in a function.

<?php

function myfunction() {

    if ('{language_code}' == 'ja')
    {
        putenv('LC_ALL=' . "ja_JP.utf8");
        setlocale(LC_ALL, "ja_JP.utf8");
    }
    else
    {
        putenv('LC_ALL=' . "en_US.utf8");
        setlocale(LC_ALL, "en_US.utf8");
    }
    bindtextdomain("{template-group}", "./system/expressionengine/i18n/locales/");
    bind_textdomain_codeset("{template-group}", "UTF-8");
    textdomain("{template_group}");
}
?></code></pre>

Now open up below file.

system/expressionengine/config/hooks.php

Add in the following

<pre><code>$hook['display_override'] = array(
  'class' => '',
  'function' => 'myfunction',
  'filename' => 'Preapp.php',
  'filepath' => 'third_party/preapp'
);

Finally, you have to enable hooks. Open up

system/expressionengine/config/hooks.php

and edit line #119 to

$config['enable_hooks'] = TRUE;

And you should be g2g.

       
matt romaine's avatar
matt romaine
50 posts
14 years ago
matt romaine's avatar matt romaine

Thanks; but when doing it this way, how do I get the template group name while inside myfunction() ?

       
lexusgs430's avatar
lexusgs430
49 posts
14 years ago
lexusgs430's avatar lexusgs430

$templategroup = $this->EE->uri->segment(2); (I believe it’s 2)

       
matt romaine's avatar
matt romaine
50 posts
14 years ago
matt romaine's avatar matt romaine
$templategroup = $this->EE->uri->segment(2); (I believe it’s 2)

Thanks lexusgs430. If you’re customizing at the level we are though, you can bet your house we can’t rely on the URI 😊 We’re embedding and sub-embedding across groups. We’d need the file path.

At any rate we’ve gone with the PHP include approach, which gives us template-level flexibility so we’re pretty happy with it for now.

http://mygengo.com - to see what we’re doing. Can’t believe EE2 can handle it, but seems to so far 😛

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.