简体   繁体   English

如何将PHP DebugBar添加到Twig布局?

[英]How to add PHP DebugBar to Twig layout?

PHP DegugBar has an extension to display which Twig templates were rendered. PHP DegugBar有一个扩展,可以显示渲染了哪些Twig模板。

I found a demo here but if you look at how they do it, they actually render the layout in pure PHP which kind of defeats the point of Twig, which has its own layouting system. 我在这里找到了一个演示,但是如果您看一下他们是如何做的,他们实际上是用纯PHP渲染布局的这有点违反了Twig的观点,Twig拥有自己的布局系统。

The problem is a catch-22: I need to render the debugbar into a Twig variable so that I can put it in the Twig layout, but if I've already rendered the debugbar it won't be able to capture the fact that I'm rendering a Twig template! 问题是一个问题22:我需要将调试栏渲染到Twig变量中,以便可以将其放在Twig布局中,但是如果我已经渲染了调试栏,它将无法捕获我这样的事实。正在渲染Twig模板!

So, I'm not sure how to do this. 因此,我不确定如何执行此操作。 Any ideas? 有任何想法吗?

Create a custom Twig extension to render the code required to render the debug bar. 创建一个自定义的Twig扩展以呈现呈现调试栏所需的代码。 This way, you can do {{ debug_bar() }} for instance to render the debug bar: 这样,您可以执行{{ debug_bar() }}来渲染调试栏:

class DebugBarExtension extends \Twig_Extension
{
    public function getFunctions()
    {
        return [
            new \Twig_SimpleFunction('debug_bar', [$this, 'renderDebugBar']),
        ];
    }

    public function renderDebugBar()
    {
        // ... render and return the debug bar
    }
}

\\DebugBar\\JavascriptRenderer::renderOnShutdownWithHead may be of use. \\DebugBar\\JavascriptRenderer::renderOnShutdownWithHead可能有用。

ie create a Twig function as @Wouter suggests but instead of rendering the debugbar immediately, register it to be rendered later, just before shutdown. 例如,按照@Wouter的建议创建一个Twig函数,但是与其立即渲染调试栏,不如将其注册为稍后渲染,就在关机之前。

I am not sure why do you want to render the DebugBar using Twig, unless you are trying to measure the rendering performance or something like that (which not need for the DebugBar IMO because it should never reach production anyway). 我不确定为什么要使用Twig渲染DebugBar,除非您尝试测量渲染性能或类似的性能(对于DebugBar IMO来说并不需要,因为它永远不会进入生产状态)。

In any case, you don't need a special plugin, artifact , class or code to display the DebugBar in your Twig rendered page. 无论如何,您都不需要特殊的插件, 工件 ,类或代码来在Twig呈现的页面中显示DebugBar。 You can simply do the following: 您可以简单地执行以下操作:

1) Add the result of the DebugBar renders ( Head and Body ) in the Array you are using to send variables to Twig. 1)在用于将变量发送到Twig的Array中添加DebugBar渲染的结果( HeadBody )。 Example: 例:

$template_vars['debugbar_Head'] = $debugbarRenderer->renderHead();
$template_vars['debugbar_Body'] = $debugbarRenderer->render();

2) Add the variables in your template: 2)在模板中添加变量:

<head>
  -- other stuff ---
  {{ debugbar_Head | raw }}
</head>

<body>
  {{ debugbar_Body | raw }}
  -- other stuff ---
</body>

Important : make sure your "debugbar_Head" is at the END of the head tag and the "debugbar_Body" at the beginning of the body tag (for some reason it helps prevent rare rendering errors). 重要提示 :请确保您的“ debugbar_Head”位于head标记的末尾,“ debugbar_Body”位于body标记的开始(出于某种原因,这有助于防止罕见的渲染错误)。

3) Render your template normally: 3)正常渲染您的模板:

echo $view->render('your-template-path', $template_vars);

Enjoy a Twig rendered page with a fully functional DebugBar on the bottom. 享受Twig呈现的页面,其底部具有功能齐全的DebugBar。

Note: there are some stuff around the web promoting some "mix" of Twig and DebugBar. 注意:网络上有一些东西可以促进Twig和DebugBar的“混合”。 Please, before try them, check if they are not framework related. 请尝试使用它们之前,请检查它们是否与框架无关。

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

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