Answers.Body: <p>If you have an <code>image</code>, you want to display it, and if there is also a <code>linkedDocument</code>, you want the <code>image</code> to link to that document. This should handle both cases, without too much code duplication.</p>
  
  <pre><code>{% if block.type == "image" %}
  
    {% set image = block.image.first %}
  
    {% if image %}
      {# set the imgTag, which we will display regardless #}
      {% set imgTag %}
        &lt;img src="{{ image.getUrl('thumb') }}" width="{{ image.getWidth('thumb') }}"  height="{{ image.getHeight('thumb') }}" alt="{{ image.title }}"&gt;&lt;/a&gt; alt="{{ image.title }}"&gt;
      {% endset %}
  
      {% set linkedDocument = block.linkedDocument.first %}
  
      {# we may or may not have a linkedDocument. Display the image in either case #}
      {% if linkedDocument %}
        &lt;a href="{{ linkedDocument.url }}"&gt;{{imgTag}}&lt;/a&gt;
      {% else %}
        {{imgTag}}
      {% endif %}
    {% endif %}
  
  {% endif %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-18T21:51:13.610Z
Answers.Text: This is just another reason why I love the Craft CMS community. Thanks so much Marion!
Answers.UserDisplayName: misteroptimist
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-18T20:50:51.663Z
Answers.LastActivityDate: 2014-12-18T20:50:51.663Z
Answers.UserDisplayName: Marion Newlevant
Answers.Body: <p>Very close. It just looks like you are trying to reference the 'linkedDocument' through 'image' instead of through the block, and you are not retrieving the url.</p>
  
  <pre><code>{% if block.type == "image" %}
  
      {% set image = block.image.first %}
      {% set document = block.linkedDocument.first %}
  
      {% if image %}
          {% if document %}&lt;a href="{{ document.url }}"&gt;{% endif %}&lt;img src="{{ image.getUrl('thumb') }}" width="{{ image.getWidth('thumb') }}" height="{{ image.getHeight('thumb') }}" alt="{{ image.title }}"&gt;{% if document %}&lt;/a&gt;{% endif %}
      {% endif %}
  
  {% endif %}
  </code></pre>
  
  <p>Update: Added conditional for <code>if document</code>. Essentially the same answer as Marion's.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-18T20:33:27.853Z
Answers.Text: How do I handle the case if there is no linked document? I want to be able to put in images without linked doucments and images with linked documents.
Answers.UserDisplayName: misteroptimist
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-18T21:01:55.737Z
Answers.Text: Ah, ok. I didn't get that part from your question. Looks like Marion has an answer for you.
Answers.UserDisplayName: Douglas McDonald
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-18T20:07:26.697Z
Answers.LastActivityDate: 2014-12-18T21:05:45.380Z
Answers.LastEditDate: 2014-12-18T21:05:45.380Z
Answers.UserDisplayName: Douglas McDonald
Body: <p>In my matrix field, I want to be able to upload my image, and then upload another asset (in this case it would be a document) so that that image I upload links to that document.</p>
  
  <p>I have my Matrix field setup like so:</p>
  
  <p>Image ->
    -Image
    -linkedDocument</p>
  
  <p>How can I set up a conditional where only add in the  tag <em>if</em> I add an asset there?</p>
  
  <p>Here is my relevant code:</p>
  
  <pre><code>% elseif block.type == "image" %}
  
          {% set image = block.image.first() %}
  
          {% if image %}
              &lt;a href="{{ image.linkedDocument }}"&gt;&lt;img src="{{ image.getUrl('thumb') }}" width="{{ image.getWidth('thumb') }}" height="{{ image.getHeight('thumb') }}" alt="{{ image.title }}"&gt;&lt;/a&gt;
              {% endif %}
          {% endif %}
  </code></pre>
  
Tags: <matrix>
Title: How to setup conditionals within a matrix?
ThreadText: # How to setup conditionals within a matrix?
  
  (Asked by: username_0 on 2014-12-18)
  
  In my matrix field, I want to be able to upload my image, and then upload another asset (in this case it would be a document) so that that image I upload links to that document.
  
  I have my Matrix field setup like so:
  
  Image ->
  -Image
  -linkedDocument
  
  How can I set up a conditional where only add in the tag *if* I add an asset there?
  
  Here is my relevant code:
  
  ```
  % elseif block.type == "image" %}
  
          {% set image = block.image.first() %}
  
          {% if image %}
              <a href="{{ image.linkedDocument }}"><img src="{{ image.getUrl('thumb') }}" width="{{ image.getWidth('thumb') }}" height="{{ image.getHeight('thumb') }}" alt="{{ image.title }}"></a>
              {% endif %}
          {% endif %}
  
  ```
  
  **Tags:** matrix
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2014-12-18. Score: 3 | Accepted answer
  
  If you have an `image`, you want to display it, and if there is also a `linkedDocument`, you want the `image` to link to that document. This should handle both cases, without too much code duplication.
  
  ```
  {% if block.type == "image" %}
  
    {% set image = block.image.first %}
  
    {% if image %}
      {# set the imgTag, which we will display regardless #}
      {% set imgTag %}
        <img src="{{ image.getUrl('thumb') }}" width="{{ image.getWidth('thumb') }}"  height="{{ image.getHeight('thumb') }}" alt="{{ image.title }}"></a> alt="{{ image.title }}">
      {% endset %}
  
      {% set linkedDocument = block.linkedDocument.first %}
  
      {# we may or may not have a linkedDocument. Display the image in either case #}
      {% if linkedDocument %}
        <a href="{{ linkedDocument.url }}">{{imgTag}}</a>
      {% else %}
        {{imgTag}}
      {% endif %}
    {% endif %}
  
  {% endif %}
  
  ```
  
  > **Comments:**
  > **username_0**: This is just another reason why I love the Craft CMS community. Thanks so much username_1!  
  
  ---
  
  ### Answer by username_2 on 2014-12-18. Score: 2
  
  Very close. It just looks like you are trying to reference the 'linkedDocument' through 'image' instead of through the block, and you are not retrieving the url.
  
  ```
  {% if block.type == "image" %}
  
      {% set image = block.image.first %}
      {% set document = block.linkedDocument.first %}
  
      {% if image %}
          {% if document %}<a href="{{ document.url }}">{% endif %}<img src="{{ image.getUrl('thumb') }}" width="{{ image.getWidth('thumb') }}" height="{{ image.getHeight('thumb') }}" alt="{{ image.title }}">{% if document %}</a>{% endif %}
      {% endif %}
  
  {% endif %}
  
  ```
  
  Update: Added conditional for `if document`. Essentially the same answer as username_1's.
  
  > **Comments:**
  > **username_0**: How do I handle the case if there is no linked document? I want to be able to put in images without linked doucments and images with linked documents.  
  > **username_2**: Ah, ok. I didn't get that part from your question. Looks like username_1 has an answer for you.  
  
  ---
  
  