Home » Smarty

Installing the Smarty Template Engine

11 July 2008 155 views 2 Comments Popularity: 2% Share/Bookmark

At first you need to download Smarty from http://smarty.php.net/download.php or you can download it directly from http://chapagain.googlecode.com/files/smarty.zip. Extract the zip file. Rename the extracted folder as ‘smarty’. Then, copy the folder ‘smarty’ to your working directory present inside the root directory of your web server.

There are different ways to install Smarty.

1) You can simply supply the absolute path to the Smarty library file:-

require(‘/usr/local/lib/php/Smarty/Smarty.class.php’);

2) You can add library directory to PHP include_path

Copy the libs folder from the Smarty distribution to the mycode folder (for example, C:\apache\htdocs\mycode\libs\).

After this, you need to edit the php.ini file and add include_path = “.;C:\apache\htdocs\ mycode\libs\” where C:\apache\htdocs\mycode\libs\ represents the location of your libs folder.

3) You can set SMARTY_DIR constant manually

define(‘SMARTY_DIR’, ‘/usr/local/lib/php/Smarty/’);

require(SMARTY_DIR . ‘Smarty.class.php’);

4) A slightly more flexible way to setup Smarty is to extend the class and initialize your Smarty environment. So instead of repeatedly setting directory paths, assigning the same vars, etc., we can do that in one place.

I will be explaining the fourth way, i.e. extending the class and initializing Smarty environment.

At first, let us have a look on the directory structure that we must create. Suppose, our work folder is named ‘example’, then our directory structure should be as under:-

example

|

|_ _ _ _ _ _ _ smarty (this directory contains Smarty library files)

|

|_ _ _ _ _ _ _ classes (this directory contains class files that we create)

|

|_ _ _ _ _ _ _ smarty_temp (contains manually created Smarty directories)

|

|_ _ _ _ _ _ _ index.php

As stated at the beginning of this article, the Smarty library files are kept in the ‘smarty’ directory.

Smarty requires four directories which are (by default) named templates, templates_c, configs and cache. Each of these are definable by the Smarty class properties $template_dir, $compile_dir, $config_dir, and $cache_dir respectively. It is highly recommended that you setup a separate set of these directories for each application that will use Smarty. We create these directories inside ‘smarty_temp’ directory.

Now, let’s move to the class file which is kept inside the classes directory.

<?php
/**
* Loading Smarty library
*/
require_once 'smarty/libs/Smarty.class.php';</span></span>

/**
* Connection to Smarty package
*
*/

class ConnectSmarty extends Smarty
{
/**
* This function connects with smarty
* manage paths to smarty directories
* @access public
*/
function ConnectSmarty()
{
$this->Smarty();

$this->template_dir = 'smarty_temp/templates';
$this->config_dir = 'smarty_temp/configs';
$this->cache_dir = 'smarty_temp/cache';
$this->compile_dir = 'smarty_temp/templates_c';
}
}

?>

The above class is called in the index page. An object of the class is created. Then, the Smarty method called ‘assign’ is used to assign values to the template. Through assign method, you can explicitly pass name/value pairs, or associative arrays containing the name/value pairs. After that ‘display’ method is used to display the template.

</span><?php
require_once('classes/ConnectSmarty.class.php');

// create an object of the class included above
$smarty = new ConnectSmarty;

// assign content
$smarty->assign('name','Mukesh Chapagain');
$smarty->assign('address','Kathmandu, Nepal');

// display the content
$smarty->display('index.tpl');

?>

index.tpl is the template file which contains html and smarty code.

</span><html>
<head>
<title> Homepage </title>
</head>

<body>

The author of this tutorial:<br/><br/>
Name: {$name} <br/>
Address: {$address} <br/>

</body>

</html>

Download source code

Note: I have not included Smarty library files in this zip file. You can download the Smarty library files from http://smarty.php.net/download.php or directly from http://chapagain.googlecode.com/files/smarty.zip

Thank You.

From Mukesh Chapagain's Blog | Post Installing the Smarty Template Engine

Related posts:

  1. An Introduction to Smarty Template Engine
  2. File Upload in PEAR and Smarty
  3. Pagination in PEAR and Smarty
  4. Templating in Smarty
  5. Creating selection list, using foreach and section loop in Smarty

2 Comments »

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

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

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.