This blog is about C++, CakePHP, Linux, PEAR, PHP, Pyrus, Apache, and MySQL and related topics.

Wednesday, October 28, 2009

How To Install Smarty Template Engine WampServer (Extended Setup)

This article will show how to an extended setup of Smarty with WampServer. This article is a modified version of Extended Setup on Smarty's website and is a continuation of How To Install Smarty Template Engine WampServer (Basic Installation) which is modified from the Basic Installation on Smarty's website.

Step One: Create an 'includes' folder at c:\wamp\bin\php\php5.3.0\includes\

Step Two: Create a folder for the application inside the includes folder. The folder name should be the same as your application name. So if your application is called 'guestbook' then name the folder 'guestbook'.

Step Three: Modify include_path in php.ini to find new 'includes' folder.

Step Four: Create a file called setup.php which will go in c:\wamp\bin\php\php5.3.0\includes\guestbook\

Cut and paste the following code:

//load Smarty library

// The setup.php file is a good place to load
// required application library files, and you
// can do that right here. An example:
// require('guestbook/guestbook.lib.php');

class Smarty_GuestBook extends Smarty {

   function Smarty_GuestBook()

        // Class Constructor.
        // These automatically get set with each new instance.



        $this->template_dir = $path.'guestbook/templates/';
        $this->compile_dir  = $path.'guestbook/templates_c/';
        $this->config_dir   = $path.'guestbook/configs/';
        $this->cache_dir    = $path.'guestbook/cache/';

        $this->caching = true;
        $this->assign('app_name', 'Guest Book');


I modified this code from the setup.php code on the Extended Setup page. The main difference being the $path variable set to $_SERVER['DOCUMENT_ROOT'] to make setting the path easier.

Step Five: Create the directory structure for our application

First we create a folder named after the application in the document root: c:\wamp\www\guestbook\

Then we create four subfolders inside the guestbook folder: cache, configs, templates, templates_c

Step Six: Create the index.tpl file inside c:\wamp\www\guestbook\templates\

Place the following code inside:

{* Smarty *}

Hello {$name}, welcome to Smarty!

Step Seven: Create the index.php file inside c:\wamp\www\guestbook\

Place the following code inside:
$smarty = new Smarty_GuestBook();
Step Eight: Test

Open the browser go to http://localhost/guestbook/index.php

If you see the following:
Hello Ned, welcome to Smarty!

Then congratulations you have installed Smarty on WampServer doing it the Extended Setup way!

No comments:

Post a Comment