<pre class="lang-TWIG prettyprint-override"><code>{# set test variable, to make your code work #}
{% set block = {imageWidth: 300} %}

{# the real output #}
&lt;img{{ block.imageWidth|length ? (' width="' ~ block.imageWidth~ 'px"')|raw }} /&gt;
</code></pre>

<p>Works for me</p>

CC BY-SA 4.0
2019-03-22T08:40:10.327Z
Ah I was missing the bracket, thank you!
fxfuture
CC BY-SA 4.0
2019-03-22T08:34:55.920Z
2019-03-22T08:34:55.920Z
Robin Schambach
<p>I have the following code:</p>

<pre><code>&lt;img{{ block.imageWidth|length ? ' width="' ~ block.imageWidth ~ 'px"' }} /&gt;
</code></pre>

<p>If imageWidth is specified as 120, it should output:</p>

<pre><code>&lt;img width="120px" /&gt;
</code></pre>

<p>However it is outputting this:</p>

<pre><code>&lt;img width=&amp;quot;120px&amp;quot; /&gt;
</code></pre>

<p>I've tried using |raw but that doesn't work.</p>

<p>Any ideas?</p>

<p>Thanks</p>

<templating><twigextension>
Output raw html from if statement and combining strings?
# Output raw html from if statement and combining strings?

(Asked by: username_0 on 2019-03-22)

I have the following code:


If imageWidth is specified as 120, it should output:


However it is outputting this:


I've tried using |raw but that doesn't work.
Any ideas?
Thanks

**Tags:** templating, twigextension

---

## 1 answers

### Answer by username_1 on 2019-03-22. Score: 2 | Accepted answer

{# set test variable, to make your code work #}
{% set block = {imageWidth: 300} %}

{# the real output #}


Works for me

> **Comments:**
> **username_0**: Ah I was missing the bracket, thank you!  

---

