<p>Use the <code>raw</code> filter on the replaced string (output), not <code>block.body</code> (input):</p>

<pre><code>{% set str = block.body %}
{{ str | replace('&lt;p&gt;', '&lt;p class="myClass" data-wow-duration="1s"&gt;') | raw }}
</code></pre>

<p>Also, some friendly advice: I'd like to point out that rewriting HTML in this way is <a href="http://blog.codinghorror.com/parsing-html-the-cthulhu-way/">pretty risky</a>.</p>

<p><em>Shameless plug alert</em>, but I wrote a plugin called <a href="https://github.com/mmikkel/RetconHTML-Craft">Retcon HTML</a> which lets you manipulate your HTML content using PHP's <a href="http://php.net/manual/en/class.domdocument.php">DOMDocument</a> – a far safer approach :)</p>

<p>This is how you could achieve your specific goal using Retcon's <a href="https://github.com/mmikkel/RetconHTML-Craft/wiki/Attr">Attr method</a>:</p>

<pre><code>{{ block.body | retconAttr( 'p', { 'class' : 'myClass', 'data-wow-duration' : '1s' } ) }}
</code></pre>

CC BY-SA 3.0
2015-04-21T01:28:55.503Z
I was wondering if there was something like this. Thank you! Final solution: 
`{{ block.body | retconAttr( 'p', { 'class' : 'myClass anotherClass', 'data-wow-duration' : '1' } ) }}`
Leah
CC BY-SA 3.0
2015-04-21T06:49:10.367Z
No worries, happy it could help you out!
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2015-04-21T01:16:49.510Z
2015-04-21T01:30:29.403Z
2015-04-21T01:30:29.403Z
Mats Mikkel Rummelhoff
<p>Currently I am using twig's replace filter to replace the tags:</p>

<pre><code>{% set str = block.body | raw %}
{{ str | replace('&lt;p&gt;', '&lt;p class="myClass" data-wow-duration="1s"&gt;') }}
</code></pre>

<p>But on output it's not outputting the actual tags but:</p>

<pre><code>&amp;lt;p class=&amp;quot;myClass&amp;quot; data-wow-duration=&amp;quot;1s&amp;quot;&amp;gt;
</code></pre>

<p>How do I keep the tags as tags?</p>

<templating><string><replace>
Replace <p> with <p class="myClass">
# Replace <p> with <p class="myClass">

(Asked by: username_0 on 2015-04-21)

Currently I am using twig's replace filter to replace the tags:
{% set str = block.body | raw %}
{{ str | replace('', '') }}

But on output it's not outputting the actual tags but:
<p class="myClass" data-wow-duration="1s">

How do I keep the tags as tags?

**Tags:** templating, string, replace

---

## 1 answers

### Answer by username_1 on 2015-04-21. Score: 7 | Accepted answer

Use the raw filter on the replaced string (output), not block.body (input):
{% set str = block.body %}
{{ str | replace('', '') | raw }}

Also, some friendly advice: I'd like to point out that rewriting HTML in this way is pretty risky.
Shameless plug alert, but I wrote a plugin called Retcon HTML which lets you manipulate your HTML content using PHP's DOMDocument – a far safer approach :)
This is how you could achieve your specific goal using Retcon's Attr method:
{{ block.body | retconAttr( 'p', { 'class' : 'myClass', 'data-wow-duration' : '1s' } ) }}

> **Comments:**
> **username_0**: I was wondering if there was something like this. Thank you! Final solution:  `{{ block.body | retconAttr( 'p', { 'class' : 'myClass anotherClass', 'data-wow-duration' : '1' } ) }}`  
> **username_1**: No worries, happy it could help you out!  

---

