Sam Skirrow
Sam Skirrow

Reputation: 3697

Twig: how to write if in_array

I have the following php statement:

<?php if(in_array(get_theme_mod('navbar_position'), array('under-header', 'bottom-of-header'))) { ?>

I'd like to convert it for use with Twig (I'm using twig to build a wordpress theme), I have found this code snippet but not too sure how to adapt it for what I need:

{% if myVar in someOtherArray|keys %}

Would it be something like this:

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

...a bit of a stab in the dark.

Upvotes: 11

Views: 20171

Answers (1)

user3849602
user3849602

Reputation:

PHP :

if (in_array(get_theme_mod('navbar_position'), array('under-header', 'bottom-of-header'))) {

You don't need to apply the |keys filter as you are not testing keys. The second argument of your function is an array you declare directly in it, with Twig you have to declare it with [].

Twig :

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

Upvotes: 20

Related Questions