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

What to use instead of default_template_handler_func?

 
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 -> Smarty 3
View previous topic :: View next topic  
Author Message
ctlajoie
Smarty Rookie


Joined: 20 Nov 2009
Posts: 29

PostPosted: Fri Nov 20, 2009 7:04 pm    Post subject: What to use instead of default_template_handler_func? Reply with quote

I am trying to duplicate the functionality I once described to someone on StackOverflow.

In short, it allowed me to "override" a template. Smarty 3 looks like it has been designed specifically with this type of use in mind, and I am anxious to try it. However I don't know how to do this in Smarty 3. Can someone point me in the right direction.

I'm not even sure if I should take this approach any more. I am fairly early on in the development of my current project, so I would be willing to modify the way my templates work to gain the new features in Smarty 3.

Any suggestions appreciated. Thanks.


Last edited by ctlajoie on Fri Nov 20, 2009 7:07 pm; edited 1 time in total
Back to top
View user's profile Send private message
ctlajoie
Smarty Rookie


Joined: 20 Nov 2009
Posts: 29

PostPosted: Fri Nov 20, 2009 7:05 pm    Post subject: Reply with quote

useless post to get my postcount up so I can post links.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Fri Nov 20, 2009 7:44 pm    Post subject: Reply with quote

What are you trying to do specifically?
Back to top
View user's profile Send private message Visit poster's website
ctlajoie
Smarty Rookie


Joined: 20 Nov 2009
Posts: 29

PostPosted: Fri Nov 20, 2009 8:03 pm    Post subject: Reply with quote

I have a directory that has most of my templates in it. I'll call it /base. I have another directory that has a few templates in it, which I'll call /ext. The templates in /ext that have the same name as ones in /base override the ones in /base.

As an example, suppose I had the following directory structure

  • base

    • test1.tpl
    • test2.tpl

  • ext

    • test1.tpl



In my code, if I had
Code:
$smarty->display('test1.tpl');
it would use the test1.tpl from /ext.

If I did
Code:
$smarty->display('test2.tpl');
it would use the one from base.

I have 2 questions:
1. Can this still be done in Smarty 3?
2. Is there a better way this could be done, considering the new features in Smarty 3?
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Fri Nov 20, 2009 8:09 pm    Post subject: Reply with quote

There are a couple of answers. Firstly, you could use an array for template_dir, and Smarty (2 or 3) would traverse the arrays looking for the template you are looking for.

Another possible solution is template inheritance feature of Smarty 3. See the section on template inheritance in the README.

http://smarty-php.googlecode.com/svn/branches/Smarty3Dev/distribution/README
Back to top
View user's profile Send private message Visit poster's website
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Fri Nov 20, 2009 8:10 pm    Post subject: Reply with quote

You can simply specify an array of template folder as property template_dirs.
Smarty is searching the template in the order of the folders in the array.
So specify /ext first and /base second.

This was also an undocumented feature in Smarty2.
Back to top
View user's profile Send private message
meze
Smarty n00b


Joined: 20 Nov 2009
Posts: 4

PostPosted: Mon Nov 23, 2009 3:41 am    Post subject: Reply with quote

i needed the same functionality (in my application, getting the path of a template is complex, not just traversing the arrays) and i've patched smarty, in smarty_internal_template.php, function buildTemplateFilepath i found this code:
Code:

                $_return = call_user_func_array($this->smarty->default_template_handler_func,
                    array($this->resource_type, $this->resource_name, &$this->template_source, &$this->template_timestamp, &$this));
                if ($_return == true) {
                    return $_filepath;
                }


I don't understand why it returns $_filepath in that case. Why can't we use the return value of default_template_handler_func as a new filepath? I replaced it with:

Code:
                $_return = call_user_func_array($this->smarty->default_template_handler_func,
                    array($this->resource_type, $this->resource_name, &$this->template_source, &$this->template_timestamp, &$this));
                if ($_return === true) {
                    return $_filepath;
                } elseif (is_string($_return)) {
                    return $_return;
                }



btw, the last "&$this" should be "$this" Wink
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Mon Nov 23, 2009 4:24 pm    Post subject: Reply with quote

The default_template_handler_func may now return a filepath to the template which shall be displayed. This change is in beta5 now.
Back to top
View user's profile Send private message
meze
Smarty n00b


Joined: 20 Nov 2009
Posts: 4

PostPosted: Mon Nov 23, 2009 10:16 pm    Post subject: Reply with quote

it still returns $file instead of a filepath because any non-empty string is always == true.

here's a patch:
Code:

Index: smarty_internal_template.php
===================================================================
--- smarty_internal_template.php   (revision 3359)
+++ smarty_internal_template.php   (working copy)
@@ -573,10 +573,10 @@
             } else {
                 $_return = call_user_func_array($this->smarty->default_template_handler_func,
                     array($this->resource_type, $this->resource_name, &$this->template_source, &$this->template_timestamp, $this));
-                if ($_return == true) {
+                if (is_string($_return)) {
+                    return $_return;
+                } elseif ($_return == true) {
                     return $file;
-                } elseif (is_string($_return)) {
-                    return $_return;
                 }
             }
         }



Check DefaultTemplateHandlerTests.php please, i wanted to add a test case for that code, but, for some reason, those tests aren't runnable (i tired to make them runnable and got two errors)
Back to top
View user's profile Send private message
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 -> Smarty 3 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