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?
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));
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 }}
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>'
);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With