我想知道当字符串包含单词时是否可以删除字符串的部分。 我在这里阅读了文档,但这并不是我想要的。
假设我有这个:
<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 ': '' } ) }}更多推荐
发布评论