Silex - Twig_Error_Syntax:函数“路径"不存在 [英] Silex - Twig_Error_Syntax: The function "path" does not exist

查看:19
本文介绍了Silex - Twig_Error_Syntax:函数“路径"不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 Silex 文档:><块引用>

Symfony 提供了一个 Twig 桥,它提供了一些 Symfony2 组件和 Twig 之间的额外集成.将其作为依赖项添加到您的 composer.json 文件中.

我在我的 composer.json 文件中包含以下内容:

<代码>{要求": {"silex/silex": "1.*","twig/twig": ">=1.8,<2.0-dev","symfony/twig-bridge": "2.3.*"}}

我像这样注册 TwigServiceProvider() :

$app->register(new Silex\Provider\TwigServiceProvider(), array('twig.path' =>__DIR__ .'/视图'));

我正在尝试像这样使用树枝 path() 方法:

注销</a>

我得到的错误如下:

<块引用>

Twig_Error_Syntax:函数路径"不存在

为什么我会收到这个错误?

  • 我尝试切换版本以检查是否是版本问题
  • 一个谷歌群组评论建议注册"树枝桥提供者,但这并不存在
  • 不想不得不在我的所有模板中使用:app.url_generator.generate
<小时>

我找到的临时解决方案:

确保 UrlGeneratorServiceProvider() 已注册:

$app->register(new UrlGeneratorServiceProvider());

path() 的 twig 创建一个新函数:

$app['twig']->addFunction(new \Twig_SimpleFunction('path', function($url) use ($app) {返回 $app['url_generator']->generate($url);}));

我不应该这样做!!我怎样才能正常工作?

解决方案

希望这对未来的观众有所帮助,因为许多人发布了这个问题而没有得到可靠的答案,所以这里是一个.

实际上就是需要UrlGeneratorServiceProvider()注册

$app->register(new UrlGeneratorServiceProvider());

另外,正如 umpirsky 在评论中提到的,您需要通过 composer 安装 symfony/twig-bridge.

您不需要添加自己的函数.在加载您的树枝模板之前,您需要注册 TwigServiceProvider() UrlGeneratorServiceProvider().这在文档中并不容易看出.

According to the Silex documentation:

Symfony provides a Twig bridge that provides additional integration between some Symfony2 components and Twig. Add it as a dependency to your composer.json file.

I include the following in my composer.json file:

{
    "require": {
        "silex/silex": "1.*",
        "twig/twig": ">=1.8,<2.0-dev",
        "symfony/twig-bridge": "2.3.*"
    }
}

I register the TwigServiceProvider() like so:

$app->register(new Silex\Provider\TwigServiceProvider(), array(
    'twig.path' => __DIR__ . '/views'
));

I'm attempting to use the twig path() method like so:

<a href="{{ path('logout') }}">Log out</a>

The error I get is as follows:

Twig_Error_Syntax: The function "path" does not exist

Why am I getting this error?

  • I have tried switching around versions to check if it is a version issue
  • One google groups comment suggested 'registering' the twig bridge provider, but this doesn't exist
  • I don't want to have to use: app.url_generator.generate in all my templates instead

A temporary solution I have found:

Ensure The UrlGeneratorServiceProvider() is registered:

$app->register(new UrlGeneratorServiceProvider());

Create a new function for twig for path():

$app['twig']->addFunction(new \Twig_SimpleFunction('path', function($url) use ($app) {
    return $app['url_generator']->generate($url);
}));

I shouldn't have to do this!! How can I get this working properly?

解决方案

Hopefully this will help future viewers as many have posted this question without a solid answer, so here is one.

It is literally that you need UrlGeneratorServiceProvider() registered

$app->register(new UrlGeneratorServiceProvider());

Also, as umpirsky mentions in the comments, you need symfony/twig-bridge installed via composer.

You do not need to add your own function. You need both the TwigServiceProvider() and the UrlGeneratorServiceProvider() registered before loading your twig template. This isn't easily apparent from the documentation.

这篇关于Silex - Twig_Error_Syntax:函数“路径"不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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