Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete white spaces of a text in twig?

Tags:

php

twig

symfony

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 Iplay.

I've tried:

  • Spaceless
  • Trim
like image 764
KubiRoazhon Avatar asked Jul 27 '16 08:07

KubiRoazhon


2 Answers

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:

SomeTextWithSpaces 

More details in the documentation.

like image 193
dlondero Avatar answered Sep 22 '22 18:09

dlondero


Try this:

{{ "I plays"|replace({' ':''}) }} 
like image 32
Jayesh Chitroda Avatar answered Sep 23 '22 18:09

Jayesh Chitroda