<p>You're trying to your <code>float</code> variable using another variable at the moment that isn't defined. I expect you mean to set <code>float</code> to the string value "yes" like so:</p>

<p><code>{% set float = "yes" %}</code></p>

<p>You could also use a conditional around your <code>class</code> declaration so you don't end up with empty classes and it'd also remove the need to define <code>float</code> as an empty value.</p>

<pre><code>&lt;div{% if float is defined %} class="{{ float }}"{% endif %}&gt; more code here &lt;/div&gt;
</code></pre>

CC BY-SA 3.0
2016-07-19T17:40:42.390Z
2016-07-19T17:40:42.390Z
Luke Pearce
<p>I'm building my first Craft site and need to set a variable if a field has an image and then use that variable elsewhere in the template.</p>

<p>The field is part of a matrix and will repeat.</p>

<p>What I have so far is:</p>

<pre><code>{# extra image #}
{% set asset = block.extraImage.first() %}
{# set variable empty so it's avialble outside of conditional #}
{% set float = "" %}

{% if asset %}
    &lt;img src="{{ asset.url }}" alt="" class="img-responsive pull-right" /&gt;

    {% set float = yes %}           
{% endif %}

&lt;div class="{{ float }}"&gt; more code here &lt;/div&gt;
</code></pre>

<p>The above code doesn't give me any template errors. But it also doesn't output the class as expected.</p>

<p>The full template code can be viewed <a href="http://pastie.org/10912043" rel="nofollow">here</a>.</p>

<templating><matrix><conditional><variable><logic>
Set a Variable inside a conditional
# Set a Variable inside a conditional

(Asked by: username_0 on 2016-07-19)

I'm building my first Craft site and need to set a variable if a field has an image and then use that variable elsewhere in the template.
The field is part of a matrix and will repeat.
What I have so far is:
{# extra image #}
{% set asset = block.extraImage.first() %}
{# set variable empty so it's avialble outside of conditional #}
{% set float = "" %}

{% if asset %}
    

    {% set float = yes %}           
{% endif %}

 more code here 

The above code doesn't give me any template errors. But it also doesn't output the class as expected.
The full template code can be viewed here.

**Tags:** templating, matrix, conditional, variable, logic

---

## 1 answers

### Answer by username_1 on 2016-07-19. Score: 3 | Accepted answer

You're trying to your float variable using another variable at the moment that isn't defined. I expect you mean to set float to the string value "yes" like so:
{% set float = "yes" %}
You could also use a conditional around your class declaration so you don't end up with empty classes and it'd also remove the need to define float as an empty value.
 more code here


---

