Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twig_Error_Syntax: Unknown "template_from_string" function. Function became obsolete?

I remember after composer updating I got this error, but previously it worked perfectly, and in comparing the packages versions I didn't find differences. I used it in this way in template:

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

where "page" is entity object from controller. Current versions are:

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

Also in config.yml:

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

Now there is in vendor/twig/twig/lib/twig/Twig/Extension/StringLoader.php visually correct file, but seems it still not working. Full stacktrace:

[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
like image 670
Dr.X Avatar asked Jan 04 '17 13:01

Dr.X


Video Answer


2 Answers

You must let the Service Container know about your Twig Extension:

# 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' }
like image 165
Gopal Joshi Avatar answered Oct 23 '22 14:10

Gopal Joshi


For symfony5 and twig3, the answer from here worked: Symfony 4: Add Twig_Extension_StringLoader

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

As this is more easily findable in google, I'll add it here.

like image 40
Tom730 Avatar answered Oct 23 '22 12:10

Tom730