Dwoo - 你可以在循环时访问其他变量吗?

发布于 2024-08-18 19:44:45 字数 356 浏览 4 评论 0原文

我认为在循环时无法从循环外部访问变量是正确的吗?

例如:

{循环$nav_header}

  • {$title}
    >{$title}

  • {/循环}

    我试图从循环外部使用 $template.imagefolder (作为示例)?

    你的,
    克里斯

    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

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(1

    银河中√捞星星 2024-08-25 19:44:45

    (复制我的答案 http://forum.dwoo.org/viewtopic.php?id =617 供将来参考)

    好吧,你可以,但你必须确切地知道你想要什么。

    $nav_header 是一个数组,因此你可以通过执行以下操作从循环外部访问它例如, {$nav_header.0.template.imagefolder} 将为您提供数组第一项所需的内容。

    如果您想从循环内访问顶级变量,即如果您的主数据节点中有 $path 和 $nav_header,您可以执行 {$_.path},这是相当于 {$_parent.path}

    另一种方法,如果您不知道在数组中到底在哪里查找,那就是在循环时保存一个变量,然后您可以从其中访问它,但是您可以目前无法分配给父作用域,因此您应该使用 foreach ,因为它不会移动作用域,即:

    {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}
    

    希望这有帮助。

    (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.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文