Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8740713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:10:43+00:00 2026-06-13T11:10:43+00:00

How i can wrap every block code with spaceless to crop whitespaces from my

  • 0

How i can wrap every block code with spaceless to crop whitespaces from my twig/html

for example now i have:

{% block content %}

    <div class="box clearfix clearall">
        <div class="ct colcontainer">
            <div class="col-1">
                <div class="chars">
                    <table class="layout data-char">
                        <thead>

blabla

{% endblock %}

And when symfony try to render it, i want that symfony saw

{% block content %}
{% spaceless %}
    <div class="box clearfix clearall">
        <div class="ct colcontainer">
            <div class="col-1">
                <div class="chars">
                    <table class="layout data-char">
                        <thead>

blabla
{% endspaceless %}
{% endblock %}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:10:44+00:00Added an answer on June 13, 2026 at 11:10 am

    Define a custom Twig tag (the copy-and-paste way)

    You can define a custom Twig tag spacelessblock which combines block and spaceless. Then you can use {% spacelessblock xyz %}…{% endspacelessblock %} in your templates. Here is how you do it the quick and dirty (copy and paste) way.

    A new Twig node

    First, define a class Twig_Node_SpacelessBlock (e.g. in the Extension directory of your bundle):

    class Twig_Node_SpacelessBlock extends \Twig_Node_Block
    {
        public function __construct($name, Twig_NodeInterface $body, $lineno, $tag = null)
        {
            parent::__construct(array('body' => $body), array('name' => $name), $lineno, $tag);
        }
    
        public function compile(Twig_Compiler $compiler)
        {
            // top part of Block.compile
            $compiler
                ->addDebugInfo($this)
                ->write(sprintf("public function block_%s(\$context, array \$blocks = array())\n", $this->getAttribute('name')), "{\n")
                ->indent()
            ;
    
            // the content of the body is treated like in Spaceless.compile
            $compiler
                ->write("ob_start();\n")
                ->subcompile($this->getNode('body'))
                ->write("echo trim(preg_replace('/>\s+</', '><', ob_get_clean()));\n")
            ;
    
            // bottom part of Block.compile
            $compiler
                ->outdent()
                ->write("}\n\n")
            ;
        }
    }
    

    A new Twig token parser

    Our new Twig node needs to be built somewhere whenever Twig finds a {% spacelessblock xyz %} in a template. For that, we need a token parser which we call Twig_TokenParser_SpacelessBlock. We basically copy and paste Twig_TokenParser_Block:

    class Twig_TokenParser_SpacelessBlock extends \Twig_TokenParser
    {
        public function parse(Twig_Token $token)
        {
            // …
            $this->parser->setBlock($name, $block = new Twig_Node_SpacelessBlock($name, new Twig_Node(array()), $lineno));
            // …
        }
    
        public function decideBlockEnd(Twig_Token $token)
        {
            return $token->test('endspacelessblock');
        }
    
        public function getTag()
        {
            return 'spacelessblock';
        }
    }
    

    Tell Twig about it

    In your extension class:

    class Extension extends \Twig_Extension
    {
        public function getTokenParsers()
        {
            return array(
                new Twig_TokenParser_SpacelessBlock(),
            );
        }
    }
    

    Tell Symfony about it

    If not already done, add the following to your your services.yml:

    services:
        # …
        my.extension:
            class:     Acme\MyBundle\Extension\Extension
            tags:
              - { name: twig.extension }
    

    Better alternatives

    Preprocessor

    A better way would be to use a preprocessor to simply replace

    {% spacelessblock xyz %}
    …
    {% endspacelessblock %}
    

    by

    {% block xyz %}{% spaceless %}
    …
    {% endspaceless %}{% endblock %}
    

    which reuses all the code that already has been written in the Twig project, including possible changes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an ascx control that can wrap content like a panel.
I need to write a delegate function that can 'wrap' some while/try/catch code around
I would like to have a function that can wrap any other function call.
In virtually every example code of GTK+ I've seen so far, the widgets of
How can I wrap every element belonging to a particular class with a link
I use this code to wrap every 3 divs in a seprate div and
Currently I have multiple resources in a project that can only be accessed from
I have a 12 box in html. i want to make three block and
I have a list of images and I want to wrap every 2 images
Let's say i have two Doctrine entities: Users and Messages Every user can have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.