|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
Code62 Smarty Rookie
Joined: 21 Jun 2011 Posts: 12 Location: Work@Liège.be
|
Posted: Tue Jun 21, 2011 11:52 am Post subject: [Solved][Smarty3] _smarty_include() is not used anymore |
|
|
Hello,
I'm working on a pretty big software that i just migrate from Smarty 2 to Smarty 3.
Previously, I extended the Smarty 2 class and overrode the _smarty_include() method to make it search in a specific directory, depending on the context.
Code: | public function _smarty_include($params) {
// do some stuff
// $include_tpl_dir = ...
$this->template_dir = $include_tpl_dir;
parent::_smarty_include($params);
} |
Now with that same class extending Smarty 3, the _smarty_include() is not used anymore, so the "template_dir" is not changed to the right value, and Smarty searchs for my included tpl in the "wrong directory"
Question:
how can I modify the directory where Smarty will look without modifying the code of Smarty, and without having to modify all my template files ?
Many thanks in advance for any help.
(nota: an other guy with the same problem here: http://www.smarty.net/forums/viewtopic.php?t=18545 , no answer after 6 months )
EDIT: add "solved" tag
Last edited by Code62 on Tue Jun 21, 2011 1:15 pm; edited 1 time in total |
|
Back to top |
|
rodneyrehm Administrator
Joined: 30 Mar 2007 Posts: 674 Location: Germany, border to Switzerland
|
Posted: Tue Jun 21, 2011 12:57 pm Post subject: |
|
|
In Smarty3 $template_dir is an array - a list of directories to search in. Smarty loops this array and includes the first template found: Code: | $smarty->template_dir = array(
'/tmp/',
'/var/templates/',
); |
For $smarty->fetch('foo.tpl'); Smarty will look for /tmp/foo.tpl, then /var/templates/foo.tpl.
If no template is found, the $default_template_handler_func is executed. That callback allows your application to jump in if smarty could not find a file. You could either return a template source, or point smarty to the correct file.
default_template_handler example returning a filepath: Code: |
$smarty->default_template_handler_func = function($resource_type, $resource_name, &$template_source, &$template_timestamp, Smarty $smarty)
{
return '/tmp/helloworld.tpl';
}; |
default_template_handler example returning template source: Code: |
$smarty->default_template_handler_func = function($resource_type, $resource_name, &$template_source, &$template_timestamp, Smarty $smarty)
{
$template_source = "Hello World";
$template_timestamp = time();
return true;
}; |
default_template_handler example returning failing as well: Code: |
$smarty->default_template_handler_func = function($resource_type, $resource_name, &$template_source, &$template_timestamp, Smarty $smarty)
{
// couldn't find anything
return false;
}; |
With Smarty 3.1 you'll get more power over $template_dir, as you can tell smarty exactly which array element (directory) to load a template from. You can specify a path relative to the current template itself, and so on… lots of fun stuff…
If neither of these hints help you, consider explaining your problem in greater detail. maybe add some code samples from Smarty2. |
|
Back to top |
|
Code62 Smarty Rookie
Joined: 21 Jun 2011 Posts: 12 Location: Work@Liège.be
|
Posted: Tue Jun 21, 2011 1:14 pm Post subject: |
|
|
Thanks a lot for these explainations !
I directly relied on the "default_template_handler_func", and with just some minors adaptations i was able to use the same code that i used in my "_smarty_include()", and now everything's work just as fine as before
(just a minor error regarding to what you said: "Argument 5 passed to OAMS\{closure}() must be an instance of Smarty, instance of Smarty_Internal_Template given" )
Thanks again, and have a nice day |
|
Back to top |
|
rodneyrehm Administrator
Joined: 30 Mar 2007 Posts: 674 Location: Germany, border to Switzerland
|
Posted: Tue Jun 21, 2011 1:16 pm Post subject: |
|
|
yeah sorry for that. accidentally copied examples from 3.1 instead of the 3.0 branch. We changed the API there I guess… |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|