<p>There's nothing in Craft core that would cause this.</p>
<p>Have you inspected the actual rendered HTML source, or are you looking at it in your browser's dev tools' DOM/elements panel? Typically, browsers will do their best to (auto)correct obvious HTML errors (like an unclosed tag).</p>

CC BY-SA 4.0
2022-10-20T00:24:57.247Z
You nailed it. Had no idea DevTools did that, especially because the source code shows the closing tags. Thanks!
user5965
CC BY-SA 4.0
2022-10-19T21:51:32.757Z
2022-10-19T21:51:32.757Z
Mats Mikkel Rummelhoff
<p>Your question has already been answered, but I think I can guess what you're trying to do and there's a much cleaner way. Try something like this:</p>
<pre><code>{% set innards %}
    &lt;h3&gt;Lots of lengthy markup here&lt;/h3&gt;
    &lt;p&gt;Lorem ipsum dolor&lt;/p&gt;
    &lt;p&gt;etc&lt;/p&gt;
{% endset %}

{% if condition %}
    &lt;a href=&quot;{{ thing.url }}&quot;&gt;{{ innards|raw }}&lt;/a&gt;
{% else %}
    {{ innards }}
{% endif %}
</code></pre>
<p>This is similar to Roman's answer above, but I prefer this way a bit using set...endset as you don't need to worry so much about escaping characters.</p>

CC BY-SA 4.0
2022-10-20T22:05:16.877Z
That's a great solution. Thanks!
user5965
CC BY-SA 4.0
2022-10-20T08:07:06.263Z
2022-10-20T08:07:06.263Z
James Smith
<p>The browser will always try to close unclosed tags, that's the point.</p>
<p>You can change your output logic to like this, but that's really a bad way:</p>
<pre><code>  {% set condition = true %}
  {% set html = '' %}
  {% if condition %}
    {% set html = '&lt;a href=&quot;#&quot;&gt;My link' %}
  {% endif %}
  {# ... #}
  {% if condition %}
    {% set html = html ~ '&lt;/a&gt;' %}
   {% endif %}
  {{ html|raw }}
</code></pre>
<p>You must change the logic so that you do not use such &quot;crutches&quot;</p>

CC BY-SA 4.0
2022-10-19T21:58:41.560Z
2022-10-19T21:58:41.560Z
RomanAvr
<p>When an <code>&lt;a&gt;</code> is used within a conditional, the output of the <code>&lt;a&gt;</code> adds a self-closing a <code>&lt;/a&gt;</code>. Is there a way to prevent this from happening? I didn't realize this occurred.</p>
<pre><code>{% if true %}
    &lt;a href=&quot;#&quot;&gt;
{% endif %}

EXPECTED OUTPUT: &lt;a href=&quot;#&quot;&gt;
ACTUAL OUTPUT: &lt;a href=&quot;#&quot;&gt; &lt;/a&gt;
</code></pre>

<craft3>
Preventing <a> from self closing within conditional
# Preventing <a> from self closing within conditional

(Asked by: username_0 on 2022-10-19)

When an  is used within a conditional, the output of the  adds a self-closing a . Is there a way to prevent this from happening? I didn't realize this occurred.
{% if true %}
    
{% endif %}

EXPECTED OUTPUT: 
ACTUAL OUTPUT:

**Tags:** craft3

---

## 3 answers

### Answer by username_1 on 2022-10-19. Score: 0 | Accepted answer

There's nothing in Craft core that would cause this.

Have you inspected the actual rendered HTML source, or are you looking at it in your browser's dev tools' DOM/elements panel? Typically, browsers will do their best to (auto)correct obvious HTML errors (like an unclosed tag).

> **Comments:**
> **username_0**: You nailed it. Had no idea DevTools did that, especially because the source code shows the closing tags. Thanks!  

---

### Answer by username_2 on 2022-10-20. Score: 0

Your question has already been answered, but I think I can guess what you're trying to do and there's a much cleaner way. Try something like this:
{% set innards %}
    Lots of lengthy markup here
    Lorem ipsum dolor
    etc
{% endset %}

{% if condition %}
    {{ innards|raw }}
{% else %}
    {{ innards }}
{% endif %}

This is similar to Roman's answer above, but I prefer this way a bit using set...endset as you don't need to worry so much about escaping characters.

> **Comments:**
> **username_0**: That's a great solution. Thanks!  

---

### Answer by username_3 on 2022-10-19. Score: 0

The browser will always try to close unclosed tags, that's the point.
You can change your output logic to like this, but that's really a bad way:
  {% set condition = true %}
  {% set html = '' %}
  {% if condition %}
    {% set html = 'My link' %}
  {% endif %}
  {# ... #}
  {% if condition %}
    {% set html = html ~ '' %}
   {% endif %}
  {{ html|raw }}

You must change the logic so that you do not use such "crutches"


---

