tags:

views:

27

answers:

1

Hi, am I right in thinking you can't access a variable from outside the loop whilst looping?

For example:

{loop $nav_header}
<li><a href="{$link}"><img src="{$template.imagefolder}/{$icon}" width="48" height="48" border="0" alt="{$title}" /><br />{$title}</a></li>
{/loop}

where I'm trying to use $template.imagefolder (as an example) from outside the loop?

Yours,
Chris

+1  A: 

(Copying my answer from http://forum.dwoo.org/viewtopic.php?id=617 for future reference)

Well, you can, but you've to know what you want exactly..

$nav_header is an array so you can access it from outside the loop by doing {$nav_header.0.template.imagefolder} for example, that'll give you what you want for the first item of the array.

If you want to access a top level variable from within the loop, i.e. if you got $path and $nav_header in your main data node, you'd do {$_.path}, which is the equivalent of {$_parent.path}

The other approach, if you don't know where to look exactly in your array, is to save a variable while you're looping, and then you can access it from out of it, however you can't assign to the parent scope at the moment, so you should use foreach for that, since it doesn't move the scope, i.e. :

{foreach $nav_header elem}
    <li><a href="{$elem.link}"><img src="{$elem.template.imagefolder}/{$elem.icon}" width="48" height="48" border="0" alt="{$elem.title}" /><br />{$elem.title}</a></li>
    {if $elem.title == "foo"}{$folder = $elem.template.imagefolder}{/if}
{/foreach}

{$folder}

Hope this helps.

Seldaek