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

Extend {include} tag functionality

 
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 -> Smarty Development
View previous topic :: View next topic  
Author Message
no_cache
Smarty n00b


Joined: 17 Dec 2009
Posts: 2

PostPosted: Thu Dec 17, 2009 6:52 pm    Post subject: Extend {include} tag functionality Reply with quote

I find that sometimes I need to create custom templates for particular devices (the iPhone comes to mind, but other smartphones could probably also benefit from this) in order to achieve some goal such as making the pages load faster by excluding images, or reformatting the page to be narrower, text bigger, etc.

Right now, I have code in my applications that checks the UA of a client and sets the template directory based on that - this means that I end up having a copy of all the templates for a site for each client I need to specially support! Most of the pages end up being the same, except for a select few that need special tweaks.

What I was thinking would be great is if we could extend {include} slightly to support multiple template directories. Lets say I have a particular page that needs a tweak to look better or load faster on an iPhone, but the rest of the site stays the same - lets call that page 'event_details', and assume that it gets included by a page called 'events'.

Inside events, I might have:

{foreach from=$a_events item=event}
{include file="event_details.tpl"}
{/foreach}

Smarty would have an internal variable to designate which version of the templates you want - 'USER_AGENT', 'MEDIA_TYPE', 'PREFERRED_TEMPLATE', something along those lines. If you set this variable, then you get the extended functionality, otherwise Smarty behaves the way it always has.

When this variable is set, Smarty would look first in TEMPLATE_ROOT/PREFERRED_TEMPLATE/ for your requested template file, and if it exists, it would use that file. If it doesn't exist, smarty would fall back to using the file found in TEMPLATE_ROOT/DEFAULT. That way, if all of your templates are exactly the same except for that one tweak in event_details.tpl to support the iPhone better, you can maintain only that one additional file. And, if your site gets more complicated in the future and you need to make more iPhone specific tweaks, you can do them one file at a time, and keep your code base as consolidated as possible.

Comments?
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Thu Dec 17, 2009 7:02 pm    Post subject: Reply with quote

You can supply an array of directories as $smarty->template_dir and they will be traversed in order. You can also make custom resources to behave however you wish.
Back to top
View user's profile Send private message Visit poster's website
no_cache
Smarty n00b


Joined: 17 Dec 2009
Posts: 2

PostPosted: Thu Dec 17, 2009 8:34 pm    Post subject: Reply with quote

mohrt wrote:
You can supply an array of directories as $smarty->template_dir and they will be traversed in order. You can also make custom resources to behave however you wish.


Wow, that got implemented fast! That's magical, I wish I had known about this earlier.

Is there a documentation improvement project that can be contributed towards? I love smarty but I think the documentation lacks detail a lot of the time.
Back to top
View user's profile Send private message
scuzzy
Smarty Regular


Joined: 31 Aug 2003
Posts: 84

PostPosted: Fri Dec 18, 2009 1:57 am    Post subject: Reply with quote

The array functionality has been there for a long time, it's realy great when you've got a site you need to customise on a per client basis.

http://ucn.dealernet.com.au/stocklist.php?groupid=24
http://ucn.dealernet.com.au/stocklist.php?groupid=189
http://ucn.dealernet.com.au/stocklist.php?groupid=259

Means you can create a primary folder with say all your templates, and a secondary folders with only the few templates that need to change and it will fall back to the primary folder for files that don't exist.
Back to top
View user's profile Send private message Visit poster's website
mvalviar
Smarty Rookie


Joined: 16 Aug 2010
Posts: 8

PostPosted: Fri Oct 29, 2010 10:56 pm    Post subject: Reply with quote

I need this feature. Does this still work in smarty 3? I tried it but I'm getting a template not found error. The template is not found on the first dir I listed but is found on the next.
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Fri Oct 29, 2010 11:02 pm    Post subject: Reply with quote

It does work same way in Smarty3.
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 -> Smarty Development 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