在 Symfony 中激活 StringLoader Twig 扩展 [英] Activate StringLoader Twig Extension in Symfony

查看:20
本文介绍了在 Symfony 中激活 StringLoader Twig 扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Symfony 中激活 Twig StringLoader Extension2.3 项目,但就是无法正确使用 yaml 语法.

I'm trying to active the Twig StringLoader Extension in a Symfony 2.3 project but just can't get the yaml-syntax right.

这篇文章引用了 Heyflynn 的答案 在处理完全相同的问题但提供的解决方案(对我来说)不起作用的帖子上.

This post refers to the answer by Heyflynn on a post dealing with the exact same problem but providing a solution that just does not work (for me).

在我的 app/config/config.yml 中写入以下几行只会给我以下异常:

Writing the following lines in my app/config/config.yml just gives me the exception below:

# app/config/config.yml
acme.twig.extension.loader:
class:        Twig_Extension_StringLoader
tags:
     - { name: 'twig.extension' }

给我这个:

FileLoaderLoadException:无法从.../app/config/config_dev.yml"导入资源.../app/config/config.yml".(没有扩展能够加载acme.twig.extension.loader"的配置(在.../app/config/config.yml).寻找命名空间acme.twig.extension.loader",找到框架"、安全"、树枝"、独白"、swiftmailer"、assetic"、学说"、sensio_framework_extra"、apy_data_grid"、project"、acme_demo"、"web_profiler", "sensio_distribution")

FileLoaderLoadException: Cannot import resource ".../app/config/config.yml" from ".../app/config/config_dev.yml". (There is no extension able to load the configuration for "acme.twig.extension.loader" (in .../app/config/config.yml). Looked for namespace "acme.twig.extension.loader", found "framework", "security", "twig", "monolog", "swiftmailer", "assetic", "doctrine", "sensio_framework_extra", "apy_data_grid", "project", "acme_demo", "web_profiler", "sensio_distribution")

(我已经在想 acmeacme.twig.extension.loader 中的出现并用项目包的名称替换它,但这给了我相同的再次例外.只是切断它也不起作用.)

(I was already wondering about the acme appearence in acme.twig.extension.loader and replaced it with the name of the project bundle but that gives me the same exception again. Just cutting it off doesnt work either.)

请帮忙!

推荐答案

Twig 不是 acme bundle 的一部分,Twig 是 vendor bundle 本身,因此错误是正确的.没有 acme.twig.extension.loader

The Twig is not part of acme bundle, Twig is vendor bundle itself and therefore the Error is correct. There is not such namespace as acme.twig.extension.loader

固定代码为:

# services.yml
services:
    twig.extension.stringloader:
        class: Twig_Extension_StringLoader
        tags:
            - { name: twig.extension }

这可以添加到 /app/config/config.yml 以在每个包中使用或将其添加到包文件夹中的 /Resources/config/services.yml 仅在特定包中使用.

This can be added to /app/config/config.yml to use to in each bundle or add it into your bundle folder into /Resources/config/services.yml to use in only in certain bundle.

然后在树枝模板中将它们用作:

Then in twig templates use them as:

{{ include(template_from_string(page.template)) }}

以上在 Symfony v2.5 中对我有用

The above works for me in Symfony v2.5

这篇关于在 Symfony 中激活 StringLoader Twig 扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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