Setting up Slim PHP Framework with Twig templating

17 Jan

Recently, I migrated one of my Facebook Canvas apps to the Slim Framework and Twig templating.

Unfortunately, the Slim documentation is… very slim… so I ran into some trouble with the paths and how to include everything to make it work. Most of the suggested solutions I found via Google where either outdated or not working.

To save you the hassle, here is how I made it work.

Let’s start with precise version info for what I’ve used:

  • Slim 2.2.0 from GitHub.
  • Slim Extras from GitHub. Too bad, there’s no version info on those. When I downloaded the extras, the latest commit was dated January 7th, 2013 on the master branch.
  • Twig 1.12.2 from GitHub.
  • PHP 5.4.4.

Next: Folder structure.

  • I moved the Slim folder (and only the Slim folder) into my project’s root.
  • Inside the Slim folder I manually created an Extras folder.
  • I moved the Views folder (and only the Views folder) from the Slim Extras inside this manually created Extras folder.
  • I moved the Twig folder (and only the Twig folder) into the project root. The Twig folder is located inside the lib folder when you download Twig from GitHub.
  • I manually created a templates folder inside the project root.

Next: Code.

  • Find Twig.php inside Slim/Extras/ and change public static $twigDirectory = “”; to¬†public static $twigDirectory = “Twig”;
  • Make sure, you also move .htaccess from the Slim bundle to your web server. Sometimes it’s hidden and you might miss this step. Depending on your hosting provider, you might have to change the RewriteBase inside .htaccess. The default worked fine for me.

Finally, here’s how I had to initialize Slim, the Extras and Twig:

With all of the above done, I was able to seamlessly use Twig’s template engine via the Slim framework as in the following example:

Hope this helps and good luck!

Tags: , ,
6 replies
  1. Ralf says:

    I love the social web: Shortly after this post, Andrew Smith, a contributor to the Slim Extras code base got in touch with me and corrected the documentation. (

  2. Peter says:

    Hi Ralf,
    I’ve some issues with new Slim 2. I’ve tried to connect Slim with Twig as you’ve written above but when I’ve added:
    $app = new Slim(array(
    \’view\’ => new \Slim\Extras\Views\Twig
    rendered site disapeared (blank page). Any clue ?


  3. Fanie says:

    Thank you.
    This helped a lot.
    I really struggled to get my directory structure right and slim working, until I found this page. I wish I found it before I pulled all my hair out.

  4. J Palala says:

    What Im having trouble with is what’sthe point of composer if it’s supposed to do dependency management. What would the composer ( way of doing things?

  5. Stas says:

    I like to use Codelobster to edit Twig templates

  6. Perry says:

    ok, the only issue I ran into was something about strict mode error or something – the page actually did render underneath the error.

    Basically the render function in the Slim Extras in Twig.php doesn’t match the parameters of the one in Slim that the class extends from.

    In Twig.php it’s render($template) in Views.php in Slim it’s render($template, $data = NULL). I could have turned off Slims error handling like you’d do in production, but I fixed it by adding the $data = NULL parameter to the render function in twig.php and it went away.

    Kind of pointless to have a null parameter that doesn’t do anything, but whatever!


Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

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