如果是真的话,Twig剥离部分字符串(Twig strip part of string if true)

系统教程 行业动态 更新时间:2024-06-14 17:01:33
如果是真的话,Twig剥离部分字符串(Twig strip part of string if true)

我想知道当字符串包含单词时是否可以删除字符串的部分。 我在这里阅读了文档,但这并不是我想要的。

假设我有这个:

<ul> {% for category in shop.categories %} <li>{{ category.title }}</li> {% endfor %} </ul> Outcome of above would be: <ul> <li>Test category 1</li> <li>Test category 2</li> <li>Testing category 3</li> </ul>

你怎么能做一个if声明,当单词test或testing在{{ category.title }}里面时,这个单词会被从那个标题中删除?!

因此,最终结果将是category 1, category 2, category 3 。

I was wondering if it is possible to strip part(s) of a string when the string contains a word. I've read the doc here but that's not exactly what I'm looking for.

Let's say I have this:

<ul> {% for category in shop.categories %} <li>{{ category.title }}</li> {% endfor %} </ul> Outcome of above would be: <ul> <li>Test category 1</li> <li>Test category 2</li> <li>Testing category 3</li> </ul>

How could you make a if statement that when the word test or testing is inside {{ category.title }} this word is stripped from that title?!

So end result will be category 1, category 2, category 3.

最满意答案

twig替换过滤器相当于php的str_replace()。

http://twig.sensiolabs.org/doc/filters/replace.html

{{ category.title | replace( {'Testing ': '', 'Test ': '' } ) }}

The twig replace filter is the equivalent of php's str_replace().

http://twig.sensiolabs.org/doc/filters/replace.html

{{ category.title | replace( {'Testing ': '', 'Test ': '' } ) }}

更多推荐

本文发布于:2023-04-20 18:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/c8f1da6f3a3ae94b9c6a3953d95aa01e.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   strip   Twig   true   string

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!