簡體   English   中英

Twig_Error_Syntax:未知的“template_from_string”函數。 功能過時了嗎?

[英]Twig_Error_Syntax: Unknown “template_from_string” function. Function became obsolete?

我記得在 Composer 更新后我得到了這個錯誤,但以前它工作得很好,在比較軟件包版本時我沒有發現差異。 我在模板中以這種方式使用它:

{{ 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 ProjectDir\src\AppBundle/Resources/views/Page.html.twig line 22

    at Twig_ExpressionParser->getFunctionNodeClass('template_from_string', '22')
        in ProjectDir\vendor\twig\twig\lib\Twig\ExpressionParser.php line 374

    at Twig_ExpressionParser->getFunctionNode('template_from_string', '22')
        in ProjectDir\vendor\twig\twig\lib\Twig\ExpressionParser.php line 162

    at Twig_ExpressionParser->parsePrimaryExpression()
        in ProjectDir\vendor\twig\twig\lib\Twig\ExpressionParser.php line 102

    at Twig_ExpressionParser->getPrimary()
        in ProjectDir\vendor\twig\twig\lib\Twig\ExpressionParser.php line 55

    at Twig_ExpressionParser->parseExpression()
        in ProjectDir\vendor\twig\twig\lib\Twig\ExpressionParser.php line 520

    at Twig_ExpressionParser->parseArguments(true)
        in ProjectDir\vendor\twig\twig\lib\Twig\ExpressionParser.php line 373

    at Twig_ExpressionParser->getFunctionNode('include', '22')
        in ProjectDir\vendor\twig\twig\lib\Twig\ExpressionParser.php line 162

    at Twig_ExpressionParser->parsePrimaryExpression()
        in ProjectDir\vendor\twig\twig\lib\Twig\ExpressionParser.php line 102

    at Twig_ExpressionParser->getPrimary()
        in ProjectDir\vendor\twig\twig\lib\Twig\ExpressionParser.php line 55

    at Twig_ExpressionParser->parseExpression()
        in ProjectDir\vendor\twig\twig\lib\Twig\Parser.php line 147

    at Twig_Parser->subparse(array(object(Twig_TokenParser_Block), 'decideBlockEnd'), true)
        in ProjectDir\vendor\twig\twig\lib\Twig\TokenParser\Block.php line 40

    at Twig_TokenParser_Block->parse(object(Twig_Token))
        in ProjectDir\vendor\twig\twig\lib\Twig\Parser.php line 190

    at Twig_Parser->subparse(null, false)
        in ProjectDir\vendor\twig\twig\lib\Twig\Parser.php line 103

    at Twig_Parser->parse(object(Twig_TokenStream))
        in ProjectDir\app\cache\dev\classes.php line 3415

    at Twig_Environment->parse(object(Twig_TokenStream))
        in ProjectDir\app\cache\dev\classes.php line 3443

    at Twig_Environment->compileSource(object(Twig_Source))
        in ProjectDir\app\cache\dev\classes.php line 3291

    at Twig_Environment->loadTemplate('AppBundle::Page.html.twig')
        in ProjectDir\vendor\symfony\symfony\src\Symfony\Bridge\Twig\TwigEngine.php line 126

    at Symfony\Bridge\Twig\TwigEngine->load('AppBundle::Page.html.twig')
        in ProjectDir\vendor\symfony\symfony\src\Symfony\Bridge\Twig\TwigEngine.php line 50

    at Symfony\Bridge\Twig\TwigEngine->render('AppBundle::Page.html.twig', array('page' => object(Page), 'ads' => array(object(VertBanner), object(VertBanner)), 'slug' => 'direktor'))
        in ProjectDir\vendor\symfony\symfony\src\Symfony\Bundle\TwigBundle\TwigEngine.php line 72

    at Symfony\Bundle\TwigBundle\TwigEngine->render('AppBundle::Page.html.twig', array('page' => object(Page), 'ads' => array(object(VertBanner), object(VertBanner)), 'slug' => 'direktor'))
        in ProjectDir\vendor\symfony\symfony\src\Symfony\Bundle\TwigBundle\TwigEngine.php line 103

    at Symfony\Bundle\TwigBundle\TwigEngine->renderResponse('AppBundle::Page.html.twig', array('page' => object(Page), 'ads' => array(object(VertBanner), object(VertBanner)), 'slug' => 'direktor'), null)
        in ProjectDir\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Controller\Controller.php line 185

    at Symfony\Bundle\FrameworkBundle\Controller\Controller->render('AppBundle::Page.html.twig', array('page' => object(Page), 'ads' => array(object(VertBanner), object(VertBanner)), 'slug' => 'direktor'))
        in ProjectDir\src\AppBundle\Controller\PageController.php line 35

    at AppBundle\Controller\PageController->getPageAction('direktor')
        in  line 

    at call_user_func_array(array(object(PageController), 'getPageAction'), array('direktor'))
        in ProjectDir\vendor\symfony\symfony\src\Symfony\Component\HttpKernel\HttpKernel.php line 144

    at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), '1')
        in ProjectDir\vendor\symfony\symfony\src\Symfony\Component\HttpKernel\HttpKernel.php line 64

    at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), '1', true)
        in ProjectDir\vendor\symfony\symfony\src\Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel.php line 69

    at Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel->handle(object(Request), '1', true)
        in ProjectDir\vendor\symfony\symfony\src\Symfony\Component\HttpKernel\Kernel.php line 185

    at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
        in ProjectDir\web\app_dev.php line 28

    at require('ProjectDir\web\app_dev.php')
        in ProjectDir\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Resources\config\router_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: Twig\Extension\StringLoaderExtension
    tags:
        - { name: 'twig.extension' }

對於 symfony5 和 twig3,這里的答案有效: Symfony 4: Add Twig_Extension_StringLoader

# config/services.yaml
services:
    Twig\Extension\StringLoaderExtension:

由於這在谷歌中更容易找到,我將在此處添加。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM