Twig_Error_Syntax:未知"template_from_string"功能。功能过时了?



我记得在作曲家更新后我遇到了这个错误,但以前它运行良好,在比较软件包版本时我没有发现差异。我在模板中以这种方式使用它:

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

其中"页面"是来自控制器的实体对象。当前版本是:

symfony/symfony                          v2.8.15
twig/extensions                          v1.4.1
twig/twig                                v1.30.0

同样在 config.yml 中:

twig:
    debug:            "%kernel.debug%"
    strict_variables: "%kernel.debug%"
    form_themes:
        - 'AppBundle::Form/form_override.html.twig'

现在vendor/twig/twig/lib/twig/Twig/Extension/StringLoader.php有视觉上正确的文件,但似乎它仍然不起作用。完整堆栈跟踪:

[1] Twig_Error_Syntax: Unknown "template_from_string" function.
    at n/a
        in ProjectDirsrcAppBundle/Resources/views/Page.html.twig line 22
    at Twig_ExpressionParser->getFunctionNodeClass('template_from_string', '22')
        in ProjectDirvendortwigtwiglibTwigExpressionParser.php line 374
    at Twig_ExpressionParser->getFunctionNode('template_from_string', '22')
        in ProjectDirvendortwigtwiglibTwigExpressionParser.php line 162
    at Twig_ExpressionParser->parsePrimaryExpression()
        in ProjectDirvendortwigtwiglibTwigExpressionParser.php line 102
    at Twig_ExpressionParser->getPrimary()
        in ProjectDirvendortwigtwiglibTwigExpressionParser.php line 55
    at Twig_ExpressionParser->parseExpression()
        in ProjectDirvendortwigtwiglibTwigExpressionParser.php line 520
    at Twig_ExpressionParser->parseArguments(true)
        in ProjectDirvendortwigtwiglibTwigExpressionParser.php line 373
    at Twig_ExpressionParser->getFunctionNode('include', '22')
        in ProjectDirvendortwigtwiglibTwigExpressionParser.php line 162
    at Twig_ExpressionParser->parsePrimaryExpression()
        in ProjectDirvendortwigtwiglibTwigExpressionParser.php line 102
    at Twig_ExpressionParser->getPrimary()
        in ProjectDirvendortwigtwiglibTwigExpressionParser.php line 55
    at Twig_ExpressionParser->parseExpression()
        in ProjectDirvendortwigtwiglibTwigParser.php line 147
    at Twig_Parser->subparse(array(object(Twig_TokenParser_Block), 'decideBlockEnd'), true)
        in ProjectDirvendortwigtwiglibTwigTokenParserBlock.php line 40
    at Twig_TokenParser_Block->parse(object(Twig_Token))
        in ProjectDirvendortwigtwiglibTwigParser.php line 190
    at Twig_Parser->subparse(null, false)
        in ProjectDirvendortwigtwiglibTwigParser.php line 103
    at Twig_Parser->parse(object(Twig_TokenStream))
        in ProjectDirappcachedevclasses.php line 3415
    at Twig_Environment->parse(object(Twig_TokenStream))
        in ProjectDirappcachedevclasses.php line 3443
    at Twig_Environment->compileSource(object(Twig_Source))
        in ProjectDirappcachedevclasses.php line 3291
    at Twig_Environment->loadTemplate('AppBundle::Page.html.twig')
        in ProjectDirvendorsymfonysymfonysrcSymfonyBridgeTwigTwigEngine.php line 126
    at SymfonyBridgeTwigTwigEngine->load('AppBundle::Page.html.twig')
        in ProjectDirvendorsymfonysymfonysrcSymfonyBridgeTwigTwigEngine.php line 50
    at SymfonyBridgeTwigTwigEngine->render('AppBundle::Page.html.twig', array('page' => object(Page), 'ads' => array(object(VertBanner), object(VertBanner)), 'slug' => 'direktor'))
        in ProjectDirvendorsymfonysymfonysrcSymfonyBundleTwigBundleTwigEngine.php line 72
    at SymfonyBundleTwigBundleTwigEngine->render('AppBundle::Page.html.twig', array('page' => object(Page), 'ads' => array(object(VertBanner), object(VertBanner)), 'slug' => 'direktor'))
        in ProjectDirvendorsymfonysymfonysrcSymfonyBundleTwigBundleTwigEngine.php line 103
    at SymfonyBundleTwigBundleTwigEngine->renderResponse('AppBundle::Page.html.twig', array('page' => object(Page), 'ads' => array(object(VertBanner), object(VertBanner)), 'slug' => 'direktor'), null)
        in ProjectDirvendorsymfonysymfonysrcSymfonyBundleFrameworkBundleControllerController.php line 185
    at SymfonyBundleFrameworkBundleControllerController->render('AppBundle::Page.html.twig', array('page' => object(Page), 'ads' => array(object(VertBanner), object(VertBanner)), 'slug' => 'direktor'))
        in ProjectDirsrcAppBundleControllerPageController.php line 35
    at AppBundleControllerPageController->getPageAction('direktor')
        in  line 
    at call_user_func_array(array(object(PageController), 'getPageAction'), array('direktor'))
        in ProjectDirvendorsymfonysymfonysrcSymfonyComponentHttpKernelHttpKernel.php line 144
    at SymfonyComponentHttpKernelHttpKernel->handleRaw(object(Request), '1')
        in ProjectDirvendorsymfonysymfonysrcSymfonyComponentHttpKernelHttpKernel.php line 64
    at SymfonyComponentHttpKernelHttpKernel->handle(object(Request), '1', true)
        in ProjectDirvendorsymfonysymfonysrcSymfonyComponentHttpKernelDependencyInjectionContainerAwareHttpKernel.php line 69
    at SymfonyComponentHttpKernelDependencyInjectionContainerAwareHttpKernel->handle(object(Request), '1', true)
        in ProjectDirvendorsymfonysymfonysrcSymfonyComponentHttpKernelKernel.php line 185
    at SymfonyComponentHttpKernelKernel->handle(object(Request))
        in ProjectDirwebapp_dev.php line 28
    at require('ProjectDirwebapp_dev.php')
        in ProjectDirvendorsymfonysymfonysrcSymfonyBundleFrameworkBundleResourcesconfigrouter_dev.php line 40

您必须让服务容器知道您的 Twig 扩展:

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

Symfony 4:

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

对于symfony5和twig3,这里的答案有效: Symfony 4:添加Twig_Extension_StringLoader

# config/services.yaml
services:
    TwigExtensionStringLoaderExtension:

由于这在谷歌中更容易找到,我将在此处添加它。

相关内容

  • 没有找到相关文章

最新更新