userfrosting / UserFrosting

Modern PHP user login and management framework
https://www.userfrosting.com
Other
1.64k stars 366 forks source link

Set twig environment to debug for using the twig dump function #356

Closed adamgen closed 9 years ago

adamgen commented 9 years ago

I'm trying to echo an array in twig and I found this documentation on twig's site:

$twig = new Twig_Environment($loader, array(
    'debug' => true,
    // ...
));
$twig->addExtension(new Twig_Extension_Debug());

In /userfrosting/vendor/slim/views/Twig.php I have edited $parserOptions = array('debug' => true); And I have added this line $this->parserInstance->addExtension(new Twig_Extension_Debug()); after

$this->parserInstance = new \Twig_Environment(
                $loader,
                $this->parserOptions
            );

and I get an error saying

Fatal error: Class 'Slim\Views\Twig_Extension_Debug' not found in /public_html/dev/userfrosting/vendor/slim/views/Twig.php on line 126

Warning: Cannot modify header information - headers already sent by (output started at /public_html/dev/userfrosting/vendor/slim/views/Twig.php:126) in /public_html/dev/userfrosting/initialize.php on line 142
alexweissman commented 9 years ago

You might just need to add Twig_Extension via Composer. See this issue: https://github.com/slimphp/Slim-Views/issues/39.

adamgen commented 9 years ago

After adding and installing the extensions via composer I get this server error:

Slim Application Error

The application could not run because of the following error:

Details

Type: Twig_Error_Syntax Message: The function "dump" does not exist in "dashboard.html" at line 23 File: /Applications/MAMP/htdocs/UserFrosting/userfrosting/vendor/twig/twig/lib/Twig/ExpressionParser.php Line: 572

adamgen commented 9 years ago

It's as simple as hack, instead of editing the vendor file (which is a big no no) I have added these line to /userfrosting/initialize.php:

$twig->addExtension(new Twig_Extension_Debug());
$twig-> enableDebug();

And it works like a charm.

FreeWebStyler commented 7 years ago

Same on Slim 3, solution:

in /bootstrap/app.php:

$container['view'] = function($container){
    $view = new \Slim\Views\Twig(__DIR__ . '/../resources/views', [
        'cache' => false,
        'debug' => true
    ]);

    $view->addExtension(new Twig_Extension_Debug());

    $view->addExtension(new \Slim\Views\TwigExtension(
        $container->router,
        $container->request->getUri()
    ));
    return $view;
};
alexweissman commented 7 years ago

@FreeWebber is the Twig_Extension_Debug() something that we should add to the core Sprinkle's ServiceProvider class?