Answers.Body: <p>Try <code>{{ social_image|default(social_image_fallback) }}</code>. I've had good results using Twig's default filter over testing variables, even with multiple fallbacks. (<a href="https://craftcms.stackexchange.com/a/8983/157">like this</a>).</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-07T12:46:57.227Z
Answers.LastActivityDate: 2015-05-07T12:46:57.227Z
Answers.LastEditDate: 2017-04-13T12:50:59.930Z
Answers.UserDisplayName: Mike Pepper
Answers.Body: <p>If you use <code>defined</code> it'll always be true because you at the minimum are defining an empty string. <code>not null</code> won't work because a null is different from a false condition (which an empty string matches).</p>
  
  <p>@mike-pepper and @carlcs both have good suggestions for getting what you need. My personally suggestion would be:</p>
  
  <pre><code>{% set social_image = "" %}
  {% for image in entry.metaImage %}
      {% set social_image = image.getUrl() %}
  {% else %}
      {% set social_image = fallbackField.first().getUrl() %}
  {% endfor %}
  </code></pre>
  
  <p>That way you can just output <code>social_image</code>.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-07T12:54:20.083Z
Answers.Text: Why loop the images and overwrite the var with each iteration?
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-07T12:58:26.827Z
Answers.Text: Thanks Bryan. I misunderstood another post about `is defined` and `is not null`
Answers.UserDisplayName: Steven Grant
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-07T12:52:12.563Z
Answers.LastActivityDate: 2015-05-07T12:52:12.563Z
Answers.UserDisplayName: Bryan Redeagle
Answers.Body: <p>I'd use the <code>length</code> filter to see if there is an image and then access it using the <code>first()</code> method:</p>
  
  <pre><code>{% if entry.metaImage|length %}
      {% set social_image = entry.metaImage.first().getUrl() %}
  {% else %}
      {% set social_image = '/assets/img/social_image.jpg' %}
  {% endif %}
  </code></pre>
  
  <p>If you prefer ternary syntax it would be like so:</p>
  
  <pre><code>{% set social_image = entry.metaImage|length ? entry.metaImage.first().getUrl() : '/assets/img/social_image.jpg' %}
  </code></pre>
  
  <p>Using it like so you separate logic and output in your template.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-07T12:48:27.107Z
Answers.LastActivityDate: 2015-05-07T12:48:27.107Z
Answers.UserDisplayName: carlcs
Body: <p>My entry has a custom field to set a social image for Facebook.</p>
  
  <p>In the entry template, I'm defining the output like so:</p>
  
  <pre><code>{% set social_image = "" %}
    {% for image in entry.metaImage %}
      {% set social_image = image.getUrl() %}
    {% endfor %}
  </code></pre>
  
  <p>Then in the layout I have this </p>
  
  <pre><code>&lt;meta property="og:image" content="{% if social_image is defined %}{{ social_image }}{% endif %}" /&gt;
  </code></pre>
  
  <p>and this works great. However, when that field hasn't been, I want to fallback to a custom field that will always have an image set.</p>
  
  <p>I tried to change the layout to <code>{% if social_image is not null %}{{ social_image }}{% else %}{{ social_image_fallback }}{% endif %}</code> setting social_image_fallback within the entry template of course, but that outputs a null value.</p>
  
  <p>I originally had <code>{% if social_image is defined %}</code> but of course if defined would always evaluate to true.</p>
  
  <p>I'm missing something simple here right?</p>
  
Tags: <templating><conditional><variable>
Title: Unexpected output from Twig set variable
ThreadText: # Unexpected output from Twig set variable
  
  (Asked by: username_0 on 2015-05-07)
  
  My entry has a custom field to set a social image for Facebook.
  
  In the entry template, I'm defining the output like so:
  
  ```
  {% set social_image = "" %}
    {% for image in entry.metaImage %}
      {% set social_image = image.getUrl() %}
    {% endfor %}
  
  ```
  
  Then in the layout I have this
  
  ```
  <meta property="og:image" content="{% if social_image is defined %}{{ social_image }}{% endif %}" />
  
  ```
  
  and this works great. However, when that field hasn't been, I want to fallback to a custom field that will always have an image set.
  
  I tried to change the layout to `{% if social_image is not null %}{{ social_image }}{% else %}{{ social_image_fallback }}{% endif %}` setting social\_image\_fallback within the entry template of course, but that outputs a null value.
  
  I originally had `{% if social_image is defined %}` but of course if defined would always evaluate to true.
  
  I'm missing something simple here right?
  
  **Tags:** templating, conditional, variable
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_1 on 2015-05-07. Score: 2 | Accepted answer
  
  Try `{{ social_image|default(social_image_fallback) }}`. I've had good results using Twig's default filter over testing variables, even with multiple fallbacks. ([like this](https://craftcms.stackexchange.com/a/8983/157)).
  
  
  ---
  
  ### Answer by username_2 on 2015-05-07. Score: 1
  
  If you use `defined` it'll always be true because you at the minimum are defining an empty string. `not null` won't work because a null is different from a false condition (which an empty string matches).
  
  @username_1 and @username_3 both have good suggestions for getting what you need. My personally suggestion would be:
  
  ```
  {% set social_image = "" %}
  {% for image in entry.metaImage %}
      {% set social_image = image.getUrl() %}
  {% else %}
      {% set social_image = fallbackField.first().getUrl() %}
  {% endfor %}
  
  ```
  
  That way you can just output `social_image`.
  
  > **Comments:**
  > **username_3**: Why loop the images and overwrite the var with each iteration?  
  > **username_0**: Thanks username_2. I misunderstood another post about `is defined` and `is not null`  
  
  ---
  
  ### Answer by username_3 on 2015-05-07. Score: 1
  
  I'd use the `length` filter to see if there is an image and then access it using the `first()` method:
  
  ```
  {% if entry.metaImage|length %}
      {% set social_image = entry.metaImage.first().getUrl() %}
  {% else %}
      {% set social_image = '/assets/img/social_image.jpg' %}
  {% endif %}
  
  ```
  
  If you prefer ternary syntax it would be like so:
  
  ```
  {% set social_image = entry.metaImage|length ? entry.metaImage.first().getUrl() : '/assets/img/social_image.jpg' %}
  
  ```
  
  Using it like so you separate logic and output in your template.
  
  
  ---
  
  