简体   繁体   English

Symfony2:phpinfo() 使用树枝模板进行布局?

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

Twig won't process PHP tags. Twig 不会处理 PHP 标签。 Hence, it is a challenge to create a phpinfo() page based on a layout (say base.html.twig ).因此,基于布局(比如base.html.twig )创建一个phpinfo()页面是一个挑战。

Is it possible to dump the HTML content of phpinfo() into some variable and pass it as body content to the layout?是否可以将phpinfo()的 HTML 内容转储到某个变量中并将其作为正文内容传递给布局? Or, is there a better way to proceed?或者,有没有更好的方法来进行?

Just capture the output of phpinfo() with output buffering, and pass it to the template.只需使用输出缓冲捕获phpinfo()的输出,并将其传递给模板。

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

echo $twig->render('phpinfo.html.twig', array('phpinfo' => $phpinfo));

This is an addition to answer from Federkun.这是对 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>'
        );
    }
}

With Symfony, if you just need to check phpinfo() 's output, you can use dump(phpinfo()) inside a controller and you'll get the basic phpinfo() 's output.使用Symfony,如果您只需要检查phpinfo()的输出,您可以在控制器中使用dump(phpinfo()) ,您将获得基本的phpinfo()输出。 Also working to call phpinfo() as parameter of the Response object then return it:还可以调用phpinfo()作为Response对象的参数,然后返回它:

class DefaultController extends AbstractController
{
   /**
    * @Route("/", name="default")
    */
   public function index()
   {
       dump(phpinfo());

       // Then return something else, or call directly phpinfo() on return, like bellow
       return new Response(phpinfo());
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM