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

CC BY-SA 3.0
2014-12-18T21:51:13.610Z
This is just another reason why I love the Craft CMS community. Thanks so much Marion!
misteroptimist
CC BY-SA 3.0
2014-12-18T20:50:51.663Z
2014-12-18T20:50:51.663Z
Marion Newlevant
<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>

CC BY-SA 3.0
2014-12-18T20:33:27.853Z
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.
misteroptimist
CC BY-SA 3.0
2014-12-18T21:01:55.737Z
Ah, ok. I didn't get that part from your question. Looks like Marion has an answer for you.
Douglas McDonald
CC BY-SA 3.0
2014-12-18T20:07:26.697Z
2014-12-18T21:05:45.380Z
2014-12-18T21:05:45.380Z
Douglas McDonald
<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>

<matrix>
How to setup conditionals within a matrix?
# 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 %}
            
            {% 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 %}
       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 %}
      {{imgTag}}
    {% else %}
      {{imgTag}}
    {% endif %}
  {% endif %}

{% endif %}

> **Comments:**
> **username_0**: This is just another reason why I love the Craft CMS community. Thanks so much Marion!  

---

### 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 %}{% endif %}{% if document %}{% endif %}
    {% endif %}

{% endif %}

Update: Added conditional for if document. Essentially the same answer as Marion'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 Marion has an answer for you.  

---

