silex

Is it possible to buffer the output of $app->run from silex?


I was wondering if it is possible to store the output created by silex. What I want to achieve is something like this :

<?php
    require('core.php');
    $out = $app->run();
    echo $app['twig']->render('header.html');
    echo $out;
    echo $app['twig']->render('footer.html');

I'd like to do it this way because some of the navigation is build inside my dynamic controllers and registered as a global into twig. I've tried to use the out buffering ob_start mechanism without succes :

<?php
    require('core.php');
    ob_start();
    $app->run();
    $out = ob_get_contents();
    ob_end_clean();
    echo $app['twig']->render('header.html');
    echo $out;
    echo $app['twig']->render('footer.html');

Any other ideas on this subject ?


Solution

  • Instead of having a split of header/main/footer and using the global variables, you should use an unique template with template inheritence.

    https://twig.symfony.com/doc/3.x/templates.html#template-inheritance