Get Smarty

Donate

Donate Bitcoin Bitcoin
Paypal

Smarty Icon

You may use the Smarty logo according to the trademark notice.

Smarty Template Engine Smarty Template Engine

For sponsorship, advertising, news or other inquiries, contact us at:

Sites Using Smarty

Largest classified with thousands of ads at Shoppok

Buy cheap eyeglasses from Cheapglasses123.com and save up to 80%.

Buy prescription glasses from www.australiaglasses.com and save.

Cheap Glasses Now On Sale at GlassesPeople.com. Starts At $7.95.

Where to buy discount wedding dresses and cheap smart dresses free shipping - Weddingdresstrend.com

Find Wedding Dresses Online at Canada from Ca-dresses.com

Find your local domestic cleaner

Read these great ice cream maker reviews

Brautkleider auf Topwedding.de

Best Wedding Dresses Australia at AuBridalDresses.com

Find free files to download on allwhatyouwant.net

Advertisement

Extended Setup

This is a continuation of the basic installation, please read that first!

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.

Lets create a new directory /php/includes/guestbook/ and make a new file called setup.php. In our example environment, /php/includes is in our include_path. Be sure you set this up too, or use absolute file paths.

Example 2.10. /php/includes/guestbook/setup.php


<?php

// load Smarty library
require('Smarty.class.php');

// 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 __construct()
   {

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

        parent::__construct();

        $this->setTemplateDir('/web/www.example.com/guestbook/templates/');
        $this->setCompileDir('/web/www.example.com/guestbook/templates_c/');
        $this->setConfigDir('/web/www.example.com/guestbook/configs/');
        $this->setCacheDir('/web/www.example.com/guestbook/cache/');

        $this->caching = Smarty::CACHING_LIFETIME_CURRENT;
        $this->assign('app_name', 'Guest Book');
   }

}
?>

    

Now lets alter the index.php file to use setup.php:

Example 2.11. /web/www.example.com/guestbook/htdocs/index.php


<?php

require('guestbook/setup.php');

$smarty = new Smarty_GuestBook();

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

$smarty->display('index.tpl');
?>

   

Now you see it is quite simple to bring up an instance of Smarty, just use Smarty_GuestBook() which automatically initializes everything for our application.

Comments
by Anton Fedonyuk on Feb 9, 2013 at 6:15
Singleton version for easy using in real projects ( p.s. globals must gie :) )
require_once( CFG_DIR_SMARTY . '/class.Smarty.php' );

class SmartySingleton
{
	static private $instance;
	
	private function __construct() {}
	
	private function __clone() {}
	
	private function __wakeup() {}
	
	static public function instance()
	{
		if( !isset( self::$instance ) )
		{
			$smarty = new Smarty;
			
			$smarty->setTemplateDir( CFG_DIR_ROOT . '/templates/' );
			$smarty->setCompileDir( CFG_DIR_ROOT . '/templates_c/' );
			$smarty->setConfigDir( CFG_DIR_ROOT . '/configs/' );
			$smarty->setCacheDir( CFG_DIR_ROOT . '/cache/' );
			
			$smarty->caching = Smarty::CACHING_LIFETIME_CURRENT;
			$smarty->debugging = false;
			
			#define( 'CFG_DIR_TEMPLATES', $smarty->getTemplateDir(0) );
			
			self::$instance = $smarty;
		};
		return self::$instance;
	}
	
}
In you scripts write ( instance on first at first use )
$smarty = SmartySingleton::instance();
$smarty->assig('bla', 'blabla');
Post a Comment
All comments are moderated. Support questions are ignored, use the forums instead.
Author:
Email: (not shown)
What is 18 plus 9? (Are you human?)

Advertisement

Sponsors [info]

UK Web Hosting @webhost.uk.net
Best Web Hosting @rshosting.com
Web Hosting UK @webhostinguk.com
Unlimited Web Hosting @infrenion.com
App Entwicklung @morphodo.com
GoDaddy Coupon @buy-hosting.net
First Click Internet Marketing @fcinternetmarketing.com