How to delete white spaces of a text in twig?
Asked Answered
T

6

35

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:

Toms answered 27/7, 2016 at 8:9 Comment(0)
C
76

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.

Casemaker answered 27/7, 2016 at 8:30 Comment(1)
This worked for me. I was trying {{ 'my string'|replace(' ', '') }} and it would not work. If I replaced the replace with string to something other than nothing, it'd work. Odd.Ramose
S
8

Try this:

{{ "I plays"|replace({' ':''}) }}
Selfdefense answered 27/7, 2016 at 8:12 Comment(7)
@KubiRoazhon: then are you 100% sure these are blank spaces?Hermelindahermeneutic
The replace method function it's a little bit different see [here](twig.sensiolabs.org/doc/filters/replace.html ).Toms
@Hermelindahermeneutic for sure. I've tested it with a simple text => I play :/Toms
try replace({' ':''}).. Not sure though, I've always seen that in the syntax shown by JayeshHermelindahermeneutic
I submitted an edit to this answer in order to avoid misunderstandings by other users.Casemaker
An edit that should've be rejected as it clearly interferes with the posted answerCalvinna
Well, I don't mind some less points of reputation if this prevents having people ranting around because they just copy/pasted wrong stuff :)Casemaker
S
4

You can also create your own filter to do that

Example :

class MyExtensions extends \Twig_Extension
{
    public function getFilters()
    {
        return array(
            new \Twig_SimpleFilter('removeWhiteSpace', array($this, 'removeWhiteSpace'), array('is_safe' => array('html'))),
        );
    }

    public function removeWhiteSpace($string)
    {

       return preg_replace('/\s+/', '', $string);
    }
}

Declare it as service :

myextensions.twig_extension:
      class: YourProject\YourBundle\Twig\MyExtensions
      public: false
      tags:
          - { name: twig.extension }

And call it in yours twig template :

{{ "Test remove white space"|removeWhiteSpace }}
Sartain answered 27/7, 2016 at 8:27 Comment(0)
M
1

For me this was not working when string contains non-breaking whitespaces:

stringWithNonBreakingWhitespace|replace({' ':''}

To replace non-braking whitespace you have to use escape sequence:

stringWithNonBreakingWhitespace|replace({'\xc2\xa0':''}
Misinterpret answered 10/7, 2019 at 21:8 Comment(0)
E
0

Use the spaceless filter to remove whitespace between HTML tags, not whitespace within HTML tags or whitespace in plain text:

{{ "<div>
        <strong>foo</strong>
    </div>"|spaceless }}

You can combine spaceless with the apply tag to apply the transformation on large amounts of HTML:

{% apply spaceless %}
    <div>
        <strong>foo</strong>
    </div>
{% endapply %}
Edisonedit answered 19/7, 2023 at 11:23 Comment(0)
K
0

Ran into a similar need while optimizing my templates. Here's a quick way to reduce HTML whitespace in Twig:

{% if primary -%}
    <div class="visually-hidden">{{ 'Primary tabs'|t }}</div>
    <ul>{{ primary }}</ul>
{%- endif %}
{%- if secondary -%}
    <div class="visually-hidden">{{ 'Secondary tabs'|t }}</div>
    <ul>{{ secondary }}</ul>
{%- endif %}

Adding "-" inside your {%- if -%} and {%- endif -%} tags cuts out the extra spaces.

Hope this helps!

Kesselring answered 15/2, 2024 at 17:17 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.