replace Twig Filter

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.

Aptuitiv-Specific Functionality
Aptuitiv's replace filter overrides the Twig replace filter to add support for passing a string value for search and replace.
{{ 'Hi my name is Bob'|replace('Bob', 'Sam') }}
{{ 'I like cheese and crackers'|replace({'cheese': 'icecream', 'crackers': 'chocolate'}) }}

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') }}

This documentation is only for Aptuitiv CMS. Learn more.
Get Started With Aptuitiv