简体   繁体   English

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

[英]Find characters in string in TWIG

I have this hidden field : 我有这个隐藏的领域:

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

And this input text 而这个input text

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

I want do a test on the value of the PartNumber value="{{ price.getPartNumber }}" 我想对PartNumber value="{{ price.getPartNumber }}"进行测试value="{{ price.getPartNumber }}"

If it contains this String 35-l0l-1003 I should change the value of the value="{{ price.getLt }}" to 30 如果它包含此字符串35-l0l-1003 ,则应将value="{{ price.getLt }}"value="{{ price.getLt }}"更改为30

I should use strpbrk to search a string for any of a set of characters. 我应该使用strpbrk在字符串中搜索一组字符中的任何一个。 I'm beginner in Twig and I don't know how can I add it. 我是Twig初学者,不知道该如何添加。 Please can you help me. 请你帮我一下。 Thank you. 谢谢。

In your case just use : 在您的情况下,只需使用:

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

However you can add almost every function in Twig. 但是,您几乎可以在Twig中添加所有功能。 Here is a simple implementation of the file_exist function (I'm actually doing it in my Controller core class) 这是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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM