Example code I'm working with:

<a{{ attributes.addClass(classes, 'field__item') }} name="{{ item.content }}">{{ item.content }}</a>

In this example, I have created a Brick with a field called anchor_id. The idea is that content creators can name an anchor whatever they want, and then add that brick to a page.

I can get the field to show up fine. But, I'd like to strip out spaces so if they enter "my awesome tag" it instead gets rendered as name="myawesometag.

I've tried a few methods, but can't seem to figure it out. Seems like it would be some simple filter, but if it is I'm not seeing it.

Thanks in advance!


You can use "replace" - much like you would in PHP - and replace the space with no space

{{ anchor_id|replace({' ': ''})

More info: https://twig.symfony.com/doc/3.x/filters/replace.html

  • 1
    That was just what I needed. Thanks! May 8 '20 at 13:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.