<p>You'd have to store the complete array into the variable</p>

<pre><code>{% set replaceHeadline = {'&lt;h1&gt;': '&lt;p&gt;', '&lt;/h1&gt;': '&lt;/p&gt;'} %}
{{ entry.richText|replace(replaceHeadline)|raw }}
</code></pre>

<p>But instead of storing that variable I'd probably just create a macro for that snippet.</p>

<p>_includes/macros.html</p>

<pre><code>{% macro formatRichText(string) %}
    {{- string|replace({
        '&lt;h1&gt;': '&lt;p&gt;',
        '&lt;/h1&gt;': '&lt;/p&gt;'
    })|raw }}
{% endmacro %}
</code></pre>

<p>_layout.html</p>

<pre><code>{% import '_includes/macros' as macros %}
</code></pre>

<p>You can now use that helper macro everywhere in your templates</p>

<pre><code>{{ macros.formatRichText(entry.richText) }}
</code></pre>

<p>Another recommendation, have a look at the <a href="https://github.com/mmikkel/RetconHTML-Craft/wiki/Change" rel="nofollow">RetconHTML</a> plugin, a nice little plugin to manipulate HTML with Twig.</p>

<pre><code>{{ string|retconChange('h1', 'p') }}
</code></pre>

CC BY-SA 3.0
2016-03-29T15:43:19.857Z
2016-03-29T15:43:19.857Z
carlcs
<p>I want to strip every headline tag from all Rich Text fields site-wide.</p>

<p>So I thought about adding a replace filter.</p>

<p>But since this will be site wide I wanted to write the replace rules in a variable:</p>

<pre><code>{% set replaceHeadline = '"&lt;h1&gt;" : "&lt;p&gt;", "&lt;/h1&gt;" : "&lt;/p&gt;"' %}
{{ entry.richText | replace({replaceHeadline}) | raw }}
</code></pre>

<p>This produces the error:</p>

<blockquote>
  <p>A hash key must be followed by a colon (:). Unexpected token
  "punctuation" of value "}" ("punctuation" expected with value ":").</p>
</blockquote>

<p>Is this not possible? Is concatenation with <code>~</code> somehow the way to go?</p>

<p>Is there a more sane approach to stripping all headline tags from Rich Text fields?</p>

<richtext><variable><replace>
Filter rules as a variables
# Filter rules as a variables

(Asked by: username_0 on 2016-03-29)

I want to strip every headline tag from all Rich Text fields site-wide.
So I thought about adding a replace filter.
But since this will be site wide I wanted to write the replace rules in a variable:
{% set replaceHeadline = '"" : "", "" : ""' %}
{{ entry.richText | replace({replaceHeadline}) | raw }}

This produces the error:

A hash key must be followed by a colon (:). Unexpected token
  "punctuation" of value "}" ("punctuation" expected with value ":").

Is this not possible? Is concatenation with ~ somehow the way to go?
Is there a more sane approach to stripping all headline tags from Rich Text fields?

**Tags:** richtext, variable, replace

---

## 1 answers

### Answer by username_1 on 2016-03-29. Score: 2 | Accepted answer

You'd have to store the complete array into the variable
{% set replaceHeadline = {'': '', '': ''} %}
{{ entry.richText|replace(replaceHeadline)|raw }}

But instead of storing that variable I'd probably just create a macro for that snippet.
_includes/macros.html
{% macro formatRichText(string) %}
    {{- string|replace({
        '': '',
        '': ''
    })|raw }}
{% endmacro %}

_layout.html
{% import '_includes/macros' as macros %}

You can now use that helper macro everywhere in your templates
{{ macros.formatRichText(entry.richText) }}

Another recommendation, have a look at the RetconHTML plugin, a nice little plugin to manipulate HTML with Twig.
{{ string|retconChange('h1', 'p') }}


---

