Installing Smarty

A Quick & Dirty version...

Smarty is one of the most important template engines in the PHP development world. It provides a rather easy and "clean" usage and setting it up is not hard, dispite of some quick-install and Windows tutorials I saw in my early documentation seeking moments.

Before anything, I'll assume that you are not looking for a tutorial on Smarty, this will cover only how to get it running. Well, here is a simple list of how to set Smarty up in your development environment.

  1. Download Smarty It's free, lightweight and fast. Also, it's the only(?) real template engine available for PHP.
  2. To be able to configure Smarty, you'll have to add the downloaded Smarty project to your web project (I assume you have it in a folder), and load it, nothing fancy!

    Take for instance, the case where I have Smarty in my server in lib\smarty. If I use this line in my entry point file(usually, my index.php file):

       define("PATH_BASE", dirname(__FILE__));
       define("DS", DIRECTORY_SEPARATOR);
      

    Then, I'll be able to load Smarty with a really simple call:

       require_once( PATH_BASE . DS . "lib" . DS . "smarty" . DS . "Smarty.class.php" );
       $smartyObject = new Smarty();
      

    On a side note, I received a comment some time ago about the DIRECTORY_SEPARATOR constant definition not working. I've looked upon the problem and it seems that the constant is part of the PHP core for Directories.

  3. Now that Smarty has been set up, we need to point out the directories where to look for and store generated files. This can be achieved with 3 simple instructions:

    I assume that PATH_TEMPLATES is the path to your templates folder. That's it! With a few lines of code, you have Smarty up and running! All you have to do now is just assign variables and load the template. Like this:

       $smartyObject->assign("message", "Hello world!");
       $smartyObject->display("hello.tpl");
      

No comments:

Post a Comment

Commenting is allowed!