Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-30T07:48:12.063Z
Answers.Text: `{{ 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?
Answers.UserDisplayName: naboovalley
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-30T10:52:45.317Z
Answers.Text: @naboovalley for dynamic keys in twig you need parentheses, so try `{{ navigation|replace({('data-url="' ~ craft.request.getUrl() ~ '"'): 'class="active"'})|raw }}`
Answers.UserDisplayName: Mike Pepper
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-30T13:52:14.390Z
Answers.Text: That did it! :)
Answers.UserDisplayName: naboovalley
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-29T21:37:26.680Z
Answers.LastActivityDate: 2014-07-30T15:23:01.997Z
Answers.LastEditDate: 2014-07-30T15:23:01.997Z
Answers.UserDisplayName: Brad Bell
Body: <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>
  
Tags: <templating><filter>
Title: Why isn't my Twig replace filter working with craft.request.getUrl()?
ThreadText: # 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 %}
           <a href="{{ entry.url }}" data-url="{{ entry.url }}">{{ entry.title }}</a>
      {% 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**: @username_0 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! :)  
  
  ---
  
  