Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

twig 2 not works with debug panel #21212

Closed
Codenator81 opened this issue Jan 9, 2017 · 15 comments
Closed

twig 2 not works with debug panel #21212

Codenator81 opened this issue Jan 9, 2017 · 15 comments

Comments

@Codenator81
Copy link

Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no
Symfony version 3.2.1

Have issues with twig 2. It not works with debug panel
just do composer update on standard project from symfony 3.2.1

Twig_Error_Syntax in exception.html.twig line 12:
Unknown "format_file_from_text" filter.

and I have errors with my Twig services because Twig_Function_Method and Twig_SimpleFilter removed from Twig. Fixed when change to Twig_Function and Twig_Filter

@stof
Copy link
Member

stof commented Jan 9, 2017

Twig_SimpleFilter is not removed from Twig 2. So saying you have an error because of it looks weird.

and Twig_Function_Method is not used anymore in Symfony, so it probably comes from your own code or from a third-party bundle (and this class is indeed removed in Twig 2, as it was deprecated in Twig 1.x since some time)

@Codenator81
Copy link
Author

@stof

Twig_Error_Syntax in exception.html.twig line 12:
Unknown "format_file_from_text" filter.

this came from TwigBundle

@stof
Copy link
Member

stof commented Jan 9, 2017

But this filter is defined in Symfony, so it looks weird

@fabpot
Copy link
Member

fabpot commented Jan 9, 2017

I got the same error a few days ago and it was because of a definition issue in my code. I will try to replicate to see how we can get a better error message in this case.

@Codenator81
Copy link
Author

Codenator81 commented Jan 9, 2017

  1. When I try install new symfony 3 project via composer composer create-project symfony/framework-standard-edition my_project_name it dont update to twig 2.0 straight way with composer update`
  2. when install new project via symfony installer it is allow update to twig 2.0 straight after install via composer update and no errors on plain project

So that is my project update issue. My project started on symfony 3.0.0
Will try check composer.json

    "require": {
        "php": ">=5.5.9",
        "symfony/symfony": "3.2.*",
        "doctrine/orm": "^2.5",
        "doctrine/doctrine-bundle": "^1.6",
        "doctrine/doctrine-cache-bundle": "^1.2",
        "symfony/swiftmailer-bundle": "^2.3",
        "symfony/monolog-bundle": "^3.0",
        "symfony/polyfill-apcu": "^1.0",
        "sensio/distribution-bundle": "^5.0",
        "sensio/framework-extra-bundle": "^3.0.2",
        "incenteev/composer-parameter-handler": "^2.0",
        "symfony/assetic-bundle": "~2.8",
        "gedmo/doctrine-extensions": "^2.4",
        "symfony/serializer": "3.2.1",
        "symfony/property-access": "3.2.1",
        "nesbot/carbon": "^1.21"
    },
    "require-dev": {
        "sensio/generator-bundle": "^3.0",
        "symfony/phpunit-bridge": "^3.0",
        "doctrine/doctrine-fixtures-bundle": "^2.3"
    },

my composer.json

@Codenator81
Copy link
Author

Codenator81 commented Jan 9, 2017

so there only

        "symfony/assetic-bundle": "~2.8",
        "gedmo/doctrine-extensions": "^2.4",
        "symfony/serializer": "3.2.1",
        "symfony/property-access": "3.2.1",
        "nesbot/carbon": "^1.21"
        "doctrine/doctrine-fixtures-bundle": "^2.3"

added on top of original composer.json
tryed add this deps to new project and it not crash it on twig 2.0

@Codenator81
Copy link
Author

Issue arise when I add custom extension:

#service.yml
    app.twig_extension:
        class: AppBundle\Twig\AppExtension
        public: false
        autowire: true
        tags:
            - { name: twig.extension }
#Twig/AppExtension.php
<?php
namespace AppBundle\Twig;

use Symfony\Component\DependencyInjection\ContainerInterface as Container;

class AppExtension extends \Twig_Extension
{
    /**
     * @var Container
     */
    protected $container;

    public function __construct(Container $container) {
        $this->container = $container;
    }

    public function getFunctions() {
        return [
            'cardEmpty' => new \Twig_Function($this, 'cardEmpty')
        ];
    }

    public function getFilters() {
        return [
            new \Twig_SimpleFilter('json_decode', array($this, 'jsonDecode'))
        ];
    }

    public function cardEmpty() {
        $session = $this->container->get('session');
        $data = $session->get('backet');
        if (isset($data['total_amount'])) {
            return $data['total_amount'] > 0 ? false : true;
        }
        return true;
    }



    public function jsonDecode($str) {
        return json_decode($str);
    }

    public function getName()
    {
        return 'app_extension';
    }
}

@stof
Copy link
Member

stof commented Jan 9, 2017

Can you show the output of composer show -i |grep twig ?

@Codenator81
Copy link
Author

AleksandrssiMac:front alex$ composer show -i |grep twig
You are using the deprecated option "installed". Only installed packages are shown by default now. The --all option can be used to show all packages.
twig/twig v2.0.0 Twig, the flexible, fast, and secure template language for PHP

@Codenator81
Copy link
Author

Error page

Whoops, looks like something went wrong.

3/3
Twig_Error_Syntax in exception.html.twig line 12:
Unknown "format_file_from_text" filter.
in exception.html.twig line 12
at Twig_ExpressionParser->getFilterNodeClass('format_file_from_text', 12) in ExpressionParser.php line 467
at Twig_ExpressionParser->parseFilterExpressionRaw(object(Twig_Node_Expression_Filter)) in ExpressionParser.php line 452
at Twig_ExpressionParser->parseFilterExpression(object(Twig_Node_Expression_GetAttr)) in ExpressionParser.php line 310
at Twig_ExpressionParser->parsePostfixExpression(object(Twig_Node_Expression_GetAttr)) in ExpressionParser.php line 202
at Twig_ExpressionParser->parsePrimaryExpression() in ExpressionParser.php line 92
at Twig_ExpressionParser->getPrimary() in ExpressionParser.php line 45
at Twig_ExpressionParser->parseExpression() in Parser.php line 123
at Twig_Parser->subparse(null, false) in Parser.php line 79
at Twig_Parser->parse(object(Twig_TokenStream)) in classes.php line 5937
at Twig_Environment->parse(object(Twig_TokenStream)) in classes.php line 5953
at Twig_Environment->compileSource(object(Twig_Source)) in classes.php line 5868
at Twig_Environment->loadTemplate('@Twig/Exception/exception.html.twig', null) in classes.php line 7250
at Twig_Template->loadTemplate('@Twig/Exception/exception.html.twig', '@Twig/Exception/exception_full.html.twig', 141) in 15781942ffb9250c9f4510a35fb14689664148a6908408578c982c9af293c1c8.php line 185
at __TwigTemplate_8c8f2fa202c26bc53a2a21aef0bc60dbd149f09e7d07c71dc9c6d61391ac3966->block_body(array('status_code' => 500, 'status_text' => 'Internal Server Error', 'exception' => object(FlattenException), 'logger' => object(Logger), 'currentContent' => '', 'assetic' => array('debug' => true, 'vars' => object(ValueContainer), 'use_controller' => true), 'app' => object(AppVariable), 'glob_underground_gas' => '0.8349'), array('title' => array(object(__TwigTemplate_8c8f2fa202c26bc53a2a21aef0bc60dbd149f09e7d07c71dc9c6d61391ac3966), 'block_title'), 'head' => array(object(__TwigTemplate_8c8f2fa202c26bc53a2a21aef0bc60dbd149f09e7d07c71dc9c6d61391ac3966), 'block_head'), 'body' => array(object(__TwigTemplate_8c8f2fa202c26bc53a2a21aef0bc60dbd149f09e7d07c71dc9c6d61391ac3966), 'block_body'))) in classes.php line 7186
at Twig_Template->displayBlock('body', array('status_code' => 500, 'status_text' => 'Internal Server Error', 'exception' => object(FlattenException), 'logger' => object(Logger), 'currentContent' => '', 'assetic' => array('debug' => true, 'vars' => object(ValueContainer), 'use_controller' => true), 'app' => object(AppVariable), 'glob_underground_gas' => '0.8349'), array('title' => array(object(__TwigTemplate_8c8f2fa202c26bc53a2a21aef0bc60dbd149f09e7d07c71dc9c6d61391ac3966), 'block_title'), 'head' => array(object(__TwigTemplate_8c8f2fa202c26bc53a2a21aef0bc60dbd149f09e7d07c71dc9c6d61391ac3966), 'block_head'), 'body' => array(object(__TwigTemplate_8c8f2fa202c26bc53a2a21aef0bc60dbd149f09e7d07c71dc9c6d61391ac3966), 'block_body'))) in 3dc8c459b8a45adb1a7b9b72b25a80af91a78722b3e7429b7e15d91a85d9c186.php line 282
at __TwigTemplate_4863a595bf550db1b63bb8da456df9c69130fc09cbd7e7a41d7ad3ba904678f4->doDisplay(array('status_code' => 500, 'status_text' => 'Internal Server Error', 'exception' => object(FlattenException), 'logger' => object(Logger), 'currentContent' => '', 'assetic' => array('debug' => true, 'vars' => object(ValueContainer), 'use_controller' => true), 'app' => object(AppVariable), 'glob_underground_gas' => '0.8349'), array('title' => array(object(__TwigTemplate_8c8f2fa202c26bc53a2a21aef0bc60dbd149f09e7d07c71dc9c6d61391ac3966), 'block_title'), 'head' => array(object(__TwigTemplate_8c8f2fa202c26bc53a2a21aef0bc60dbd149f09e7d07c71dc9c6d61391ac3966), 'block_head'), 'body' => array(object(__TwigTemplate_8c8f2fa202c26bc53a2a21aef0bc60dbd149f09e7d07c71dc9c6d61391ac3966), 'block_body'))) in classes.php line 7296
at Twig_Template->displayWithErrorHandling(array('status_code' => 500, 'status_text' => 'Internal Server Error', 'exception' => object(FlattenException), 'logger' => object(Logger), 'currentContent' => '', 'assetic' => array('debug' => true, 'vars' => object(ValueContainer), 'use_controller' => true), 'app' => object(AppVariable), 'glob_underground_gas' => '0.8349'), array('title' => array(object(__TwigTemplate_8c8f2fa202c26bc53a2a21aef0bc60dbd149f09e7d07c71dc9c6d61391ac3966), 'block_title'), 'head' => array(object(__TwigTemplate_8c8f2fa202c26bc53a2a21aef0bc60dbd149f09e7d07c71dc9c6d61391ac3966), 'block_head'), 'body' => array(object(__TwigTemplate_8c8f2fa202c26bc53a2a21aef0bc60dbd149f09e7d07c71dc9c6d61391ac3966), 'block_body'))) in classes.php line 7272
at Twig_Template->display(array('status_code' => 500, 'status_text' => 'Internal Server Error', 'exception' => object(FlattenException), 'logger' => object(Logger), 'currentContent' => '', 'assetic' => array('debug' => true, 'vars' => object(ValueContainer), 'use_controller' => true), 'app' => object(AppVariable), 'glob_underground_gas' => '0.8349'), array('head' => array(object(__TwigTemplate_8c8f2fa202c26bc53a2a21aef0bc60dbd149f09e7d07c71dc9c6d61391ac3966), 'block_head'), 'title' => array(object(__TwigTemplate_8c8f2fa202c26bc53a2a21aef0bc60dbd149f09e7d07c71dc9c6d61391ac3966), 'block_title'), 'body' => array(object(__TwigTemplate_8c8f2fa202c26bc53a2a21aef0bc60dbd149f09e7d07c71dc9c6d61391ac3966), 'block_body'))) in 15781942ffb9250c9f4510a35fb14689664148a6908408578c982c9af293c1c8.php line 26
at __TwigTemplate_8c8f2fa202c26bc53a2a21aef0bc60dbd149f09e7d07c71dc9c6d61391ac3966->doDisplay(array('status_code' => 500, 'status_text' => 'Internal Server Error', 'exception' => object(FlattenException), 'logger' => object(Logger), 'currentContent' => '', 'assetic' => array('debug' => true, 'vars' => object(ValueContainer), 'use_controller' => true), 'app' => object(AppVariable), 'glob_underground_gas' => '0.8349'), array('head' => array(object(__TwigTemplate_8c8f2fa202c26bc53a2a21aef0bc60dbd149f09e7d07c71dc9c6d61391ac3966), 'block_head'), 'title' => array(object(__TwigTemplate_8c8f2fa202c26bc53a2a21aef0bc60dbd149f09e7d07c71dc9c6d61391ac3966), 'block_title'), 'body' => array(object(__TwigTemplate_8c8f2fa202c26bc53a2a21aef0bc60dbd149f09e7d07c71dc9c6d61391ac3966), 'block_body'))) in classes.php line 7296
at Twig_Template->displayWithErrorHandling(array('status_code' => 500, 'status_text' => 'Internal Server Error', 'exception' => object(FlattenException), 'logger' => object(Logger), 'currentContent' => '', 'assetic' => array('debug' => true, 'vars' => object(ValueContainer), 'use_controller' => true), 'app' => object(AppVariable), 'glob_underground_gas' => '0.8349'), array('head' => array(object(__TwigTemplate_8c8f2fa202c26bc53a2a21aef0bc60dbd149f09e7d07c71dc9c6d61391ac3966), 'block_head'), 'title' => array(object(__TwigTemplate_8c8f2fa202c26bc53a2a21aef0bc60dbd149f09e7d07c71dc9c6d61391ac3966), 'block_title'), 'body' => array(object(__TwigTemplate_8c8f2fa202c26bc53a2a21aef0bc60dbd149f09e7d07c71dc9c6d61391ac3966), 'block_body'))) in classes.php line 7272
at Twig_Template->display(array('status_code' => 500, 'status_text' => 'Internal Server Error', 'exception' => object(FlattenException), 'logger' => object(Logger), 'currentContent' => '')) in classes.php line 7279
at Twig_Template->render(array('status_code' => 500, 'status_text' => 'Internal Server Error', 'exception' => object(FlattenException), 'logger' => object(Logger), 'currentContent' => '')) in classes.php line 5840
at Twig_Environment->render('@Twig/Exception/exception_full.html.twig', array('status_code' => 500, 'status_text' => 'Internal Server Error', 'exception' => object(FlattenException), 'logger' => object(Logger), 'currentContent' => '')) in ExceptionController.php line 70
at ExceptionController->showAction(object(Request), object(FlattenException), object(Logger))
at call_user_func_array(array(object(ExceptionController), 'showAction'), array(object(Request), object(FlattenException), object(Logger))) in classes.php line 4900
at HttpKernel->handleRaw(object(Request), 2) in classes.php line 4855
at HttpKernel->handle(object(Request), 2, false) in ExceptionListener.php line 50
at ExceptionListener->onKernelException(object(GetResponseForExceptionEvent), 'kernel.exception', object(TraceableEventDispatcher))
at call_user_func(array(object(ExceptionListener), 'onKernelException'), object(GetResponseForExceptionEvent), 'kernel.exception', object(TraceableEventDispatcher)) in WrappedListener.php line 106
at WrappedListener->__invoke(object(GetResponseForExceptionEvent), 'kernel.exception', object(ContainerAwareEventDispatcher))
at call_user_func(object(WrappedListener), object(GetResponseForExceptionEvent), 'kernel.exception', object(ContainerAwareEventDispatcher)) in classes.php line 3240
at EventDispatcher->doDispatch(array(object(WrappedListener), object(WrappedListener)), 'kernel.exception', object(GetResponseForExceptionEvent)) in classes.php line 3155
at EventDispatcher->dispatch('kernel.exception', object(GetResponseForExceptionEvent)) in TraceableEventDispatcher.php line 136
at TraceableEventDispatcher->dispatch('kernel.exception', object(GetResponseForExceptionEvent)) in classes.php line 4932
at HttpKernel->handleException(object(Twig_Error_Syntax), object(Request), 1) in classes.php line 4864
at HttpKernel->handle(object(Request), 1, true) in Kernel.php line 168
at Kernel->handle(object(Request)) in app_dev.php line 30
at require('/Volumes/DataHDD/main/MGP/lpg/web/front/web/app_dev.php') in router_dev.php line 40
2/3
Twig_Error_Syntax in classes.php line 5958:
An exception has been thrown during the compilation of a template ("Warning: Illegal offset type") in "@WebProfiler/Profiler/open.html.twig".
in classes.php line 5958
at Twig_Environment->compileSource(object(Twig_Source)) in classes.php line 5868
at Twig_Environment->loadTemplate('@WebProfiler/Profiler/open.html.twig') in classes.php line 5840
at Twig_Environment->render('@WebProfiler/Profiler/open.html.twig', array('filename' => '/Volumes/DataHDD/main/MGP/lpg/web/front/var/cache/dev/classes.php', 'file' => 'var/cache/dev/classes.php', 'line' => '5958')) in ProfilerController.php line 429
at ProfilerController->openAction(object(Request))
at call_user_func_array(array(object(ProfilerController), 'openAction'), array(object(Request))) in classes.php line 4900
at HttpKernel->handleRaw(object(Request), 1) in classes.php line 4855
at HttpKernel->handle(object(Request), 1, true) in Kernel.php line 168
at Kernel->handle(object(Request)) in app_dev.php line 30
at require('/Volumes/DataHDD/main/MGP/lpg/web/front/web/app_dev.php') in router_dev.php line 40
1/3
ContextErrorException in ExtensionSet.php line 434:
Warning: Illegal offset type
in ExtensionSet.php line 434
at Twig_ExtensionSet->initExtension(object(AppExtension)) in ExtensionSet.php line 420
at Twig_ExtensionSet->initExtensions() in ExtensionSet.php line 388
at Twig_ExtensionSet->getUnaryOperators() in classes.php line 6117
at Twig_Environment->getUnaryOperators() in Lexer.php line 353
at Twig_Lexer->getOperatorRegex() in Lexer.php line 65
at Twig_Lexer->__construct(object(Twig_Environment)) in classes.php line 5924
at Twig_Environment->tokenize(object(Twig_Source)) in classes.php line 5953
at Twig_Environment->compileSource(object(Twig_Source)) in classes.php line 5868
at Twig_Environment->loadTemplate('@WebProfiler/Profiler/open.html.twig') in classes.php line 5840
at Twig_Environment->render('@WebProfiler/Profiler/open.html.twig', array('filename' => '/Volumes/DataHDD/main/MGP/lpg/web/front/var/cache/dev/classes.php', 'file' => 'var/cache/dev/classes.php', 'line' => '5958')) in ProfilerController.php line 429
at ProfilerController->openAction(object(Request))
at call_user_func_array(array(object(ProfilerController), 'openAction'), array(object(Request))) in classes.php line 4900
at HttpKernel->handleRaw(object(Request), 1) in classes.php line 4855
at HttpKernel->handle(object(Request), 1, true) in Kernel.php line 168
at Kernel->handle(object(Request)) in app_dev.php line 30
at require('/Volumes/DataHDD/main/MGP/lpg/web/front/web/app_dev.php') in router_dev.php line 40

@stof
Copy link
Member

stof commented Jan 9, 2017

the Twig_Error_Syntax looks related to the initial error in Twig_ExtensionSet->initExtension(object(AppExtension)), forbidding Twig to initialized extensions properly (and so failing later when we use it to display the exception page, as it is in an incomplete state).

The original issue is that your AppExtension::getFunctions implementation uses the old Twig 1.x way (which triggered warnings since months), and is not compatible with Twig 2.

@fabpot do you have an idea to ensure that exceptions during the extension initialization does not break the exception controller ? May it be solved by moving $this->extensionInitialized = true; at the end of the method, to register it as initialized only when it works ? I think this would avoid the weird Twig_Error_Syntax here, as the exception controller subrequest would then retrigger a (failing) initialization instead of thinking it is done (while incomplete)

@Codenator81
Copy link
Author

thanks @stof Right

    public function getFunctions() {
        return [
            'cardEmpty' => new \Twig_Function('cardEmpty', [$this, 'cardEmpty']),
        ];
    }

fix bug so this is my bug not symfony but error yes weird

@stof
Copy link
Member

stof commented Jan 9, 2017

you can even remove the 'cardEmpty' => part. Array keys are meaningless when using the new way

@Codenator81
Copy link
Author

Codenator81 commented Jan 9, 2017

True. Tested it. Works. Thanks @stof

fabpot added a commit to twigphp/Twig that referenced this issue Jan 9, 2017
…ne (stof)

This PR was merged into the 1.x branch.

Discussion
----------

Delay marking the environment as initialized until it is done

This avoids breaking the symfony exception page rendering saying that filters are not available due to a partial initialization: symfony/symfony#21212
It would report the initialization error instead.

Commits
-------

a33fb6c Delay marking the environment as initialized until it is done
fabpot added a commit to twigphp/Twig that referenced this issue Jan 9, 2017
…tof)

This PR was merged into the 2.x branch.

Discussion
----------

Typehint the function, filter and test names as string

This gives faster feedback when using the class in a wrong way, instead of getting a ``Warning: Illegal offset type in ExtensionSet.php line 434`` during the function registration (see symfony/symfony#21212 for a case getting this message)

Commits
-------

2b4642c Typehint the function, filter and test names as string
@fabpot fabpot closed this as completed Jan 9, 2017
@kemalkolayca
Copy link

Thank you very much. This approach solved my problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants