<p>If an attribute is set to <code>true</code>, it will get added without a value.</p>
<pre><code>{{ tag('input', {
    id: idVar,
    name: nameVar,
    required: true
}) }}
</code></pre>
<p>Also, if it is set to <code>null</code> or <code>false</code>, it will be omitted, as if it was never listed in the first place.</p>

CC BY-SA 4.0
2020-08-19T18:37:26.533Z
Great. So the function knows which attribute names are _allowed_ to be boolean. Presumably inherited from knowledge embedded in Yii?
Jonathan Schofield
CC BY-SA 4.0
2020-08-19T18:38:15.143Z
Assuming I have understood correctly, it would be good if this could be noted in the docs.
Jonathan Schofield
CC BY-SA 4.0
2020-10-07T14:00:02.287Z
The [docs](https://craftcms.com/docs/3.x/dev/functions.html#tag) link to [yii\helpers\BaseHtml::renderTagAttributes()](https://www.yiiframework.com/doc/api/2.0/yii-helpers-basehtml#renderTagAttributes()-detail) which has a full description of the different types of attributes/values that are supported.
Brandon Kelly
CC BY-SA 4.0
2020-08-19T17:59:57.863Z
2020-08-19T17:59:57.863Z
Brandon Kelly
<p>When using the <a href="https://craftcms.com/docs/3.x/dev/functions.html#tag" rel="nofollow noreferrer">tag function</a>, how does one handle valueless attributes which could also be null?</p>
<p>For example, it's great we can do…</p>
<pre><code>{% set idVar   = 'idstring' %}
{% set nameVar = 'namestring' %}
{% set typeVar = 'typestring' %}

{{ tag('input', {
    id: idVar,
    name: nameVar,
    type: typeVar
}) }}
</code></pre>
<p>…to generate:</p>
<pre><code>&lt;input id=&quot;idstring&quot; name=&quot;namestring&quot; type=&quot;typestring&quot;&gt;
</code></pre>
<p>But what about if the <code>required</code> attribute is also a variable? We can’t do:</p>
<pre><code>{% set reqVar  = '' %}

{% if field is required %}
    {% set reqVar = 'required' %}
{% endif %}

{{ tag('input', {
    id: idVar,
    name: nameVar,
    required: reqVar
}) }}
</code></pre>
<p>…because if <code>reqVar</code> is empty, one would want the <code>required</code> attribute to not be written at all. To generate:</p>
<pre><code>&lt;input id=&quot;idstring&quot; name=&quot;namestring&quot; type=&quot;typestring&quot;&gt;
</code></pre>
<p>…and <strong>not</strong>:</p>
<pre><code>&lt;input id=&quot;idstring&quot; name=&quot;namestring&quot; type=&quot;typestring&quot; required&gt;
</code></pre>
<p>The same thing applies to other boolean attributes like <code>hidden</code>.</p>
<p>Is there a way to do this with this function? What am I missing or not getting?</p>

<attribute><tag><function>
Setting valueless boolean attributes with the tag function
# Setting valueless boolean attributes with the tag function

(Asked by: username_0 on 2020-08-19)

When using the tag function, how does one handle valueless attributes which could also be null?
For example, it's great we can do…
{% set idVar   = 'idstring' %}
{% set nameVar = 'namestring' %}
{% set typeVar = 'typestring' %}

{{ tag('input', {
    id: idVar,
    name: nameVar,
    type: typeVar
}) }}

…to generate:


But what about if the required attribute is also a variable? We can’t do:
{% set reqVar  = '' %}

{% if field is required %}
    {% set reqVar = 'required' %}
{% endif %}

{{ tag('input', {
    id: idVar,
    name: nameVar,
    required: reqVar
}) }}

…because if reqVar is empty, one would want the required attribute to not be written at all. To generate:


…and not:


The same thing applies to other boolean attributes like hidden.
Is there a way to do this with this function? What am I missing or not getting?

**Tags:** attribute, tag, function

---

## 1 answers

### Answer by username_1 on 2020-08-19. Score: 4

If an attribute is set to true, it will get added without a value.
{{ tag('input', {
    id: idVar,
    name: nameVar,
    required: true
}) }}

Also, if it is set to null or false, it will be omitted, as if it was never listed in the first place.

> **Comments:**
> **username_0**: Great. So the function knows which attribute names are _allowed_ to be boolean. Presumably inherited from knowledge embedded in Yii?  
> **username_0**: Assuming I have understood correctly, it would be good if this could be noted in the docs.  
> **username_1**: The [docs](https://craftcms.com/docs/3.x/dev/functions.html#tag) link to [yii\helpers\BaseHtml::renderTagAttributes()](https://www.yiiframework.com/doc/api/2.0/yii-helpers-basehtml#renderTagAttributes()-detail) which has a full description of the different types of attributes/values that are supported.  

---

