Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7420683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:16:52+00:00 2026-05-29T08:16:52+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T08:16:53+00:00Added an answer on May 29, 2026 at 8:16 am

    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();
    

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Question Can I build a image database/library that has an e-commerce style checkout system
Question Is there a way to have a method that will always run anytime
I'm building a Zend-based Web app that will permit third-party extensions. Essentially, this application's
I'm developing a PHP framework; it started off as application-specific, however it has moved
i have a question for you. I have a component that have 2 methods.
Question: I use the bidirectional dicionary class I found here: Bidirectional 1 to 1
Question How do you modify a value in a PHP array if you do
Question What's the full code to enable jQuery on a page and then use
Question: Can I use the multiprocessing module together with gevent on Windows in an
I can't believe that I can't find the answer to this question but I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.