12

I know Twig has a {% spaceless %} filter, but it only removes spaces between html tags.

I need to return a single line of html for every page in the site. I have a regexp that can deal with that, but I don't know where to use it in sf2, since everything happens automagically.

I think I have to register a new templating engine, or add a twig extension, but I couldn't find enough documentation about the subject, so I'm stuck

Any ideas?

HappyDeveloper
  • 12,480
  • 22
  • 82
  • 117

1 Answers1

10

To run your regex on all view output, you can hook into the kernel.response event that is dispatched by the Symfony2 framework.

From the section on kernel.response:

The purpose of this event is to allow other systems to modify or replace the Response object after its creation:

public function onKernelResponse(FilterResponseEvent $event)
{
    $response = $event->getResponse();

    // ... modify the response object
}

I would recommend reading the Internals chapter for even more details.

Stephen Watkins
  • 25,047
  • 15
  • 66
  • 100
  • 1
    Yes I'm sure. I don't think the spaceless filter is ment to do that, so I can't report it as a bug. It only removes spaces between html tags. – HappyDeveloper Jul 12 '11 at 20:33
  • Sometimes it even fails to do that, I'm seeing many newlines in my code, between html tags, so I just want to run my own regexp as I always did, but I don't know how to do it in this framework. – HappyDeveloper Jul 12 '11 at 20:40
  • 1
    The answer was the 'internals' part, thanks. I got my single line html output now =p – HappyDeveloper Jul 13 '11 at 17:16
  • I'd say that spaceless is not a good solution. For example, Dashboard is not the same as Dashboard (note gap after closing i). – Denis V Feb 27 '14 at 00:02
  • Good points by all. I've modified the answer to address the comments. – Stephen Watkins Mar 18 '14 at 16:21