This evaluates to false
{% if block.imageWidth starts with block.imageWidth %}
But this evaluates to true
{% if '{{ block.imageWidth }}' starts with '{{ block.imageWidth }}' %}
Some sort of issue with Craft's Twig integration?
I'm going to assume that your imageWidth
field is a dropdown menu... correct me if I'm wrong. When you output a dropdown fieldtype in your templates, it's an object, not a string.
{% if block.imageWidth starts with block.imageWidth %}
The starts with
comparison operator is designed for strings, but here it's handling two objects. Makes sense that it would just give up and evaluate to false.
{% if '{{ block.imageWidth }}' starts with '{{ block.imageWidth }}' %}
In Twig, you can't nest tags. Those strings are being read literally, and not parsed out into their respective values. So what you've got here is a comparison of two identical strings... Makes sense that this would evaluate to true.
{% if block.imageWidth.value starts with block.imageWidth.value %}
Working under the assumption that imageWidth
is a dropdown menu, then you are actually getting an object from block.imageWidth
. Twig isn't confident that you actually want a string here, so you'll need to append .value
to specifically get the value of your field.
I've seen this trip up a few people, but it makes perfect sense once you wrap your head around it. Many objects in Craft will try to automatically "stringify" themselves when output directly using curly braces. But when it's not a direct output tag, you'll generally need to tell Twig that you specifically want the string value, and not the object itself.