Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

[Solved][Smarty3] _smarty_include() is not used anymore

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
Code62
Smarty Rookie


Joined: 21 Jun 2011
Posts: 12
Location: Work@Liège.be

PostPosted: Tue Jun 21, 2011 11:52 am    Post subject: [Solved][Smarty3] _smarty_include() is not used anymore Reply with quote

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" Sad

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


EDIT: add "solved" tag


Last edited by Code62 on Tue Jun 21, 2011 1:15 pm; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
rodneyrehm
Administrator


Joined: 30 Mar 2007
Posts: 674
Location: Germany, border to Switzerland

PostPosted: Tue Jun 21, 2011 12:57 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Code62
Smarty Rookie


Joined: 21 Jun 2011
Posts: 12
Location: Work@Liège.be

PostPosted: Tue Jun 21, 2011 1:14 pm    Post subject: Reply with quote

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 Very Happy


(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 Smile
Back to top
View user's profile Send private message Visit poster's website
rodneyrehm
Administrator


Joined: 30 Mar 2007
Posts: 674
Location: Germany, border to Switzerland

PostPosted: Tue Jun 21, 2011 1:16 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP