Hi,
I wrote some code to call in links to my page depending on what value is in the Extra Attribute. (Hope that makes sense). I am not very good with smarty, and was wondering if there was a more concise way to write the below code. There will be about 6 or so assigned variables in total. Would it be in an array? I am not sure how to write an array for something like this.
<!--Variables-->
{assign var="foo" value="foo-links"}
{assign var="bar" value="bar-links"}
{assign var="baz" value="baz-links"}
...etc
<--Statements-->
{if {page_attr key='extra1'} == 'Foo'}{menu template="quick" childrenof=$foo show_root_siblings="1" collapse="1"}{/if}
{if {page_attr key='extra1'} == 'Bar'}{menu template="quick" childrenof=$bar show_root_siblings="1" collapse="1"}{/if}
{if {page_attr key='extra1'} == 'Baz'}{menu template="quick" childrenof=$baz show_root_siblings="1" collapse="1"}{/if}
...etc
Thank you in advance.
How to correctly write this in smarty
-
cpansewicz
- Forum Members

- Posts: 142
- Joined: Fri Jun 17, 2011 12:22 am
Re: How to correctly write this in smarty
to reduce some code, you could set the value of extra1 to the value needed in menu statement.
Code: Select all
{page_attr key='extra1' assign=foo}
{if !empty($foo)}{menu template="quick" childrenof=$foo show_root_siblings="1" collapse="1"}{/if}Re: How to correctly write this in smarty
perhaps for use in 2.0, in short hand
Code: Select all
{$foo="{page_attr key='extra1'}" scope=global}- + - + - + - + - + - + -
LATEST TUTORIAL AT CMS CAN BE SIMPLE:
Migrating Company Directory module to LISE
Migrating Company Directory module to LISE
- + - + - + - + - + - + -
-
cpansewicz
- Forum Members

- Posts: 142
- Joined: Fri Jun 17, 2011 12:22 am
Re: How to correctly write this in smarty
I would have to set that up like below? Is this correct? (I haven't updated to 2.0 yet)
{page_attr key='extra1' assign=foo}
{page_attr key='extra1' assign=bar}
{page_attr key='extra1' assign=baz}
{if !empty($foo)}
{menu template="quick" childrenof=$foo show_root_siblings="1" collapse="1"}
{elseif !empty($bar)}
{menu template="quick" childrenof=$bar show_root_siblings="1" collapse="1"}
{elseif !empty($baz)}
{menu template="quick" childrenof=$baz show_root_siblings="1" collapse="1"}
{/if}
{page_attr key='extra1' assign=foo}
{page_attr key='extra1' assign=bar}
{page_attr key='extra1' assign=baz}
{if !empty($foo)}
{menu template="quick" childrenof=$foo show_root_siblings="1" collapse="1"}
{elseif !empty($bar)}
{menu template="quick" childrenof=$bar show_root_siblings="1" collapse="1"}
{elseif !empty($baz)}
{menu template="quick" childrenof=$baz show_root_siblings="1" collapse="1"}
{/if}
Re: How to correctly write this in smarty
No, your example assigns the extra1 three times using different variable names. if $foo is empty, so will be $bar and $baz.{page_attr key='extra1' assign=foo}
{page_attr key='extra1' assign=bar}
{page_attr key='extra1' assign=baz}
{if !empty($foo)}
{menu template="quick" childrenof=$foo show_root_siblings="1" collapse="1"}
{elseif !empty($bar)}
{menu template="quick" childrenof=$bar show_root_siblings="1" collapse="1"}
{elseif !empty($baz)}
{menu template="quick" childrenof=$baz show_root_siblings="1" collapse="1"}
{/if}
Assuming that you use extra1 for nothing else but the above purpose (creating a menu), my previous post should be all you need. Rolf's suggestion adds a scope to the assigned variable. Depending on where you test for $foo, the scope may be necessary.
Code: Select all
{$foo="{page_attr key='extra1'}" scope=global}
{if !empty($foo)}{menu template="quick" childrenof=$foo show_root_siblings="1" collapse="1"}{/if}
-
cpansewicz
- Forum Members

- Posts: 142
- Joined: Fri Jun 17, 2011 12:22 am
Re: How to correctly write this in smarty
Thank you! Now I get it, and this works great. Thanks again.
I know this is completely wrong, but for the menu, is there a way to write something like: childrenof=$foo+'-links'
I know this is completely wrong, but for the menu, is there a way to write something like: childrenof=$foo+'-links'
Re: How to correctly write this in smarty
try:
Code: Select all
{... childrenof="{$foo}-links" ...}-
cpansewicz
- Forum Members

- Posts: 142
- Joined: Fri Jun 17, 2011 12:22 am
Solved: How to correctly write this in smarty
Of course. Thanks. I always forget the braces.


