在TWIG中查找字符串中的字符

时间:2017-06-21 07:45:40

标签: php html twig

我有这个隐藏的领域:

<input type="hidden" id="PartNum" name="pn" value="{{ price.getPartNumber }}">

这是input text

<input type="hidden" id="ltid" name="lt" value="{{ price.getLt }}">

我想对PartNumber value="{{ price.getPartNumber }}"

的值进行测试

如果它包含此字符串35-l0l-1003,我应该将value="{{ price.getLt }}"的值更改为30

我应该使用strpbrk在字符串中搜索任何一组字符。 我是Twig的初学者,我不知道如何添加它。 请你帮帮我。 谢谢。

1 个答案:

答案 0 :(得分:0)

在你的情况下,只需使用:

 {{'Toto' in 'Tototatattiti'}} //Will return true
 {{'Toto' in 'Tata' }} //Will return false

但是你可以在Twig中添加几乎所有功能。这是file_exist函数的简单实现(我实际上是在我的Controller核心类中完成的)

    //Getting twig by a loader
    $twig = $this->templating;
    //Getting twig environnement
    $env = $twig->getTwig();
    //Adding the new function
    $fe = new Twig_SimpleFunction('file_exists', 'file_exists');
    $env->addFunction($fe);