Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-08-19T18:37:26.533Z
Answers.Text: Great. So the function knows which attribute names are _allowed_ to be boolean. Presumably inherited from knowledge embedded in Yii?
Answers.UserDisplayName: Jonathan Schofield
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-08-19T18:38:15.143Z
Answers.Text: Assuming I have understood correctly, it would be good if this could be noted in the docs.
Answers.UserDisplayName: Jonathan Schofield
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-10-07T14:00:02.287Z
Answers.Text: 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.
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-08-19T17:59:57.863Z
Answers.LastActivityDate: 2020-08-19T17:59:57.863Z
Answers.UserDisplayName: Brandon Kelly
Body: <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>
  
Tags: <attribute><tag><function>
Title: Setting valueless boolean attributes with the tag function
ThreadText: # Setting valueless boolean attributes with the tag function
  
  (Asked by: username_0 on 2020-08-19)
  
  When using the [tag function](https://craftcms.com/docs/3.x/dev/functions.html#tag), 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:
  
  ```
  <input id="idstring" name="namestring" type="typestring">
  
  ```
  
  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:
  
  ```
  <input id="idstring" name="namestring" type="typestring">
  
  ```
  
  …and **not**:
  
  ```
  <input id="idstring" name="namestring" type="typestring" required>
  
  ```
  
  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.  
  
  ---
  
  