5

I am trying to check for the existence of a text field and an asset field and set an icon variable depending on the result. I don't seem to be having much luck with defined, length and empty combinations.

Here's my code so far:

{% if (story.videoStoryUrl is defined) and (not story.storyImage | length) %}
    {% set icon = "video" %}
{% elseif (story.videoStoryUrl is empty) and (not story.storyImage | length) %}
    {% set icon = "text" %}
{% else %}
    {% set icon = "image" %}
{% endif %}

Any help appreciated :)

1
  • What do you want to happen here and what are the results you're seeing?
    – Brad Bell
    Commented Mar 23, 2016 at 23:02

1 Answer 1

5

It's probably better if you set variables to reference, so your if statements make more sense.

{% set asset = story.storyImage.first() %}
{% set videoUrl = story.videoStoryUrl %}
{% set icon = "text" %}

{% if asset | length %}
    {% if videoUrl is empty %}
        {% set icon = "video" %}
     {% else %}
        {% set icon = "image" %}
    {% endif %}
{% endif %}

{{ icon }}
2
  • Thanks! - that almost works - changed if video url and removed empty. Interestingly, can you compare two variables in one 'if'?
    – joomkit
    Commented Mar 24, 2016 at 7:30
  • Yes, it uses 'and' like you did above but my guess is the 'not' in your logic right after it is breaking it, so instead of complicating the if statement break it down to what you really want to display.
    – Xpertbot
    Commented Mar 24, 2016 at 13:49

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.