View previous topic :: View next topic |
Author |
Message |
ctlajoie Smarty Rookie
Joined: 20 Nov 2009 Posts: 29
|
Posted: Fri Nov 20, 2009 7:04 pm Post subject: What to use instead of default_template_handler_func? |
|
|
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 |
|
ctlajoie Smarty Rookie
Joined: 20 Nov 2009 Posts: 29
|
Posted: Fri Nov 20, 2009 7:05 pm Post subject: |
|
|
useless post to get my postcount up so I can post links. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Fri Nov 20, 2009 7:44 pm Post subject: |
|
|
What are you trying to do specifically? |
|
Back to top |
|
ctlajoie Smarty Rookie
Joined: 20 Nov 2009 Posts: 29
|
Posted: Fri Nov 20, 2009 8:03 pm Post subject: |
|
|
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
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Fri Nov 20, 2009 8:09 pm Post subject: |
|
|
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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Fri Nov 20, 2009 8:10 pm Post subject: |
|
|
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 |
|
meze Smarty n00b
Joined: 20 Nov 2009 Posts: 4
|
Posted: Mon Nov 23, 2009 3:41 am Post subject: |
|
|
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" |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Mon Nov 23, 2009 4:24 pm Post subject: |
|
|
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 |
|
meze Smarty n00b
Joined: 20 Nov 2009 Posts: 4
|
Posted: Mon Nov 23, 2009 10:16 pm Post subject: |
|
|
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 |
|
|