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

<pre><code>{% 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 }}
</code></pre>

CC BY-SA 3.0
2016-03-24T07:30:14.837Z
Thanks! - that almost works - changed if video url and removed empty. Interestingly, can you compare two variables in one 'if'?
joomkit
CC BY-SA 3.0
2016-03-24T13:49:32.930Z
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
CC BY-SA 3.0
2016-03-24T03:40:41.280Z
2016-03-24T03:40:41.280Z
Xpertbot
<p>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.</p>

<p>Here's my code so far:</p>

<pre><code>{% 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 %}
</code></pre>

<p>Any help appreciated :)</p>

CC BY-SA 3.0
2016-03-23T23:02:11.133Z
What do you want to happen here and what are the results you're seeing?
Brad Bell
<templating>
Twig combined conditionals to set variable
# Twig combined conditionals to set variable

(Asked by: username_0 on 2016-03-23)

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

**Tags:** templating

> **Comments:**
> **username_1**: What do you want to happen here and what are the results you're seeing?  

---

## 1 answers

### Answer by username_2 on 2016-03-24. Score: 5 | Accepted answer

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 }}

> **Comments:**
> **username_0**: Thanks! - that almost works - changed if video url and removed empty. Interestingly, can you compare two variables in one 'if'?  
> **username_2**: 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.  

---

