The “Almost” Perfect Smarty Setup

So, you’re looking for the “almost” perfect startup tutorial for the uber-famous Smarty template engine written with PHP? Or, are you just looking for a good straight-up to the point tutorial on using Smarty? Well, either way HERE IT IS!!

For the basic setup check out this tutorial here. Once you have Smarty running on your server then take a seat and follow me.

Your folder/directory structure should be like the following:

/root/
Example: /var/www/vhosts/yoursite.com/httpdocs/
/root/cache/ - Chmod 775
Example: /var/www/vhosts/yoursite.com/httpdocs/cache/
/root/configs/
Example: /var/www/vhosts/yoursite.com/httpdocs/configs/
/root/templates/
Example: /var/www/vhosts/yoursite.com/httpdocs/templates/
/root/templates/themeName/ - Your Theme’s Folder
Example: /var/www/vhosts/yoursite.com/httpdocs/templates/themeName/
/root/templates/themeName/index.tpl - Your Main Theme File
Example: /var/www/vhosts/yoursite.com/httpdocs/templates/themeName/index.tpl
/root/templates_c/ - Chmod 775
Example: /var/www/vhosts/yoursite.com/httpdocs/templates_c/
/root/smarty/
Example: /var/www/vhosts/yoursite.com/httpdocs/smarty/
/root/smarty/internals/
Example: /var/www/vhosts/yoursite.com/httpdocs/smarty/internals/
/root/smarty/plugins/
Example: /var/www/vhosts/yoursite.com/httpdocs/smarty/plugins/
/root/smarty/Config_File.class.php
Example: /var/www/vhosts/yoursite.com/httpdocs/smarty/Config_File.class.php
/root/smarty/debug.tpl
Example: /var/www/vhosts/yoursite.com/httpdocs/smarty/debug.tpl
/root/smarty/Smarty.class.php
Example: /var/www/vhosts/yoursite.com/httpdocs/smarty/Smarty.class.php
/root/smarty/Smarty_Compiler.class.php
Example: /var/www/vhosts/yoursite.com/httpdocs/smarty/Smarty_Compiler.class.php

For secure template placement:

Example:
/var/www/vhosts/yoursite.com/httpdocs/ = The web root.
/var/www/vhosts/yoursite.com/templates/
/var/www/vhosts/yoursite.com/templates_c/

To protect your templates from being viewed as-is I like to utilize a pre-existing security script, with an .htaccess file. Inside the folder/directory of your template file you make an .htaccess file with the following contents:

<Files ~ “\.tpl$”>
Order allow,deny
Deny from all
</Files>

For the example above, you would place an .htaccess file with the above contents inside the folder: /root/templates/themeName/

/root/templates/themeName/.htaccess

Now, to setup the PHP code which displays the page I’ll demonstrate a simple example:
The PHP code: index.php

<?php
// Smarty initializers and code (start)
define(’SMARTY_DIR’, ‘/var/www/vhosts/yoursite.com/httpdocs/smarty/’);
// or if you’re on a shared: /home/accountName/public_html/smarty/
require_once(SMARTY_DIR . ‘Smarty.class.php’);// or die(’Could not load Smarty class.’);
class Kiler_Smarty extends Smarty { // PHP4-5 compatible;
function Kiler_Smarty(){
// Class Constructor.
// These automatically get set with each new instance.
$this->Smarty();
$this->template_dir = ‘/var/www/vhosts/yoursite.com/httpdocs/templates/’;
// or if you’re on a shared: /home/accountName/public_html/templates/
$this->compile_dir = ‘/var/www/vhosts/yoursite.com/httpdocs/templates_c/’;
// or if you’re on shared: /home/accountName/public_html/templates_c/
$this->config_dir = ‘/var/www/vhosts/yoursite.com/httpdocs/configs/’;
// or if you’re on shared: /home/accountName/public_html/configs/
$this->cache_dir = ‘/var/www/vhosts/yoursite.com/httpdocs/cache/’;
// or if you’re on shared: /home/accountName/public_html/cache/
// you can also do like:
$this->assign(’app_name’, ‘Your Website System Name; beta build 110107′);
}
}
$smarty = new Kiler_Smarty();
$smarty->display(’themeName/index.tpl);
?>

Inside the main theme file [/templates/themeName/index.tpl] It could look like this:

<html><head><title>My Website</title></head><body>Hello from Smarty version: {$smarty.version}! This is {$app_name}!</body></html>

Although, it is advisable to place your template files outside of the web root on non-development servers if the server is under constant exposure.

You would need to move the templates folder and the templates_c folder outside of the web root, like so:

/var/www/vhosts/yoursite.com/templates/
// or if you’re on shared: /home/accountName/templates/
/var/www/vhosts/yoursite.com/templates_c/
// or if you’re on shared: /home/accountName/templates_c/

Then be sure to change the PHP code lines reading:

$this->template_dir = ‘/var/www/vhosts/yoursite.com/httpdocs/templates/’;
// or if you’re on a shared: /home/accountName/public_html/templates/
$this->compile_dir = ‘/var/www/vhosts/yoursite.com/httpdocs/templates_c/’;
// or if you’re on shared: /home/accountName/public_html/templates_c/

Change It To:

$this->template_dir = ‘/var/www/vhosts/yoursite.com/templates/’;
// or if you’re on a shared: /home/accountName/templates/
$this->compile_dir = ‘/var/www/vhosts/yoursite.com/templates_c/’;
// or if you’re on shared: /home/accountName/templates_c/

Of course, you’ll also have to handle the file permissions as well.

Do this process of setting up Smarty and the template folder/file structure over and over again until you can memorize the process then the rest of using Smarty will be a breeze! We will be posting more Smarty tutorials in the very near future, so stay tuned!

Leave a Reply

You must be logged in to post a comment.