Tutorial: Slim PHP View Templates

In an effort to get something up and running on my personal website I set out to find a lightweight PHP framework that would allow me the flexibility to do what I want without having to deal with the complexities of, say, the Zend Framework or Cake PHP – both are awesome frameworks and good for various purposes, but what I needed was something simple at its core, but flexible enough to grow with.

I started exploring lightweight PHP frameworks, and landed on the Slim PHP Framework . For those of you who don’t know what Slim is, in a nutshell, it’s a RESTful PHP framework that was inspired by the Ruby framework called Sinatra. Ruby and Sinatra are awesome. I enjoy working with the simplicity that is the Ruby, but I find that it’s more expensive to deploy and very few of the inexpensive hosts allow custom gem installs that would be required for a lot of applications; aside from that I work with PHP on a daily basis both professionally and in a lot of my side work, so it just feels good to be able to continue with that paradigm.

After giving the Slim Docs a quick read, it wasn’t immediately obvious what steps needed to be taken to set up a project using a custom view handler (real riveting stuff here, I know).

It’s really not difficult at all, but without a complete example in the documentation it took a little digging… I figured I’d lay out the steps here so that a Slim newbie wanting to use a view handler from the Slim-Extras project might be able hit the ground running instead of trying to decipher documentation.

So here goes…

  1. Go download Slim, and while you’re at it download slim-extras (Slim contains all the files you need to get up and running with the Slim Framework, and Slim-Extras contains the view drivers for various view templates supported by the framework).
  2. For the purposes of this tutorial, and because I like its simplicity, we’re going to be using the Twig Template Engine; go ahead and download Twig.
  3. Decompress the Slim archive, and rename the folder with something relevant to your project. This will serve as the root for your project.
  4. Decompress the Slim-Extras archive, and drop the contents of that (a folder called ‘Views’) inside your project root.
  5. Decompress Twig into your project root, your project should look like the following:

    Project Structure

  6. Navigate into the Views directory and open the TwigView.php file. You’re going to need to edit the $twigDirectory variable (at the time I wrote this it was on line 46 in TwigView.php) from it’s default value of null to whatever your Twig directory called – this is relative to the project root, not the Views directory, so if you left the Twig directory with its default name, you should put ‘Twig’ as the value here.
  7. Open up the index.php file located in the project root folder, and add two things: a require statement for the TwigView handler, and an init parameter that lets Slim know we want to use the TwigView view handler:
  8. Now we’ll go ahead and create a sample route that will use a Twig template (the sample code below is basically the vanilla version of index.php included with Slim, but I’ve stripped out the original comments and examples included with it and added my own for detail more relevant to this tutorial):
  9. The line with Slim::render(‘home.html’) will be looking for a file called home.html in a directory called ‘templates,’ which should live in the root of the project. Go ahead and create the templates directory in your project root, and create a file called home.html in there. I’m not going to go into much detail about Twig, but just for clarification – for variable output one would use double curly braces {{ likeThis }}, and for programmatic control structures, such as if and for, one would use {% if %}. The contents of home.html are as follows:
    Simple Twig View Example

The output of that page is going to look something like the following screenshot:

Output from Slim/Twig example.

Output from Slim/Twig example.

So, that’s pretty simple and hopefully I didn’t get too complicated with my outline here. Here’s a zip of the SlimTwigExample. Please fire away with comments/questions or suggestions on whatever and/or how to make this tutorial flow better – it’s the first one I’ve ever written.

9 thoughts on “Tutorial: Slim PHP View Templates

  1. vanting says:

    I just created a project named RedSlim which is a pre-configured development environemnt using Slim, Twig, Redbean and Bootstrap. The code is hosted in github (https://github.com/vanting/RedSlim), and you can deploy it to the pagodabox cloud (the slim hosting) within a minute.

  2. your tutorial was perfect and gave me the nudge i needed to use this for my next project instead of jumping into node. good stuff man, ill drop you a link next time i get a chance

    • will says:

      Sorry – I wrote this a while ago and for some reason opted to post screenshots instead of using code blocks. But… typing it out instead of copy/pasting helps with retention… right? lol.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>