Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2: phpinfo() using a twig template for layout?

Tags:

php

twig

symfony

Twig won't process PHP tags. Hence, it is a challenge to create a phpinfo() page based on a layout (say base.html.twig).

Is it possible to dump the HTML content of phpinfo() into some variable and pass it as body content to the layout? Or, is there a better way to proceed?

like image 359
Jérôme Verstrynge Avatar asked Oct 03 '15 20:10

Jérôme Verstrynge


3 Answers

Just capture the output of phpinfo() with output buffering, and pass it to the template.

ob_start();
phpinfo();
$phpinfo = ob_get_clean();

echo $twig->render('phpinfo.html.twig', array('phpinfo' => $phpinfo));
like image 70
Federkun Avatar answered Nov 16 '22 05:11

Federkun


This is an addition to answer from Federkun. In controller:

ob_start();
phpinfo();
$phpinfo = ob_get_contents();
ob_end_clean();
return $this->render('phpinfo.html.twig', array(
    'phpinfo'=>$phpinfo,
));

Don't forget to put a | raw in twig!

{{ phpinfo | raw }}
like image 40
sneaky Avatar answered Nov 16 '22 07:11

sneaky


class DefaultController extends Controller
{
    /**
     * @Route("/", name="index")
     * @Method("GET")
     */
    public function index()
    {
        ob_start();
        phpinfo();
        $phpinfo = ob_get_clean();

        return new Response(
            '<html><body>'.$phpinfo.'</body></html>'
        );
    }
}
like image 38
es cologne Avatar answered Nov 16 '22 06:11

es cologne