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
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
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: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<?php public function sessions_end() { if (REQ == 'PAGE') { // your code here } } ?>
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.
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
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
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
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.
$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 😛
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.