How to correctly write this in smarty

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Post Reply
cpansewicz
Forum Members
Forum Members
Posts: 142
Joined: Fri Jun 17, 2011 12:22 am

How to correctly write this in smarty

Post 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.
User avatar
rotezecke
Power Poster
Power Poster
Posts: 411
Joined: Fri Apr 18, 2008 9:34 pm

Re: How to correctly write this in smarty

Post 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}
User avatar
Rolf
Power Poster
Power Poster
Posts: 7825
Joined: Wed Apr 23, 2008 7:53 am
Contact:

Re: How to correctly write this in smarty

Post by Rolf »

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
- + - + - + - + - + - + -
Image
cpansewicz
Forum Members
Forum Members
Posts: 142
Joined: Fri Jun 17, 2011 12:22 am

Re: How to correctly write this in smarty

Post 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}
User avatar
rotezecke
Power Poster
Power Poster
Posts: 411
Joined: Fri Apr 18, 2008 9:34 pm

Re: How to correctly write this in smarty

Post 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'}
cpansewicz
Forum Members
Forum Members
Posts: 142
Joined: Fri Jun 17, 2011 12:22 am

Re: How to correctly write this in smarty

Post 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'
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3497
Joined: Mon Nov 28, 2011 9:29 am

Re: How to correctly write this in smarty

Post by velden »

try:

Code: Select all

{... childrenof="{$foo}-links" ...}
cpansewicz
Forum Members
Forum Members
Posts: 142
Joined: Fri Jun 17, 2011 12:22 am

Solved: How to correctly write this in smarty

Post by cpansewicz »

Of course. Thanks. I always forget the braces.
Post Reply

Return to “Modules/Add-Ons”