本文介绍了为什么 `dump` 函数在 twig 文件中不存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知道为什么会出现这个错误

I don't know why it shows this error

The function "dump" does not exist in twig file

虽然我已经写在 config.yml 文件中:

while I have already writen in config.yml file:

services:
product_store.twig.extension.debug:
    class: Twig_Extension_Debug
    tags:
         - { name: 'twig.extension' }

在树枝文件中我尝试转储:

and in twig file I try to dump with:

{{ dump(product) }}

推荐答案

来自 lifo 的回答鼓励你使用 debug 标签,但 debug 标签 {% debug product %} 在 Twig 1.5 中已弃用并被替换使用 dump 功能 {{ dump(产品)}}.

The answer from lifo encourages you to use the debug tag, but the debug tag {% debug product %} was deprecated in Twig 1.5 and replaced with the dump function {{ dump(product) }}.

从 Symfony 标准版 2.0.9 开始启用的正确扩展是 Twig_Extension_Debug 并且应该添加到 app/config/config_dev.yml 以便它只加载到开发环境:

The proper extension to enable as of Symfony Standard Edition 2.0.9 is Twig_Extension_Debug and should be added to app/config/config_dev.yml so it is only loaded in the dev environment:

services:
    twig.extension.debug:
        class: Twig_Extension_Debug
        tags: [{ name: 'twig.extension' }]

然后您应该可以在模板中使用 {{ dump(product) }}.

You should then be able to use {{ dump(product) }} in your templates.

如果问题仍然存在,您可以尝试一些方法:

If a problem still exists, you can try a few things:

  1. 使用 php app/console container:debug twig.extension.debug --env=dev 确保依赖注入容器正确获取您的服务定义.

  1. Use php app/console container:debug twig.extension.debug --env=dev to ensure the dependency injection container is properly picking up your service definition.

[container] Information for service twig.extension.debug

Service Id       twig.extension.debug
Class            Twig_Extension_Debug
Tags
    - twig.extension                 ()
Scope            container
Public           yes
Synthetic        no
Required File    -

  • 为您的开发环境打开已编译的依赖注入容器类并搜索 Twig_Extension_Debug 以查看是否已定义任何其他服务来使用它.该文件位于 app/cache/dev/appDevDebugProjectContainer.php

  • Open the compiled dependency injection container class for your dev environment and search for Twig_Extension_Debug to see if any other service was already defined to use it. This file lives at app/cache/dev/appDevDebugProjectContainer.php

    确保参数%kernel.debug%为真.

    确保您使用的是 Twig 1.5 或更高版本.

    Ensure you're using Twig 1.5 or later.

    这篇关于为什么 `dump` 函数在 twig 文件中不存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 09-03 10:18