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

Array for $template_dir
Goto page 1, 2  Next
 
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 -> Feature Requests
View previous topic :: View next topic  
Author Message
Roc
Guest





PostPosted: Thu Apr 17, 2003 7:21 pm    Post subject: Array for $template_dir Reply with quote

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

PostPosted: Thu Apr 17, 2003 7:23 pm    Post subject: Reply with quote

How would this be useful?
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger
Guest






PostPosted: Thu Apr 17, 2003 7:34 pm    Post subject: Re: Array for $template_dir Reply with quote

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





PostPosted: Thu Apr 17, 2003 7:39 pm    Post subject: Reply with quote

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

PostPosted: Thu Apr 17, 2003 8:04 pm    Post subject: Reply with quote

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


Joined: 17 Apr 2003
Posts: 15

PostPosted: Thu Apr 17, 2003 8:18 pm    Post subject: Reply with quote

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


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

PostPosted: Thu Apr 17, 2003 8:20 pm    Post subject: Reply with quote

There was at least one recent discussion concerning this topic in the newsgroup.

I have dug up the relevant links in the thread for your enjoyment.

It all starts with a simple query from Luis:
http://lists.php.net/article.php?group=php.smarty.dev&article=1004

Simple ideas start pouring in:
http://lists.php.net/article.php?group=php.smarty.dev&article=1005
http://lists.php.net/article.php?group=php.smarty.dev&article=1006
http://lists.php.net/article.php?group=php.smarty.dev&article=1007
http://lists.php.net/article.php?group=php.smarty.dev&article=1008
http://lists.php.net/article.php?group=php.smarty.dev&article=1009
http://lists.php.net/article.php?group=php.smarty.dev&article=1011
http://lists.php.net/article.php?group=php.smarty.dev&article=1016

Luis refines his question and starts a new thread:
http://lists.php.net/article.php?group=php.smarty.dev&article=1017

Which starts to get a lot of traction in terms of approach:
http://lists.php.net/article.php?group=php.smarty.dev&article=1018
http://lists.php.net/article.php?group=php.smarty.dev&article=1019
http://lists.php.net/article.php?group=php.smarty.dev&article=1020
http://lists.php.net/article.php?group=php.smarty.dev&article=1021
http://lists.php.net/article.php?group=php.smarty.dev&article=1022
http://lists.php.net/article.php?group=php.smarty.dev&article=1023

Some threads don't die:
http://lists.php.net/article.php?group=php.smarty.dev&article=1032
http://lists.php.net/article.php?group=php.smarty.dev&article=1033
http://lists.php.net/article.php?group=php.smarty.dev&article=1034
http://lists.php.net/article.php?group=php.smarty.dev&article=1035

Of the different approaches suggested, one that seemed promising was to use a custom resource to handle this need instead of altering the Smarty core.

I don't think that the thread was completely resolved because a consensus was never reached nor was a general solution provided.

In the end, it APPEARS that this is not missing functionality as there are valid ways to extend the existing Smarty to handle this.

xo boots
Back to top
View user's profile Send private message
Roc
Smarty Rookie


Joined: 17 Apr 2003
Posts: 15

PostPosted: Thu Apr 17, 2003 8:53 pm    Post subject: Reply with quote

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


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

PostPosted: Thu Apr 17, 2003 9:11 pm    Post subject: Reply with quote

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


Joined: 16 Apr 2003
Posts: 47
Location: Denmark

PostPosted: Thu Apr 17, 2003 9:20 pm    Post subject: Reply with quote

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 Smile

Anyway, I don't really see a need for this... hmm
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger
Roc
Smarty Rookie


Joined: 17 Apr 2003
Posts: 15

PostPosted: Thu Apr 17, 2003 9:41 pm    Post subject: Reply with quote

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


Joined: 16 Apr 2003
Posts: 47
Location: Denmark

PostPosted: Thu Apr 17, 2003 9:43 pm    Post subject: Reply with quote

How would you need it to create for multilingual sites?

I'd use a translation file
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger
Roc
Smarty Rookie


Joined: 17 Apr 2003
Posts: 15

PostPosted: Thu Apr 17, 2003 10:16 pm    Post subject: Reply with quote

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 Very Happy But this has nothing to do with the need for multiple template dirs.
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Thu Apr 17, 2003 10:26 pm    Post subject: Reply with quote

I think I understood the suggestion Wink. I'm just trying to keep the discussion revolving around a resource to handle this issue Smile 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
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Thu Apr 17, 2003 10:48 pm    Post subject: Reply with quote

boots wrote:
I think I understood the suggestion Wink. I'm just trying to keep the discussion revolving around a resource to handle this issue Smile 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
View user's profile Send private message Send e-mail Visit poster's website
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 -> Feature Requests All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
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