|
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 |
ilyalyu Smarty Regular
Joined: 03 Nov 2009 Posts: 72
|
Posted: Thu Jul 12, 2012 2:28 pm Post subject: Template inheritance: passing variables to parent template |
|
|
Is there a way to pass variable to parent template? Something like this:
{extends 'parent.html' var1='value1' var2='value2'} |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Jul 12, 2012 2:49 pm Post subject: |
|
|
You shouldn't have to do that, what are you trying to do? Any block you are extending can use the local vars. |
|
Back to top |
|
ilyalyu Smarty Regular
Joined: 03 Nov 2009 Posts: 72
|
Posted: Thu Jul 12, 2012 8:38 pm Post subject: |
|
|
I need to use one variable in several blocks. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Thu Jul 12, 2012 9:28 pm Post subject: |
|
|
Template inheritance does merge/replace blocks of child templates at compile time into the parent template. The resulting code is rendered in the context of one template object. So assigned local variables have not the scope of a block, but the scope of the whole parent template. |
|
Back to top |
|
mleko Smarty n00b
Joined: 25 Mar 2011 Posts: 1
|
Posted: Fri Jul 27, 2012 1:05 pm Post subject: |
|
|
Actually pasing variable to parent could quite usefull
Example
main.tpl
Code: | <ul id="mainmenu">
<li {if $mainMenuActive|default == "dashboard"}class="active"{/if}><a href="/">Dashboard</a></li>
<li {if $mainMenuActive|default == "calendar"}class="active"{/if}><a href="/Calendar">Calendar</a></li>
</ul>
<div id="page">
{block name='content'}{/block}
</div>
|
dasboard.tpl
Code: | {extends file='main.tpl'}
{block name='content'}
{assign var="mainMenuActive" value="dashboard" scope="parent"}
Some content
{/block}
|
There might be syntax errors, as I don't use block/extend in favour of capture/include but i hope idea is clear.
I know there is partial workaround http://www.smarty.net/forums/viewtopic.php?t=17425, https://gist.github.com/832525. But it breaks on deeper inheritance.
Capture/include does this gracefully even allow scoping.
I think that parent blocks should be able to use vars assigned in children. It might look that this breaks encapsulation, but it doesn't. If you look on this from perspective : "parent declare(use) property, derived from it child define(assign) this property", encapsulation is fine. Of course there can be some trouble with multiple inheritance, like in every language that allows it is. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Sat Jul 28, 2012 8:47 am Post subject: |
|
|
As I explained before in template inheritance child and parent telates have the same scope. So all variables assigned in a chiled template are seen by the parent template.
In your example you use $mainMenuActive before it gets assigned by the 'content' block that does not work. Child blocks are executed when they apear in the parent.
The scope=parent of the {assign} tag has nothing to do with template inheritance. The scope here references the calling template when you use {include} |
|
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
|