Setup Smarty Templates

From GLMWiki
Jump to: navigation, search

Install Smarty Templates

Download Smarty - Latest Stable Release - (tar.gz)

http://www.smarty.net/download

Uncompress archive

tar -zxf Smarty-3.1.14.tar.gz  (or whatever the file name is)

Install Library Files

sudo mkdir /usr/local/lib/php
sudo mkdir /usr/local/lib/php/Smarty
sudo cp -r {unarchived smarty directory}/libs/* /usr/local/lib/php/Smarty/.
sudo chmod -R 644 /usr/local/lib/php
sudo find /usr/local/lib/php -type d -exec chmod 755 '{}' \; 

Get Smarty Documentation - save in a convenient place

http://www.smarty.net/documentation


Setup Smarty for a Web Site

Add Smarty Directories

cd {web site root}
mkdir smarty
mkdir smarty/templates_c
mkdir smarty/cache
mkdir smarty/configs

Set permissions

sudo chown www-data.www-data smarty/templates_c
sudo chown www-data.www-data smarty/cache
sudo chmod 2770 smarty/templates_c
sudo chmod 2770 smarty/cache

Setup templates directory

mkdir {directory where templates will go}

In your code make sure you have something like the following...

 // Load Smarty class and create instance
require('/usr/local/lib/php/Smarty/Smarty.class.php');
$smarty = new Smarty();
 // Set required Smarty paths
$smarty->setTemplateDir(BASE_PATH.'/templates');
$smarty->setCompileDir(BASE_PATH.'/smarty/templates_c');
$smarty->setCacheDir(BASE_PATH.'/smarty/cache');
$smarty->setConfigDir(BASE_PATH.'/smarty/configs');

Adding parameters for Smarty Templates

Add individual output parameters to Smarty instance

$smarty->assign('name', 'Ned'); 

Assign an array of parameters under a common name

$smarty->assign('contactsArray', 
 array(
  'fax' => '555-222-9876',
  'email' => 'zaphod@slartibartfast.example.com',
  'phone' => array(
   'home' => '555-444-3333',
   'cell' => '555-111-1234'
  )
 )
);

Add parameters in an object

$Contact = (object) array(
 'fax' => '555-222-9876',
 'email' => 'zaphod@slartibartfast.example.com',
 'phone' => (object) array(
  'home' => '555-444-3333',
  'cell' => '555-111-1234'
 )
);

Then assign to the Smarty template

$smarty->assign('contactObj', $Contact);