在 Symfony 中启用 Twig Debug dump() 的分步指南 [英] Step by Step Guide to Enabling Twig Debug dump() in Symfony

查看:21
本文介绍了在 Symfony 中启用 Twig Debug dump() 的分步指南的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最大的目标是打印出twig表单模板/views/Form/fields.html.twig中可用的变量,以便我可以找出哪些变量可用,特别是在{% block widget_attributes %} 基于字段类型(应该存在但由于某种原因无法访问,并且 其他建议来获取类型.

The big goal is to just print out the variables that are available in the twig form template /views/Form/fields.html.twig so that I can find out WHICH variables are available, AND in particular put a condition in the {% block widget_attributes %} based on the field type (which supposedly exists but for some reason is not accessible, and other suggestions to get the type are warned against).

我只想查看所有可用的变量...以及它们保存的值.很简单吧?

I just want to see all the variables that are available... and the values they hold. Easy, right?

所以这让我陷入了很多麻烦,一些有用的文章指出了如何遍历当前上下文的变量:

So that led me down a lot of rabbit holes, and some helpful articles point out how you can loop through the variables of the current context:

{% block widget_attributes %}
    <ol>
        {% for key, value in _context %}
            <li>{{ key }} :
                {% if value is not iterable%}
                    {{ value }}
                {% else %}
                    {{ dump(value) }}
                {% endif %}
            </li>
        {% endfor %}
    </ol>
    {% set attr = attr|merge({'class': (attr.class|default('') ~ ' form-control')|trim}) %}

    {{ parent() }}
{% endblock widget_attributes %}

但这不会打印出type,并且如果它不可迭代,它实际上不会转储该值.它可以毫无错误地杀死 symfony.因此,出于多种原因,让调试工作至关重要.

But this doesn't print out type, and it doesn't actually dump the value if it's not iterable. It kills symfony without any error. So getting debug to work is essential for many reasons.

所有启用转储的建议都不起作用.Twig 的网站特别没有帮助,因为它没有提供加载方式或位置的上下文$twig = new Twig_Environment(最新版本在 twig 上是 1.5,而在 symfony 是 1.16?).Symfony 表示它将默认启用.但它不起作用.

All the suggestions to enable dump, don't work. Twig's website is particularly unhelpful, since it provides no context how or where to load the $twig = new Twig_Environment (and what is up with the latest version being 1.5 at twig but 1.16 in symfony?). Symfony says it will be enabled by default. But it doesn't work.

app.php(加载内核已启用调试):

The app.php (to load the kernel has debug enabled):

$kernel = new AppKernel('dev', true);

这是我的 config.yml 中的内容:

This is what is in my config.yml:

twig:
    debug:            "%kernel.debug%"
    strict_variables: "%kernel.debug%"

以及在 config_dev.yml 中启用的其他建议要么工作:

And the other suggestions for enabling in the config_dev.yml don't work either:

imports:
    - { resource: config.yml }

# this is from one of the suggestions, but it doesn't work and may be an older method
services:
    twig.extension.debug:
        class: Twig_Extension_Debug
        tags: [{ name: 'twig.extension' }]

仍然迷路

与 Symfony 中的许多东西一样,它既强大又令人敬畏,直到它无法工作,然后没有关于如何使其工作的文档.任何帮助将不胜感激.

Still Lost

So as with so many thing in Symfony, it's powerful and awesome, until it doesn't work, and then there is no documentation on how to make it work. Any help would be appreciated.

我正在运行 Symfony 2.5,它的作曲家更新到了 Twig 1.16.

I'm running Symfony 2.5, which composer updates to Twig 1.16.

推荐答案

我在其他帖子中读到的所有建议似乎都适用于旧版本的 Symfony,但它们对我不起作用.但是 现在在 Symfony 中默认启用 Twig 调试.所以这就是我为解决我的问题所做的:

All the suggestions I read in other posts seemed to be for an older version of Symfony and they did not work for me. But Twig debugging is enabled by default in Symfony now. So this is what I did to solve my problem:

1.升级到 Symfony 2.5. 编辑/composer.json 文件并更新 symfony 版本.

1. Upgrade to Symfony 2.5. Edit the /composer.json file and update the symfony version.

2.更新所需的依赖项. 在命令行上运行 composer update

3.更新 Twig. 这也会自动将 twig 更新到 1.16(Symfony 需要最低版本,所以如果您的项目需要 Twig 的最新版本 1.5,您需要在我们自己的 composer.json 文件中要求)).

3. Update Twig. This also automatically updated twig to 1.16 (Symfony requires a minimum version, so if your project requires Twig's latest version of 1.5 you need to require that in our own composer.json file).

4.在调试模式下加载内核. 确保内核加载时调试模式在开发模式下打开,默认情况下,这将在你的 app_dev.php 文件中(加载的索引文件以访问你的开发模式).

4. Load Kernel with Debug On. Make sure the kernel loads with debug turned on in Dev mode, by default this would be in your app_dev.php file (the index file loaded to access your dev mode).

$kernel = new AppKernel('dev', true);

5.检查配置.确保基于内核调试模式启用了twig调试,编辑config.yml:

5. Check Config. Make sure twig debug is enabled based on kernel debug mode, edit the config.yml:

twig:
    debug:            "%kernel.debug%"
    strict_variables: "%kernel.debug%"

6.检查开发配置. 确保您的 config_dev.yml 导入了 config.yml(或至少具有上面的相关配置).

6. Check Dev Config. Make sure your config_dev.yml imports config.yml (or at least has the relevant config from above).

imports:
    - { resource: config.yml }

这样做之后,转储功能现在可以在 Twig 中工作了:

After doing that, the dump function now works in Twig:

{% block widget_attributes %}
    {{ dump(attr) }}
    {% set attr = attr|merge({'class': (attr.class|default('') ~ ' form-control')|trim}) %}
    {{ parent() }}
{% endblock widget_attributes %}

这篇关于在 Symfony 中启用 Twig Debug dump() 的分步指南的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆