|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
no_cache Smarty n00b
Joined: 17 Dec 2009 Posts: 2
|
Posted: Thu Dec 17, 2009 6:52 pm Post subject: Extend {include} tag functionality |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Dec 17, 2009 7:02 pm Post subject: |
|
|
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 |
|
no_cache Smarty n00b
Joined: 17 Dec 2009 Posts: 2
|
Posted: Thu Dec 17, 2009 8:34 pm Post subject: |
|
|
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 |
|
scuzzy Smarty Regular
Joined: 31 Aug 2003 Posts: 84
|
|
Back to top |
|
mvalviar Smarty Rookie
Joined: 16 Aug 2010 Posts: 8
|
Posted: Fri Oct 29, 2010 10:56 pm Post subject: |
|
|
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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Fri Oct 29, 2010 11:02 pm Post subject: |
|
|
It does work same way in Smarty3. |
|
Back to top |
|
|
|
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
|