3

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 :)

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

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 }}
|improve this answer|||||
  • Thanks! - that almost works - changed if video url and removed empty. Interestingly, can you compare two variables in one 'if'? – joomkit Mar 24 '16 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 Mar 24 '16 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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