|
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 |
king_2 Smarty Rookie
Joined: 10 Nov 2009 Posts: 8
|
Posted: Mon Dec 21, 2009 1:48 am Post subject: Template inheritance question |
|
|
Hi!
At first, thanks for excellent library!
I have main template, let it will be 'main.tpl', with some blocks logically defined in it, such as 'banner', 'content', 'menu', and, for example, 'login'.
I want to put contents depending on URL, and may not want to display login form on some pages, or want to use different menu template on some pages, ot even to change main template (skins etc) or something like this, so I cannot do simple {insert} into main.tpl.
Now I build list of all templates needed by my framework, then do $BLOCKS['blockname'] = $smarty->fetch("$tplname"), then assign php $BLOCKS to smarty $BLOCKS, and do $smarty->display("main.tpl"). This does all as I want, placing content into blocks (which are simple smarty variables like {$BLOCK.login}).
So, in any case, now I have N smarty fetches (cached, but with overhead to check cache, read file, assign it to variable) + one display call for main template.
Can I use smarty (and what should I do?) to force smarty to build main.tpl with all blocks built by php code, inserted into main.tpl, and cache this data for cache_id that I can supply (URL + isset(userid)?1:0, for example)?
In other words, I want to get one file which will be displayed at each URL, with all data inserted into this file (except dynamic blocks). Can I?
Template inheritance as it defined now, can be used to make chain-like inheritance only. Can it be used to make tree-like structures?
Thanks! |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Mon Dec 21, 2009 7:33 pm Post subject: |
|
|
You can cache main.tpl using your cache_id. Use the is_cached( method on main.tpl to check if the page is already in the cache. Fetch and assign your subtemplate blocks only if this is not the case.
But if you use the above method Smarty does not automatically detect changes of your subtemplates. So if you have changes in your subtemplate content you must empty the cache.
What did you mean by tree like template inheritance? Can you give an example? |
|
Back to top |
|
king_2 Smarty Rookie
Joined: 10 Nov 2009 Posts: 8
|
Posted: Wed Dec 23, 2009 9:14 pm Post subject: |
|
|
Tree-like structure of templates - structure when we have one main template, and one or more templates inserted into it, each of such sub-templates can have subtemplate(s), an so on. Not simple structure, when I should set all block in fetched template.
In other words, now I should go from 'content' template to 'desing' one. But I have many blocks that needs to be assigned, and have to go from main template to sub-templates. This is exactly what I get using {include}, but I need to set which sub-template I should insert in some place of main template depending of different facts, i.e., group of user logged in, section of website and so on.
OK, so I have two sub-templates for login block, 3 subtemplates for menu, about 5-6 templates for content. For example, when I fetch sub-template for 'content' block for some URL, I will get text of article for this URL. I do not need to change this compiled text, I found all images, made all replaces and code processing, formatting and got final text to be inserted into CONTENT block of main template. For this URL I will always need this composed text. If text will change, I will just delete cache, this is not about I worrying about.
For another sub-template, menu, I should fetch it more frequently, because I need to customise menu for user depending on section of site, group of user.
Third sub-template, that produce no visible effect, I include into first template with {dymnamic}, if simply increments hits counter for article.
As I understand, fetch checks that it has compiled template, reads it, then do same with caches used in that template, and returns the result. Can I avoid that checks for some templates?
In other words, if I have structure:
main.tpl
- login.tpl
- menu.tpl
- content.tpl
-- article.tpl
-- paging.tpl
-- hitcounter.tpl
- footer.tpl
I need to be dynamic menu.tpl and hitcounter.tpl only. If I absolutely sure that for this URL+cache_id I want to get same result always, and I want all theese templates to be compiled into one file, that has static text for article, footer and all other, and has dynamic only two templates? And not getting theese templates by fetch, but compiled into this one file?
Can I tune smarty to get it or I goes wrong way to optimize performance?
p.s. Smarty3 is 30-40% slower than smarty2 in my case, even with cache enabled and templates compiled |
|
Back to top |
|
ChrisFah Smarty Pro
Joined: 02 Nov 2009 Posts: 159 Location: Traun, Austria
|
Posted: Thu Dec 24, 2009 11:47 am Post subject: |
|
|
I also use one main template which contains only many sub-templates.
I disable caching at main template, and use caching with cache
_id for each sub-template. cache_id of each subtemplate is therefor depending on things that content is depending on. Eg: show loginbox only if $session[custoner_id] is not set, else a account box will be shown.
With this i can cache nearly every template. But you have to think about how to create your cache_id.
Regards, Chris |
|
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
|
|