Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silex micro-framework and Twig: enable debug

Tags:

php

twig

silex

My question: How do I permit use of debug in Twig templates within Silex?


I'm playing around with the Silex micro-framework (a PHP framework that leverages Symfony).

When using the Twig template system with it I wanted to output a particular object. Normally I would do this with var_dump($app); and in Twig with {% debug app %}.

My problem is getting debug (and setting Silex's own debug to true does not help with Twig) to work with Silex. Out of the box a call to debug will result in an error message:

Twig_Error_Syntax: Unknown tag name "debug" in...

The call to debug looks like this:

{% debug app %}

I have found references to how to configure Twig's config.yml file to correctly use debug but Silex does not use a config file for Twig.

Silex does say you can set options via passing an associative array to twig.options and while Twig docs say you can pass an environment option like:

$twig = new Twig_Environment($loader, array('debug' => true));

Trying to pass it in Silex like:

$app->register(new Silex\Provider\TwigServiceProvider(), array(
    'twig.options' => array('debug' => true),
));

Does not work. Is this the wrong sort of option? Simply incorrect formatting? I've no idea and nothing I have tried works.

I sense myself getting into "wheel spinning" mode so I am asking here on SO in hopes that I can move on to more productive work this morning. :)

(ugh... how's that for a hyper-specific StackOverflow question?)


Solution: (all this just to get var_dump like functionality... oh my): This was a bit of a pain in the butt, to be honest, and the Silex docs were of no help whatsoever in discovering this but here is what I had to do to get this to work.

First load the Twig autoloader:

$app['autoloader']->registerPrefixes(array(
    'Twig_Extensions_'  => array(__DIR__.'/vendor/Twig-extensions/lib')));

Why do you have to register it this way? No idea. How does it actually find the autoloader? No idea. But it works.

Register the provider and set the debug option:

$app->register(new Silex\Provider\TwigServiceProvider(), array(
    'twig.path'         => __DIR__.'/views',
    'twig.class_path'   => __DIR__.'/vendor/Twig/lib',
    'twig.options'      => array('debug' => true), //<-- this is the key line to getting debug added to the options
));

And finally (the best part):

$oldTwigConfiguration = isset($app['twig.configure']) ? $app['twig.configure']: function(){};
$app['twig.configure'] = $app->protect(function($twig) use ($oldTwigConfiguration) {
    $oldTwigConfiguration($twig);
    $twig->addExtension(new Twig_Extensions_Extension_Debug());
});

To be honest, I think that's enough Silex for me.

Credit for this solution goes to Nerdpress.


*ninja edit: a year and a half later I have to say that Silex was a dud for me. I have been using Slim for all micro-framework needs and it is fantastic. Gets the job done quickly, cleanly, simply and easily.

like image 245
rg88 Avatar asked Feb 08 '12 15:02

rg88


1 Answers

I completely removed the old answer to show the output from a little example-app I built:

composer.json:

{
  "require": {
    "silex/silex": "1.*",
    "twig/twig": "1.*",
    "twig/extensions": "*"
  }
}

app.php:

require_once __DIR__ . '/../vendor/.composer/autoload.php';

$app = new Silex\Application();

$app['debug'] = true;

$app->register(new Silex\Provider\TwigServiceProvider(), array(
    'twig.path' => __DIR__ . '/views',
    'twig.options' => array('debug' => true)
));
$app['twig']->addExtension(new Twig_Extensions_Extension_Debug());

$app->get('/', function () use ($app) {
    return $app['twig']->render('debug_test.twig', array('app' => $app));
});
$app->run();
like image 95
dbrumann Avatar answered Sep 21 '22 09:09

dbrumann