Dwoo - 你可以在循环时访问其他变量吗?
我认为在循环时无法从循环外部访问变量是正确的吗?
例如:
{循环$nav_header}
>{$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(复制我的答案 http://forum.dwoo.org/viewtopic.php?id =617 供将来参考)
好吧,你可以,但你必须确切地知道你想要什么。
$nav_header 是一个数组,因此你可以通过执行以下操作从循环外部访问它例如, {$nav_header.0.template.imagefolder} 将为您提供数组第一项所需的内容。
如果您想从循环内访问顶级变量,即如果您的主数据节点中有 $path 和 $nav_header,您可以执行 {$_.path},这是相当于 {$_parent.path}
另一种方法,如果您不知道在数组中到底在哪里查找,那就是在循环时保存一个变量,然后您可以从其中访问它,但是您可以目前无法分配给父作用域,因此您应该使用 foreach ,因为它不会移动作用域,即:
希望这有帮助。
(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. :
Hope this helps.