|
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 |
dlanz Smarty Rookie
Joined: 14 May 2015 Posts: 5
|
Posted: Fri Jun 12, 2015 2:40 pm Post subject: Assigning Templates to variables - Which way is better? |
|
|
I started out assigning templates to variables so I can output the variable within another template using code similar to this:
Code: |
$tpl->assign('view1', $tpl->fetch('view1.tpl.htm'));
$tpl->batchlist('main.tpl.htm');
|
And then output the fetched template from main.tpl.htm using:
But then I see another method which is explained here:
http://www.smarty.net/forums/viewtopic.php?t=74&sid=2bfc83cba6d5f63f7e6a4c210b6fdb4a
I'm noticing a ton of PHP E_NOTICE errors which trace back to lines similar to:
Code: | $tpl->assign('view1', $tpl->fetch('view1.tpl.htm')); |
So I'm in the process of trying method B to see if that stops the errors.
I'm guessing method B is the correct way, but can anyone explain why?
Note*: I'm also going to try this to see is there is any difference (all though I don't think there would be):
Code: |
$view1 = $tpl->fetch('view1.tpl.htm');
$tpl->assign('view1', $view1);
$tpl->batchlist('main.tpl.htm');
|
|
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
|
Back to top |
|
dlanz Smarty Rookie
Joined: 14 May 2015 Posts: 5
|
Posted: Mon Jun 15, 2015 5:58 pm Post subject: |
|
|
Okay, so I read through the links you provided and some additional resources. I understand the advantages of using the {include} tags and even {extends} tag. Performance wise {extends} is better because it caches a single template rather than each included template individually.
I've played around with the extends tag and it seems as if I can't use a variable to represent the path of any {include} or {extends} tags. That's a little different (and kind of the opposite) than how I had my templates setup, but with a little re-working I managed to switch methods. At first finding out I couldn't use variables for the paths I though "what good is that then?". But I just had to look at the situation differently.
Thank you for your help. |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Mon Jun 15, 2015 9:39 pm Post subject: |
|
|
You shouldn't need to use variables there, rather, you create templates for every occasion, that extend main theme.
At most, you do a "case..switch" statement to select an appropriate theme in case of different rendering for different devices, if you are not using compile_id. But you really should. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Mon Jun 15, 2015 9:52 pm Post subject: |
|
|
To merge {include} sub templates into a single compiled template for better performance use
$smarty->merge_compiled_includes = true;
or
{include .... inline} |
|
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
|