Setting Up
There are a few different ways of setting Smarty up. If you are in control of your webserver, go ahead with the Regular Setup. If you are working with a webserver controlled by another party you should probably follow the Limited Privileges Setup. And if you have done this before, or are fearless, go ahead and start with the Advanced Setup.
TopRegular Setup
If you are running your webserver locally or if you have the ability to modify your php.ini file: follow the steps below accordingly. If not, you can still use Smarty, but you will have to skip to the Limited Privileges Installation steps in the next section.
- Download Smarty from its official website
- Extract the
libsfolder from your archive to your web serverlibdirectory*- Linux/Unix -
/usr/local/lib/Smarty-v.e.r./ - Windows -
c:\webroot\libs\Smarty-v.e.r.\
- Linux/Unix -
- Change your
php.inifile and add Smarty to yourinclude_path
php.ini file
;;;;;;;;;;;;;;;;;;;;;;;;; ; Paths and Directories ; ;;;;;;;;;;;;;;;;;;;;;;;;; ; UNIX: "/path1:/path2" ;include_path = ".:/usr/share/php:/usr/local/lib/Smarty-v.e.r/libs/" ; ; Windows: "\path1;\path2" include_path = ".;c:\php\includes;c:\webroot\libs\Smarty-v.e.r\libs\"
Now you're ready to create a Smarty object
Test filetest.php
<?php
require_once('Smarty.class.php');
$smarty = new Smarty();
?>
Run this script. If you have no errors, congratulations! If you run this script and encounter an error, double check your include paths using phpinfo();. If this is correct then you may need to follow the Limited Privileges Smarty Installation setup.
Limited Privileges Setup
This section is intended for people that have limited privelages to their web server. Skip these steps if you were able to follow the regular Smarty installation successfully.
- Download Smarty from its official website
- Extract the
libsfolder from your archive to your web serverlibdirectory*- Linux/Unix -
/usr/local/lib/Smarty-v.e.r./ - Windows -
c:\webroot\libs\Smarty-v.e.r.\
- Linux/Unix -
Now you're ready to create a Smarty object:
Test filetest.php
<?php
// NOTE: Choose the appropriate style based on
// your webserver machine's Operating System)
// unix or linux style (note capital 'S')
// define('SMARTY_DIR', '/usr/local/lib/Smarty-v.e.r/libs/');
// windows style
define('SMARTY_DIR', 'c:\webroot\libs\Smarty-v.e.r\libs\');
require_once(SMARTY_DIR . 'Smarty.class.php');
$smarty = new Smarty();
?>
Open up this script in a browser. Congratulations, you have created your first Smarty object! There are a few more steps we need to complete before Smarty can begin to unleash its full potential.
TopAdvanced Setup
This part assumes that you already have Smarty downloaded, installed, and running. If you haven't done this yet, you need to visit the appropriate setup section above.
The way you were shown to setup Smarty as aforementioned is fine and working. However, do you really want to have to instantiate a new Smarty object every time you create a new template file? No way! Let's go over the advanced setup procedure.
I like to create an init.php file in a folder called lib under my web site's root containing all of my site's initializations. This usually includes things like the site's name, database information, and a Smarty object instantiation.
lib/init.php file
<?php
// Include some commonly used custom functions
require_once('functions.php');
// Database information
$db = array (
'host' => 'localhost',
'user' => 'user',
'pass' => 'pass',
'name' => 'db_name');
// Error reporting, site root
error_reporting(E_ALL);
$SITE_ROOT = '/wwwroot/example_site/';
// Extend the Smarty object
class Smarty_DefaultSite extends Smarty {
function Smarty_DefaultSite()
{
// Class Constructor.
$this->Smarty();
// Set Smarty directories
$this->template_dir = $SITE_ROOT . 'templates/';
$this->compile_dir = $SITE_ROOT . 'templates_c/';
$this->config_dir = $SITE_ROOT . 'configs/';
$this->cache_dir = $SITE_ROOT . 'cache/';
// Assign our application's name
$this->assign('app_name', 'Default Site');
// Enable debugging via URL
// This is recommended for development phases only
$smarty->debugging_ctrl = 'URL;
}
}
// I tend to use Smarty on every page if I am going to use it at all
// So let's write less code and instantiate our Smarty object now
$smarty = new Smarty_DefaultSite();
?>
That may have been a lot to swallow at once. If you are unfamiliar with the PHP portions of this file you should reference the official PHP online documentation. Let's review the Smarty-related code.
Smarty is nice in that it allows us to extend its class. This makes declaring a Smarty object easier to understand and more pleasing to read.
We then create a function with the same name of the class (a constructor). This special function will be called every time an object of the Smarty_DefaultSite class is created.
Next we have some simple directory declarations. Here we can also assign a global site or application name to be easily included in the title of every page.
Next we enable the debugging. This is an especially important feature used in development. Smarty comes packaged with a debugging console that will let you know every piece of data (from our PHP pages that call the template) that has been assigned to Smarty variables.
To use it, put a parameter in the URL called SMARTY_DEBUG.
http://www.defaultsite.com/index.php?SMARTY_DEBUG
Or this
http://www.defaultsite.com/show.php?id=25&SMARTY_DEBUG
After we have created this file we can simply use require_once('lib/init.php') on every PHP page that is created. We're now free to assign variables and display Smarty templates at will!