Q:

Using quotes in Twig ternary operators

How does one prevent quotes from being escaped when used in a twig ternary operator?  My desire is <div id="my_id">my div11</div> (or <div id='my_id'>my div11</div>), however, as seen my results are different.
{% set id="my_id" %}
<div{{ id?" id='"~id~"'" }}>my div1</div>
<div{{ id?' id="'~id~'"' }}>my div2</div>
<div{{ id?" id='#{id}'" }}>my div3</div>
<div{{ id?" id=/'#{id}/'" }}>my div4</div>
<div{{ id?' id='~"'"|raw~id~"'"|raw }}>my div5</div>
<div{{ id?' id='~'"'|raw~id~"'"|raw }}>my div6</div>
<div{{ id?" id="~id }}>my div7</div>
<div{{ id?' id='~id }}>my div8</div>
<div{{ id?" id=#{id}" }}>my div9</div>

{% set id="" %}
<div{{ id?" id='"~id~"'" }}>my div1</div>
<div{{ id?' id="'~id~'"' }}>my div2</div>
<div{{ id?" id='#{id}'" }}>my div3</div>
<div{{ id?" id=/'#{id}/'" }}>my div4</div>
<div{{ id?' id='~"'"|raw~id~"'"|raw }}>my div5</div>
<div{{ id?' id='~'"'|raw~id~"'"|raw }}>my div6</div>
<div{{ id?" id="~id }}>my div7</div>
<div{{ id?' id='~id }}>my div8</div>
<div{{ id?" id=#{id}" }}>my div9</div>

Output:
<div id=&#039;my_id&#039;>my div1</div>
<div id=&quot;my_id&quot;>my div2</div>
<div id=&#039;my_id&#039;>my div3</div>
<div id=/&#039;my_id/&#039;>my div4</div>
<div id=&#039;my_id&#039;>my div5</div>
<div id=&quot;my_id&#039;>my div6</div>
<div id=my_id>my div7</div>
<div id=my_id>my div8</div>
<div id=my_id>my div9</div>

<div>my div1</div>
<div>my div2</div>
<div>my div3</div>
<div>my div4</div>
<div>my div5</div>
<div>my div6</div>
<div>my div7</div>
<div>my div8</div>
<div>my div9</div>

A:

try this
  <div{{ (id?' id="'~id~'"')|raw }}>my div1</div>

