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

问题描述

根据 Silex 文档:p>

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

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

{要求": {"silex/silex": "1.*","twig/twig": ">=1.8,<2.0-dev",symfony/树枝桥":2.3.*"}}

我像这样注册 TwigServiceProvider():

$app->register(new SilexProviderTwigServiceProvider(), array('twig.path' =>__DIR__ .'/意见'));

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

<a href="{{ path('logout') }}">注销</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:

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 SilexProviderTwigServiceProvider(), 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:

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:函数“路径";不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 10:18