View previous topic :: View next topic |
Author |
Message |
Roc Guest
|
Posted: Thu Apr 17, 2003 7:21 pm Post subject: Array for $template_dir |
|
|
As the subject says, it should be possible to setup an array of paths that are scanned for template files, using the order of path definitions (this is important to implement a kind of "priority model"). |
|
Back to top |
|
Tom Sommer Administrator
Joined: 16 Apr 2003 Posts: 47 Location: Denmark
|
Posted: Thu Apr 17, 2003 7:23 pm Post subject: |
|
|
How would this be useful? |
|
Back to top |
|
Guest
|
Posted: Thu Apr 17, 2003 7:34 pm Post subject: Re: Array for $template_dir |
|
|
Roc wrote: | As the subject says, it should be possible to setup an array of paths that are scanned for template files, using the order of path definitions (this is important to implement a kind of "priority model"). |
you can try to write a resource-handler that does just this:
see http://smarty.php.net/manual/en/template.resources.php |
|
Back to top |
|
Roc Guest
|
Posted: Thu Apr 17, 2003 7:39 pm Post subject: |
|
|
Let's assume a themeable application. First I use a set of very basic templates to provide a default appereance. I'll put them together in a folder. To create themes I will be able to override certain template files, but not all. Maybe some small changes would be convinient. On top, a user will have a custom look, and therefore needs also replaced some templates.
My life would be very easy, if I could do it in this way:
- Look into user's templates folder.
- If template file is not found, then look into theme's templates folder.
- If still not found, use the default on.
This should work on all commands like display & fetch, also within templates via include, insert etc.
I have a project at the moment, where I need this functionality and have to implement it via workarounds.
Basically, I think this should not hard to be implemented or cause a decrease of performance, but gives more flexibility. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Apr 17, 2003 8:04 pm Post subject: |
|
|
You could write a custom template resource for this:
Code: |
{include file="tpltree:index.tpl"}
|
This could recurse down the template tree you assign to template_dir. Be aware that smarty uses a relative path for template compiling, so you'd have to set a unique compile_id for each template directory to avoid conflicts.
Monte |
|
Back to top |
|
Roc Smarty Rookie
Joined: 17 Apr 2003 Posts: 15
|
Posted: Thu Apr 17, 2003 8:18 pm Post subject: |
|
|
Yes, of course this can be done via a resource handler. But I think, implementing the array functionality for $template_dir would be simpler than the resource handler - maybe I'm not right. Also to me, this would be more in the direction of "thinking" in a Smarty way. Resource handlers, as I understand them, are intended for more sophisticated things like retrieving data from RMDBs, LDAP, XML files... In opposite to that, I only want to have a "more intelligent" file search path, enhancing the current "file:" resource a little bit - in a fully downward compatible way. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
|
Back to top |
|
Roc Smarty Rookie
Joined: 17 Apr 2003 Posts: 15
|
Posted: Thu Apr 17, 2003 8:53 pm Post subject: |
|
|
Thanks for the links. I took a look into most of the discussions and my conclusion is, that $template_dir as array optionally will solve the basic problem. I like this concept also, since Smarty is using it for the $plugins_dir already. There was also some exception about a performance loss due to increased file system lookups. I think this will be not really the case, cause the code will be something like:
Code: |
if (is_array($this->template_dir))
// ok, now we have to do the lookups
else
// we can proceed as usual, no lookups needed
|
So for using Smarty the traditional way, no additional lookups would be necessary. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu Apr 17, 2003 9:11 pm Post subject: |
|
|
Actually, the performance issue comes up when an array IS used. In that case, several directories may have to be scanned before a template is found. Depending on the number of templates (and how often they change) this can have penalties at runtime.
One of the engine developers may have more to say on this, but I believe that plugin loading is nothing like template loading and that runtime performance is not affected by plugins having a directory array.
At anyrate, my vote is for providing this via a resource. It should be remembered that the default file system template loading is actually provided by a default (builtin) resource, namely "file:". I think the most reasonable solution is to develop a new multi-directory file system loader resource.
Perhaps a new attribute can be added to Smarty that allows the default resource handler to be specified. That way the engine doesn't change, but you could still use your own custom resource without having to add the extra syntax to all of your includes.
One thing I didn't mention when providing the links to the threads is that nearly everyone was against changing the current "file:" resource so that it could handle arrays.
xo boots |
|
Back to top |
|
Tom Sommer Administrator
Joined: 16 Apr 2003 Posts: 47 Location: Denmark
|
Posted: Thu Apr 17, 2003 9:20 pm Post subject: |
|
|
Hmm, I like file:, I think you misunderstood his point boots, he was saying that IF an array should be given as an argument, then it would search those directories.... normal users could just pass a string as always, it also allows good BC
Anyway, I don't really see a need for this... hmm |
|
Back to top |
|
Roc Smarty Rookie
Joined: 17 Apr 2003 Posts: 15
|
Posted: Thu Apr 17, 2003 9:41 pm Post subject: |
|
|
Someone else had the same idea. This is from the wiki (Smarty Feature Sugguestions):
Quote: |
* Allow array for template_dir. This is something I've done before, it gives a lot of flexabilty to create new versions for a new skin, or for multilingual sites - AlisterBulman?
|
|
|
Back to top |
|
Tom Sommer Administrator
Joined: 16 Apr 2003 Posts: 47 Location: Denmark
|
Posted: Thu Apr 17, 2003 9:43 pm Post subject: |
|
|
How would you need it to create for multilingual sites?
I'd use a translation file |
|
Back to top |
|
Roc Smarty Rookie
Joined: 17 Apr 2003 Posts: 15
|
Posted: Thu Apr 17, 2003 10:16 pm Post subject: |
|
|
Multi-language support is an additional thing. I primary use GNU gettext for that via a custom function, e.g.
Code: |
{locale id="nav_home"}
|
Maybe I should write another feature request for a consistent way to have i18n support in Smarty But this has nothing to do with the need for multiple template dirs. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu Apr 17, 2003 10:26 pm Post subject: |
|
|
I think I understood the suggestion . I'm just trying to keep the discussion revolving around a resource to handle this issue and I think there is a way to make it fairly seamless while still using Roc's idea:
- add a new 'default_resource' attribute to Smarty
- add a new built-in (?) resource to Smarty that handles dir array ('filedir' ?)
- if a string is passed as template dir, then the core sets
smarty->default_resource = 'file';
- if an array is passed as template dir, then the core sets
smarty->default_resource = 'filedir';
By-the-way, does anyone have a production quality resource that can read from a dir array that they can post here? |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Thu Apr 17, 2003 10:48 pm Post subject: |
|
|
boots wrote: | I think I understood the suggestion . I'm just trying to keep the discussion revolving around a resource to handle this issue and I think there is a way to make it fairly seamless while still using Roc's idea:
- add a new 'default_resource' attribute to Smarty
- add a new built-in (?) resource to Smarty that handles dir array ('filedir' ?)
- if a string is passed as template dir, then the core sets
smarty->default_resource = 'file';
- if an array is passed as template dir, then the core sets
smarty->default_resource = 'filedir';
By-the-way, does anyone have a production quality resource that can read from a dir array that they can post here? |
why do you want to make this one sooo special?
resources can be uses nearly seemless, by just dropping them
into the plugins dir.
this one is by far not production-quality, but feel free to make it that.
Code: |
<?php
/*
* Smarty plugin
* -------------------------------------------------------------
* Type: resource
* Name: path
* Purpose: search for plugins in an array "template_dir_path"
* which is a property of $smarty
*
* -------------------------------------------------------------
*/
function smarty_resource_path_find($tpl_name, &$smarty) {
foreach((array)$smarty->template_dir_path as $_path) {
$_filename = $_path.$tpl_name;
if (file_exists($_filename)) return $_filename;
}
}
function smarty_resource_path_source($tpl_name, &$tpl_source, &$smarty) {
$_filename = smarty_resource_path_find($tpl_name, $smarty);
if (isset($_filename)) {
if ($_fp = fopen($_filename, 'rb')) {
$tpl_source = fread($_fp, filesize($_filename));
fclose($_fp);
return true;
}
}
return false;
}
function smarty_resource_path_timestamp($tpl_name, &$tpl_source, &$smarty) {
$_filename = smarty_resource_path_find($tpl_name, $smarty);
if (isset($_filename)) {
$tpl_source = filemtime($_filename);
return true;
}
return false;
}
function smarty_resource_path_secure($tpl_name, &$smarty)
{
// assume all templates are secure
return true;
}
function smarty_resource_path_trusted($tpl_name, &$smarty)
{
// not used for templates
}
?>
|
drop it into your plugins-dir as "resource.path.php", do
$smarty->template_dir_path = array('foo', 'bar', 'bla');
$smarty->display('path:schwupdiwupp.tpl');
that's all.
i really don't see any need to make this resource "more special" than
others.
have fun
messju
(fixed 2 stupid flaws. i think it's time to go afk for me now.) |
|
Back to top |
|
|