Smarty:在模板中取消设置数组索引
我想在 Smarty 3 模板中执行 {unset($array['index'])}
。
是否支持这样的语法(或类似的语法)?经过谷歌搜索和阅读文档后,我找不到令人满意的东西。
也许我应该向 Smarty 开发团队提出功能请求? :)
无论如何,考虑到当前可用的模板功能,您将如何做到这一点?
I would like to do {unset($array['index'])}
into a Smarty 3 template.
Is such a syntax (or similar) supported ? After Googling and doc reading I can't find something satisfying.
Maybe I should ask for a feature request to Smarty dev team ? :)
Anyway, how would you do this given the currently available template functions ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我不认为 smarty 对此有直接支持。您始终可以使用 smarty 的
{php}
标记来执行此操作,但我强烈建议您不要这样做。逻辑不属于表示级模板。I don't think there's a direct support for this in smarty. You can always do this with smarty's
{php}
tag, however I would strongly discourage you from doing so. Logic doesn't belong in a presentation-level template.不过有一种方法:-)
尽管在模板中执行此操作不是一个好主意,但有时它可能会节省您的时间。
There is a way though :-)
Even though it is not a good idea to do it in templates, sometimes it might save you time.
试试这个
try this
两步:
在Smarty3中工作。
动态索引示例:
Two steps:
It work in Smarty3.
Example with a dynamic index:
模板引擎背后的主要思想是,您可以在解析视图之前完成所有加载、逻辑、取消设置等操作。话虽这么说,您不应该取消模板中的数据,而且我很确定他们不会实现该功能请求。
我也不明白为什么你想取消设置 smarty 变量:只要不使用它,它就不会显示。
The main idea behind a template engine is that you can do all the loading, logic, unsetting etc. before you parse the view. With that being said you shouldn't be unsetting data in your template, and I'm pretty sure they will not implement that feature request.
I also don't get it why you'd want to unset a smarty variable: just don't use it and it won't get displayed.
我认为,您不应该想要这样,因为所有逻辑都必须位于代码中而不是模板中。
但是您可以编写自己的修饰符 http://www.smarty.net/docs/ en/plugins.modifiers.tpl
I think, that you shouldn't want this, 'cause all logic must be in code not in templates.
But you can write your own modifier http://www.smarty.net/docs/en/plugins.modifiers.tpl
你不覆盖这个值吗?
这对我在 smarty <3 中有用,不知道是否仍然有效,因为他们真的搞砸了 smarty。
you don't you overwrite the value?
this worked for me in smarty <3, don't know if still works as they really messed up smarty.
这里
10
是一个数组索引。Here
10
is an array index.尝试
try