KubiRoazhon KubiRoazhon - 1 year ago 209
Twig Question

How to delete white spaces of a text in twig?

I'm using the twig template engine while using symfony2. I'm trying to find a way to delete the white spaces from a text.

For example,

I play
shall become

I've tried:

Answer Source

First let's see what you tried and why that wasn't working:

  • Spaceless: is not working because "Use the spaceless tag to remove whitespace between HTML tags, not whitespace within HTML tags or whitespace in plain text" see spaceless documentation.
  • Trim: is not working because "The trim filter strips whitespace (or other characters) from the beginning and end of a string" see trim documentation.

What you need to use is the following:

{{ 'Some Text With Spaces'|replace({' ':''}) }}

This will output:


More details in the documentation.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download