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

Multiple template directories: how to

 
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 -> Tips and Tricks
View previous topic :: View next topic  
Author Message
nico654
Smarty Rookie


Joined: 22 Apr 2004
Posts: 5

PostPosted: Tue May 11, 2004 5:03 pm    Post subject: Multiple template directories: how to Reply with quote

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 Smile
I hope this feature will be available as default on the next Smarty release Wink
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed May 12, 2004 2:59 am    Post subject: Reply with quote

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
View user's profile Send private message
bobdavis
Smarty n00b


Joined: 25 Jun 2004
Posts: 1
Location: San Antonio, TX

PostPosted: Fri Jun 25, 2004 3:10 pm    Post subject: Reply with quote

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
View user's profile Send private message AIM Address
mohrt
Administrator


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

PostPosted: Fri Jun 25, 2004 7:47 pm    Post subject: Reply with quote

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


Joined: 28 Jun 2004
Posts: 15
Location: Hamburg, Germany

PostPosted: Mon Jun 28, 2004 9:31 am    Post subject: Reply with quote

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
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Mon Jun 28, 2004 9:58 am    Post subject: Reply with quote

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
View user's profile Send private message
muffl0n
Smarty Rookie


Joined: 28 Jun 2004
Posts: 15
Location: Hamburg, Germany

PostPosted: Thu Jul 08, 2004 6:03 pm    Post subject: Reply with quote

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
View user's profile Send private message
LePoulpe
Smarty n00b


Joined: 01 Aug 2004
Posts: 1

PostPosted: Sun Aug 01, 2004 5:11 pm    Post subject: Reply with quote

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
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 -> Tips and Tricks 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