• About
  • Web Development
  • System Administration
  • Miscellanous

How To Enable Debugging Of Twig When Used With Slim 3 Framework

June 28, 2017

Slim LogoBackground

When I'm developing a web application with Laravel, it has a useful helper function: dd($some_variable). It dumps the content of $some_variable into a nice and readable format. But sometimes using Laravel is overkill when the application is such a tiny to small one. Let's use this client requirement for example:

Build web application with ability to:

  1. User register, confirmation, and login. We'll need a page on admin dashboard to manage our users.
  2. Each user has ability to use our calculator by entering some values in a form. The backend will then process those values with third-party API provider A and third-party API provider B. Finally it send the final result (after applying our formula) back to user.
  3. Optionally send calculation result to user email.

Deadline would be 5 days after job awarded.

It's easy to develop this project using Laravel, but doesn't it feels like overkill? That's why I use Slim 3 for this case.

Enable Debugging on Slim 3 Framework

Mentioned on their documentation, Twig has a helper function called dump(). Detailed documentation available here: https://twig.sensiolabs.org/doc/2.x/functions/dump.html.

However, it's not enabled by default. We must add the Twig_Extension_Debug extension explicitly when creating our Twig environment:

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

If you're using slim-skeleton, open the file src/dependencies.php. Complete snippet would be:

// Register component on container
$container['view'] = function ($container) {
    $view = new \Slim\Views\Twig('path/to/templates', [
        'debug' => true, // This line should enable debug mode
        'cache' => 'path/to/cache'
    ]);
    $view->addExtension(new \Slim\Views\TwigExtension(
        $container['router'],
        $container['request']->getUri()
    ));
   
    // This line should allow the use of {{ dump() }}
    $view->addExtension(new \Twig_Extension_Debug());

    return $view;
};

Detailed documentation can be found here: http://discourse.slimframework.com/t/twig-debug-mode/535/2

0Share
0Share
Tweet

Web Development

Related Posts

How to Add CLI in Slim 3 Framework and Schedule it Using Cron

How to Add CLI in Slim 3 Framework and Schedule it Using Cron

In Web Development

0 Comments

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • 
  • 
  • 
  • 
  • 
  • 
  • 
  • 
r00t4bl3.com is a collection of web development tips, tricks, and workarounds. Sometimes also write notes of database queries and errors.

Search content

Popular Posts

How to Install PostgreSQL 10 on Linux Mint 18.3 Sylvia

How to Install Postman Native App in Linux Mint 18.3 Sylvia

How to install MongoDB 3.6 on Linux Mint 18.3 Sylvia

How to Install PostgreSQL 9.6 on Linux Mint 18.1 Serena

Running composer require/update Sometimes Very Slow and Takes Very Long Time

Latest Posts

How to Upgrade from PHP 5.6 to 7 on CentOS 6/7

How to Install PostgreSQL 11 on Linux Mint 19 Tara

How to Install Google Chrome on CentOS 7

How to Create a Live USB of Debian in Linux Mint

How to Create a Live USB of Manjaro Linux in Linux

Web development with love

Copyright © 2016 · r00t4bl3.com.

Password Generator · Contact · Privacy policy · Terms of use