replace

The replace filter replaces parts of a string with other values.

You can replace one thing at a time by passing the separate search and replace values.

{{ 'Hi my name is Bob'|replace('Bob', 'Sam') }}

Alternately you could pass a hash containing the search/replace pairs.

{{ 'Hi my name is Bob'|replace({'Bob': 'Sam'}) }}

You can also replace multiple values at once by using a hash of search/replace pairs.

{{ 'I like %this% and %that%.'|replace({'%this%': 'apples', '%that%': 'bananas'}) }}

{{ 'I like cheese and crackers'|replace({'cheese': 'icecream', 'crackers': 'chocolate'}) }

Arguments

The replace filter has the following signature.

replace(search, replace)

Argument Description
search

Either a string value to search for or a hash of search/replace pairs. If a hash is used then the replace argument is not used or necessary.

Single string value with the replace argument.

{{ 'Hi my name is Bob'|replace('Bob', 'Sam') }}

Passing a hash of search/replace pairs.

{{ 'I like cheese and crackers'|replace({'cheese': 'icecream', 'crackers': 'chocolate'}) }}

 

replace

The value to replace the search argument value with. This is only used if the search value is a string.

{{ 'Hi my name is Bob'|replace('Bob', 'Sam') }}

< Back to the list of filters