<p>Try:</p>

<pre><code>{{ pages|replace({('data-url="' ~ craft.request.getUrl() ~ '"'): 'class="active"'})|raw }}
</code></pre>

<p>You were double nesting Twig tags <code>{{ {{ }} }}</code>, which is never necessary.  You'll want to use string concatenation instead. Also for dynamic keys in twig you need parentheses.</p>

CC BY-SA 3.0
2014-07-30T07:48:12.063Z
`{{ navigation|replace({'data-url="' ~ craft.request.getUrl() ~ '"': 'class="active"'})|raw }}` gives me "A hash key must be followed by a colon (:)" and `{{ navigation|replace({'data-url="' ~ craft.request.getUrl() ~ "': 'class="active"'})|raw }}` gives me error "Unclosed """". Any ideas?
naboovalley
CC BY-SA 3.0
2014-07-30T10:52:45.317Z
@naboovalley for dynamic keys in twig you need parentheses, so try `{{ navigation|replace({('data-url="' ~ craft.request.getUrl() ~ '"'): 'class="active"'})|raw }}`
Mike Pepper
CC BY-SA 3.0
2014-07-30T13:52:14.390Z
That did it! :)
naboovalley
CC BY-SA 3.0
2014-07-29T21:37:26.680Z
2014-07-30T15:23:01.997Z
2014-07-30T15:23:01.997Z
Brad Bell
<p>I'm rendering some HTML into a variable. On this HTML I want to replace a value using <code>craft.request.getUrl()</code> but the value doesn't seem to get replaced. Any idea what I'm doing wrong?</p>

<pre><code>{% set pages %}
    {% set entries = craft.entries.section('pages') %}
    {% for entry in entries %}
         &lt;a href="{{ entry.url }}" data-url="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt;
    {% endfor %}
{% endset %}

{{ pages|replace({'data-url="{{ craft.request.getUrl() }}"': 'class="active"'})|raw }}
</code></pre>

<templating><filter>
Why isn't my Twig replace filter working with craft.request.getUrl()?
# Why isn't my Twig replace filter working with craft.request.getUrl()?

(Asked by: username_0 on 2014-07-29)

I'm rendering some HTML into a variable. On this HTML I want to replace a value using craft.request.getUrl() but the value doesn't seem to get replaced. Any idea what I'm doing wrong?
{% set pages %}
    {% set entries = craft.entries.section('pages') %}
    {% for entry in entries %}
         {{ entry.title }}
    {% endfor %}
{% endset %}

{{ pages|replace({'data-url="{{ craft.request.getUrl() }}"': 'class="active"'})|raw }}

**Tags:** templating, filter

---

## 1 answers

### Answer by username_1 on 2014-07-29. Score: 6 | Accepted answer

Try:
{{ pages|replace({('data-url="' ~ craft.request.getUrl() ~ '"'): 'class="active"'})|raw }}

You were double nesting Twig tags {{ {{ }} }}, which is never necessary.  You'll want to use string concatenation instead. Also for dynamic keys in twig you need parentheses.

> **Comments:**
> **username_0**: `{{ navigation|replace({'data-url="' ~ craft.request.getUrl() ~ '"': 'class="active"'})|raw }}` gives me "A hash key must be followed by a colon (:)" and `{{ navigation|replace({'data-url="' ~ craft.request.getUrl() ~ "': 'class="active"'})|raw }}` gives me error "Unclosed """". Any ideas?  
> **username_2**: @naboovalley for dynamic keys in twig you need parentheses, so try `{{ navigation|replace({('data-url="' ~ craft.request.getUrl() ~ '"'): 'class="active"'})|raw }}`  
> **username_0**: That did it! :)  

---

