View previous topic :: View next topic |
Author |
Message |
nico654 Smarty Rookie
Joined: 22 Apr 2004 Posts: 5
|
Posted: Tue May 11, 2004 5:03 pm Post subject: Multiple template directories: how to |
|
|
Here it is a way so let Smarty accept more than one template directory.
This should be useful if you have multiple apps that have some templates in common.
Please consider you are changing Smarty core, so make a backup of any file you will change before proceed.
Also consider you are doing this at your own risk.
To let Smarty accept an array of template directories you need to edit just two files: libs/Smarty.class.php and libs/core/core.is_secure.php
Smarty.class.php
at line 84 replace:
[php:1:ee520b69c3]
var $template_dir = 'templates';
[/php:1:ee520b69c3]
with:
[php:1:ee520b69c3]
var $template_dir = array('templates');
[/php:1:ee520b69c3]
core.is_secure.php
from lines 29 to 31 replace:
[php:1:ee520b69c3]
if (!in_array($smarty->template_dir, $smarty->secure_dir))
// add template_dir to secure_dir array
array_unshift($smarty->secure_dir, $smarty->template_dir);
[/php:1:ee520b69c3]
with:
[php:1:ee520b69c3]
foreach ((array)$smarty->template_dir as $curr_dir){
if (!in_array($curr_dir, $smarty->secure_dir))
// add template_dir to secure_dir array
array_unshift($smarty->secure_dir, $curr_dir);
}
[/php:1:ee520b69c3]
Then you can apply to $template_dir the same rules you are appling to $plugins_dir.
That's all
I hope this feature will be available as default on the next Smarty release |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed May 12, 2004 2:59 am Post subject: |
|
|
Hmm. Nice, but did you try using an array of template dirs with the base Smarty? Arrays of template dirs already work out of the box -- but AFAIK, this "feature" is not officially supported (hence why it is not documented, I suppose). I mention, though, that I haven't tested the behaviour in terms of secure_dir, so your patch could certainly be of merit. |
|
Back to top |
|
bobdavis Smarty n00b
Joined: 25 Jun 2004 Posts: 1 Location: San Antonio, TX
|
Posted: Fri Jun 25, 2004 3:10 pm Post subject: |
|
|
If I have a class that extends Smarty, I should be able to set $template_dir = array() in that class, right?
Then, following on what boots is saying, I should be able to just use multiple directories without making changes to the smarty core.
Or does the base Smarty class override what I do in a class that extends Smarty? |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Fri Jun 25, 2004 7:47 pm Post subject: |
|
|
One known problem with arrays of template directories is that cache ids could collide. I suppose this could be fixed by including the template directory path (that the root template was found?) in the cache id assembling process.
I suppose if foo.tpl was always found in the same directory then it wouldn't be an issue. If you decide to dynamically determine what template directories are included, that is where trouble starts. |
|
Back to top |
|
muffl0n Smarty Rookie
Joined: 28 Jun 2004 Posts: 15 Location: Hamburg, Germany
|
Posted: Mon Jun 28, 2004 9:31 am Post subject: |
|
|
I ran into the described problem:
I abstracted a function that would look if the specified template is in the custom-directory. If not it changed the template_dir to a default directory (fallback). It now also changes the compile_dir cause the compiled templates collided.
Is it planned to include an addition to the compile_dir-name so that this problem is avoided? |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Mon Jun 28, 2004 9:58 am Post subject: |
|
|
muffl0n: fwiw, I personally wouldn't favour that, though I can see its uses and benefits. The thing is, is that I see this as an "advanced" use and somewhat of a corner case. For some people it will be very important, for most, not at all.
IMHO it is better that Smarty stay as tight as possible for the masses but be flexible enough to allow tinkering for those who need it. In this instance, I think Smarty already hits that balance fairly well.
Greetings. |
|
Back to top |
|
muffl0n Smarty Rookie
Joined: 28 Jun 2004 Posts: 15 Location: Hamburg, Germany
|
Posted: Thu Jul 08, 2004 6:03 pm Post subject: |
|
|
But would the inclusion of the directory name to the unique name generated for the compiled template by smarty be such a big thing? Isn't it just a (small) addition to the currently used (hashing?) algorithm?
Beginner users wouldn't even notice this feature. |
|
Back to top |
|
LePoulpe Smarty n00b
Joined: 01 Aug 2004 Posts: 1
|
Posted: Sun Aug 01, 2004 5:11 pm Post subject: |
|
|
Hi,
First of all, thanks to the people who made smarty what it is ...
I'm just upping this thread, as I got the same problem.
I want to implement some kind of cms/forum and I want to layer the code as much as possible : one dir for out-of-the-box smarty (upgrade with delete & unzip), one dir for my code out-of the box, and last one for the user's cms/forum. This would allow smooth migration of the two base component.
Templates will be stored mainly on my code, but may be overriden by user code (either for stylesheet, specific pages, etc.)
So ... Any chance that we get a "native" multi-template lib ?
Thanks |
|
Back to top |
|
|