webfactory/legacy-integration-bundle — pull request #5 by mpdude (2014-11-06 15:44:42+00:00)
Title: "Embed" Symfony2 template results into legacy response

I would like to run the legacy app to create some kind of template frame. Basically, it would create and return all the necessary HTML and place some markup triggers (like special comments, a particular string or anything the like) in it.

```
...

...

... other content ...

...
```

Then, the Symfony2 controller would be run and render a template (Twig) as usual.

This Twig template should be able to render HTML as usual and then "embed" rendered snippets in lieu of the placeholders. For example:

```twig
{{ webfactory_legacy_integration_embed('', block('some_block')) }}
{{ webfactory_legacy_integration_embed('', block('another_block')) }}
{{ webfactory_legacy_integration_embed_result() }}
```

This could be placed in a base template so other templates can `@extend` it. They could then create the appropriate blocks and the output would be merged into the "frame" provided by the legacy app.

Accessing the result through a second function is ugly and there might be a better way. But keep in mind that we need to control the entire result returned from the templating engine - so this solution keeps everything in Twig and avoids a "merge" phase on a later kernel event.

Why is that useful?

Being able to access parts of the legacy response with XPath is helpful if you control the overall output/HTML in Twig and just want to insert legacy snippets here and there.

But if you're not that far yet and have to take most of the HTML from legacy, XPath is not really suitable because you will need a lot of complicated expressions to get the markup from above, between and after the snippets you would like to embed.

The approach here also avoids parsing the legacy XHTML completely and can work with simple string replacements.