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.
- 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!