树枝中的多个字符串值包含检查?
Multiple string values containment check in twig?
我想检查一个变量中是否包含多个字符串值,到目前为止我知道我可以检查一个变量中是否包含单个字符串值但没有在多个值包含中找到任何内容。
谁能帮帮我?
我现在拥有的是:
{% if "VenuesController::detailsAction" not in controllerAndActionName %}
我想做的事情:
{% if ["VenuesController::detailsAction", "VmsController::indexAction", "DefaultController::headerAction"] not in controllerAndActionName %}
这可能吗?
您需要使用 <string> not in <array>
而不是 <array> not in <string>
:
{% if controllerAndActionName not in ["VenuesController::detailsAction", "VmsController::indexAction", "DefaultController::headerAction"] %}
不知道您是否可以直接在 twig
中执行此操作,但解决方法应该是这样
{% 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 %}
通过使用自定义 Twig 扩展,我可以通过以下方式实现:
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;
}
}
然后再说一遍:
{% if checkMultipleStringValuesContainment(["VenuesController::detailsAction", "StaticController::howitworksAction", "StaticController::listyourvenueAction"], controllerAndActionName) == false %}
我想检查一个变量中是否包含多个字符串值,到目前为止我知道我可以检查一个变量中是否包含单个字符串值但没有在多个值包含中找到任何内容。
谁能帮帮我?
我现在拥有的是:
{% if "VenuesController::detailsAction" not in controllerAndActionName %}
我想做的事情:
{% if ["VenuesController::detailsAction", "VmsController::indexAction", "DefaultController::headerAction"] not in controllerAndActionName %}
这可能吗?
您需要使用 <string> not in <array>
而不是 <array> not in <string>
:
{% if controllerAndActionName not in ["VenuesController::detailsAction", "VmsController::indexAction", "DefaultController::headerAction"] %}
不知道您是否可以直接在 twig
中执行此操作,但解决方法应该是这样
{% 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 %}
通过使用自定义 Twig 扩展,我可以通过以下方式实现:
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;
}
}
然后再说一遍:
{% if checkMultipleStringValuesContainment(["VenuesController::detailsAction", "StaticController::howitworksAction", "StaticController::listyourvenueAction"], controllerAndActionName) == false %}