Twig:如果在in_array中怎么写

Sam*_*row 11 php arrays if-statement twig

我有以下php语句:

<?php if(in_array(get_theme_mod('navbar_position'), array('under-header', 'bottom-of-header'))) { ?>
Run Code Online (Sandbox Code Playgroud)

我想将它转换为与Twig一起使用(我正在使用twig来构建一个wordpress主题),我发现这段代码片段但不太确定如何根据我的需要调整它:

{% if myVar in someOtherArray|keys %}
Run Code Online (Sandbox Code Playgroud)

它会是这样的:

{% if theme.theme_mod('navbar_position') in 'under-header', 'bottom-of-header'|keys %}
Run Code Online (Sandbox Code Playgroud)

......在黑暗中有点刺伤.

小智 19

PHP:

if (in_array(get_theme_mod('navbar_position'), array('under-header', 'bottom-of-header'))) {
Run Code Online (Sandbox Code Playgroud)

您不需要应用|keys过滤器,因为您没有测试密钥.你的函数的第二个参数是你直接在其中声明的数组,你必须使用Twig声明它[].

树枝:

{% if theme.theme_mod('navbar_position') in ['under-header', 'bottom-of-header'] %}
Run Code Online (Sandbox Code Playgroud)