Answers.Body: <p>You can use the <a href="http://twig.sensiolabs.org/doc/filters/length.html"><code>length</code></a> Twig filter for that.</p>
  
  <pre><code>{% if entry.featureImage | length %}
      {% set image = entry.featureImage.first() %}
      &lt;div class="imagewrapper"&gt;
         &lt;img src="{{ image.getUrl() }}" width="{{ image.getWidth('banner') }}" height="{{ image.getHeight('banner') }}"&gt;
      &lt;/div&gt;
  {% endif %}
  </code></pre>
  
  <p>If there isn't an image in <code>featureImage</code> then nothing will be output.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-25T02:45:27.170Z
Answers.LastActivityDate: 2014-06-25T02:45:27.170Z
Answers.UserDisplayName: Alex Kendrick
Body: <p>I know how to add images into my template:</p>
  
  <pre><code>{% set image = entry.featureImage.first() %}
  &lt;div class="imagewrapper"&gt;
      &lt;img src="{{ image.getUrl() }}" width="{{ image.getWidth('banner') }}" height="{{ image.getHeight('banner') }}"&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>But what if my client doesn't have feature images for all of his entries? I need some function that'll tell the website, if there is no image, omit <code>&lt;div class="imagewrapper"&gt;</code>.</p>
  
  <p>Perhaps some kind of if/else statement that I can use:</p>
  
  <pre><code>{% if
      //the featureimage for this entry is filled in
  %}
      {% set image = entry.featureImage.first() %}
      &lt;div class="imagewrapper"&gt;
          &lt;img src="{{ image.getUrl() }}" width="{{ image.getWidth('banner') }}" height="{{ image.getHeight('banner') }}"&gt;
      &lt;/div&gt;
  {% endif %}
  </code></pre>
  
Tags: <templating><entries>
Title: How do I use an if/else statement to determine if a field is filled in?
ThreadText: # How do I use an if/else statement to determine if a field is filled in?
  
  (Asked by: username_0 on 2014-06-25)
  
  I know how to add images into my template:
  
  ```
  {% set image = entry.featureImage.first() %}
  <div class="imagewrapper">
      <img src="{{ image.getUrl() }}" width="{{ image.getWidth('banner') }}" height="{{ image.getHeight('banner') }}">
  </div>
  
  ```
  
  But what if my client doesn't have feature images for all of his entries? I need some function that'll tell the website, if there is no image, omit `<div class="imagewrapper">`.
  
  Perhaps some kind of if/else statement that I can use:
  
  ```
  {% if
      //the featureimage for this entry is filled in
  %}
      {% set image = entry.featureImage.first() %}
      <div class="imagewrapper">
          <img src="{{ image.getUrl() }}" width="{{ image.getWidth('banner') }}" height="{{ image.getHeight('banner') }}">
      </div>
  {% endif %}
  
  ```
  
  **Tags:** templating, entries
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2014-06-25. Score: 9 | Accepted answer
  
  You can use the [`length`](http://twig.sensiolabs.org/doc/filters/length.html) Twig filter for that.
  
  ```
  {% if entry.featureImage | length %}
      {% set image = entry.featureImage.first() %}
      <div class="imagewrapper">
         <img src="{{ image.getUrl() }}" width="{{ image.getWidth('banner') }}" height="{{ image.getHeight('banner') }}">
      </div>
  {% endif %}
  
  ```
  
  If there isn't an image in `featureImage` then nothing will be output.
  
  
  ---
  
  