Blueprint PHP Framework Tutorial – Part 7 (Templating)

If you’ve followed along this far, you should now have a fairly good working knowledge of controllers, models and views. You may be wondering how the view actually translates into actual output to the page. Well, thats what we’re going to look at in this tutorial. On the way we’ll also look at the Page class and see how we can use that in our templates.

Template files are found in the /out/ folder. The View method responsible for calling the template file is render(). This is almost always the last thing that is called in a controller action like so:

As we saw in the last tutorial, $vars is an array of information that we want to pass into the template. Lets take a look at the View’s render method:

This very simple method extracts the array keys from the $vars array and assigns them as variables within the scope of this method. It then includes the requested template file and processes it before returning the output back to the controller which invariably will just echo out the result.

Just before the template is included a file called functions.php is loaded (if it exists). The file distributed with the framework looks like this:

Remember that we are within the scope of the View->render() method. Because the view’s container was set in the controller via the setContainer() method, we can access the config object and the page object (we’ll talk some more about this in a moment). Once again, because we are within the scope of the View->render() method we set the $base variable to the location of /out/ (VIEW_DIR if you haven’t changed the /etc/constants.php file). This will be helpful for including sub-files in our template as trying to include ../somefile will not include what you might expect it to.

This file sets some closures that serve as wrappers for the objects we want to expose in the template files. In our template files we can call:

to echo out the the full URL to our public root. This effectively maps to:

Which is less scary to a designer who might want to just echo out the website’s address? These closures can be very powerful and you are limited only by your imagination as to how you can use them to make templating easier for the designer. You could of course swap all this out and use Smarty, Twig or another dedicated templating engine to handle all this sort of stuff. Just extend the View to load the third party classes and away you go.

Right then, as promised, lets take a look at the Page class. You can view a copy here on github. The idea behind the Page class is to make it easy to set certain element’s values that are often used in an HTML web page. If you are using the framework for a website, I would suggest loading it in the bootstrap script. This will set the default CSS files, and JS files, H1, title, meta-description etc from the /etc/config.php file. You can then just overwrite with a different H1 for example in the controller action you are using:

Don’t forget the pull the dependency out in the setContainer method of the controller:

Then in the template (if you’re using the unadulterated functions.php file) you can call:

It couldn’t be easier. You can follow the same process to set custom CSS files and JavaScript files for certain controller actions but not others.

Hopefully you have a good working knowledge of everything you need to build simple websites using the Blueprint PHP Framework now. The next few tutorials will concentrate on the Database class, dynamic form building and how we can use caching in our application. The last tutorial I will do will be about the best way of extending the framework to best suit your needs. Go forth and build…

Leave a Reply