Page 1 of 1

How to correctly write this in smarty

Posted: Sun Sep 13, 2015 6:23 pm
by cpansewicz
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.

Re: How to correctly write this in smarty

Posted: Sun Sep 13, 2015 7:33 pm
by rotezecke
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

Posted: Sun Sep 13, 2015 7:47 pm
by Rolf
perhaps for use in 2.0, in short hand

Code: Select all

{$foo="{page_attr key='extra1'}" scope=global}

Re: How to correctly write this in smarty

Posted: Sun Sep 13, 2015 8:07 pm
by cpansewicz
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}

Re: How to correctly write this in smarty

Posted: Sun Sep 13, 2015 11:48 pm
by rotezecke
{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}
No, your example assigns the extra1 three times using different variable names. if $foo is empty, so will be $bar and $baz.
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}
if you use extra1 for other things you may need to expand your test, e.g. {if !empty($foo) && $foo != 'myotherpurpose'}

Re: How to correctly write this in smarty

Posted: Mon Sep 14, 2015 5:49 am
by cpansewicz
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'

Re: How to correctly write this in smarty

Posted: Mon Sep 14, 2015 8:39 am
by velden
try:

Code: Select all

{... childrenof="{$foo}-links" ...}

Solved: How to correctly write this in smarty

Posted: Mon Sep 14, 2015 5:01 pm
by cpansewicz
Of course. Thanks. I always forget the braces.