Smarty:在模板中取消设置数组索引

发布于 2024-12-05 04:37:16 字数 191 浏览 2 评论 0原文

我想在 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 技术交流群。

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

发布评论

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

评论(9

枉心 2024-12-12 04:37:16

我不认为 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.

楠木可依 2024-12-12 04:37:16

不过有一种方法:-)

{$array=$array|array_diff_key:(['index']|array_flip)}

尽管在模板中执行此操作不是一个好主意,但有时它可能会节省您的时间。

There is a way though :-)

{$array=$array|array_diff_key:(['index']|array_flip)}

Even though it is not a good idea to do it in templates, sometimes it might save you time.

心如荒岛 2024-12-12 04:37:16

试试这个

{$array.index = null}

try this

{$array.index = null}
半暖夏伤 2024-12-12 04:37:16

两步:

{$array.index = null}
{$array = $array|array_filter}

在Smarty3中工作。

动态索引示例:

{foreach $array as $item}
    {if $item.foo == 'bar'}
        <h1>{$item.text nofilter}</h1>
        {* unset item from array *}
        {$array[$item@key] = null}
        {$array = $array|array_filter}
        {break}
    {/if}
{/foreach}

{if $array}
    <ul>
    {foreach $array as $item}
        <li>{$item.text nofilter}</li>
    {/foreach}
    </ul>
{/if}

Two steps:

{$array.index = null}
{$array = $array|array_filter}

It work in Smarty3.

Example with a dynamic index:

{foreach $array as $item}
    {if $item.foo == 'bar'}
        <h1>{$item.text nofilter}</h1>
        {* unset item from array *}
        {$array[$item@key] = null}
        {$array = $array|array_filter}
        {break}
    {/if}
{/foreach}

{if $array}
    <ul>
    {foreach $array as $item}
        <li>{$item.text nofilter}</li>
    {/foreach}
    </ul>
{/if}
风吹过旳痕迹 2024-12-12 04:37:16

模板引擎背后的主要思想是,您可以在解析视图之前完成所有加载、逻辑、取消设置等操作。话虽这么说,您不应该取消模板中的数据,而且我很确定他们不会实现该功能请求。

我也不明白为什么你想取消设置 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.

单调的奢华 2024-12-12 04:37:16

我认为,您不应该想要这样,因为所有逻辑都必须位于代码中而不是模板中。

但是您可以编写自己的修饰符 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

夜清冷一曲。 2024-12-12 04:37:16

你不覆盖这个值吗?

{assign var="array" value=array()}
{$array['index']='1'}
{$array['index2']='2'}
{$array['index']=''}
{$array|print_r}

这对我在 smarty <3 中有用,不知道是否仍然有效,因为他们真的搞砸了 smarty。

you don't you overwrite the value?

{assign var="array" value=array()}
{$array['index']='1'}
{$array['index2']='2'}
{$array['index']=''}
{$array|print_r}

this worked for me in smarty <3, don't know if still works as they really messed up smarty.

回梦 2024-12-12 04:37:16
{$array=$links.lists|array_diff_key:(['10']|array_flip)}
{$array|print_r}

这里10是一个数组索引。

{$array=$links.lists|array_diff_key:(['10']|array_flip)}
{$array|print_r}

Here 10 is an array index.

百善笑为先 2024-12-12 04:37:16

尝试

{分配var=$array.index value=null)}

try

{assign var=$array.index value=null)}

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