简体   繁体   中英

Multiple string values containment check in twig?

I would like to check for multiple string values containment in a variable, so far I know that I can check for a single string value containment in a variable but did not find anything on multiple values containment.

Can anyone help me out?

What I have right now:

{% if "VenuesController::detailsAction" not in controllerAndActionName %}

What I want to do:

{% if ["VenuesController::detailsAction", "VmsController::indexAction", "DefaultController::headerAction"] not in controllerAndActionName %}

Is this possible?

With use of custom Twig extension, I made it possible the following way:

public function getFunctions()
{
   return array(
     new \Twig_SimpleFunction('checkMultipleStringValuesContainment', array($this, 'checkMultipleStringValuesContainment'))
   );
}
public function checkMultipleStringValuesContainment($values, $variable) {
    $joinedValues = join($values, "|");
    if (preg_match('~('.$joinedValues.')~', $variable)) {
        return true;
    } else {
        return false;
    }
}

And then again:

{% if checkMultipleStringValuesContainment(["VenuesController::detailsAction", "StaticController::howitworksAction", "StaticController::listyourvenueAction"], controllerAndActionName) == false  %}

您需要使用<string> not in <array> <array> not in <string>而不是<string> not in <array> <array> not in <string>

{% if controllerAndActionName not in ["VenuesController::detailsAction", "VmsController::indexAction", "DefaultController::headerAction"] %}

Don't know if you can do this directly in twig , but a workaround should be this

{% set bool = true %}
{% for string in ["VenuesController::detailsAction", "VmsController::indexAction", "DefaultController::headerAction"] %}
    {% if string in controllerAndActionName %}
        {% set bool = false %}
    {% endif %}
{% endfor %}
{% if bool %}
    Foo
{% endif %}

demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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