1

I have a following code and I want to be able to assign value of count conditionally.

{% set obj = {
 count: CONDITIONAL VALUE,
 city: 'London',
 country: 'UK'
} %}

I have an object with many properties, I need to check if that object has property counter . If it has I want to assign value of counter to count above else I want to pass 2 as default value to count. In javascript i could do:- count: counter || 2

Thank you very much

1 Answer 1

1

If you want a default value for a non-existing or null variable u'd use the filter default

{% set obj = {
 count: my_value|default(2),
 city: 'London',
 country: 'UK'
} %}


{{ obj.count }}

demo

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! Happy new year.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.