Skip to content
defvayne23 edited this page Jan 4, 2012 · 3 revisions

Below is the easiest way to setup Smarty without having to rewrite any of your code (besides views).

1. Download

http://www.smarty.net/download

2. Extract

Extract Smarty libs directory to app/core/. Rename folder to smarty. Path to Smarty should now be app/core/smarty/Smarty.class.php.

Optional: Rename folder to include version number. Ex: app/core/smarty_3.1.7/.

3. Setup Folder

Create folder app/views/compiled with write access.

4. Initiate Smarty

Open app/core/load.php.

Add to the __construct the follow:

define('SMARTY_DIR', $this->root.'app/core/smarty/');
require_once('Smarty.class.php');

$this->smarty = new Smarty();
$this->smarty->template_dir = $this->root.'app/views/';
$this->smarty->compile_dir = $this->root.'app/views/compiled/';
$this->smarty->plugins_dir = $this->root.'app/helpers/';
$this->smarty->use_sub_dirs = true;

Can configure Smarty more how you please. More Info

5. Load Templates

Replace the view function with:

public function view($sTemplate, $aAssign = array(), $sReturn = false) {
  if($this->smarty->templateExists($sTemplate)) {
    foreach($aAssign as $sName => $sValue) {
      $this->smarty->assign($sName, $sValue);
    }
    
    if($sReturn == true) {
      return $this->smarty->fetch($sTemplate);
    } else {
      $this->smarty->display($sTemplate);
    }
  } else {
    return false;
  }
}

6. Load Plugins

Replace the helper function with:

public function helper($sHelper, $sType = 'function') {
  if(!function_exists($sHelper.'_helper')) {
    if(is_file($this->root.'app/helpers/'.$sHelper.'.php')) {
      include($this->root.'app/helpers/'.$sHelper.'.php');
      $this->smarty->registerPlugin($sType, $sHelper, $sHelper);
      
      if(!function_exists($sHelper.'_helper')) {
        return false;
      }
    } else {
      return false;
    }
  }
  
  return true;
}

All set!

Smarty should now be setup!

WARNING!!!

Usage of helpers with Smarty will modify how interaction with the function should take place. Function name does not need to change, but arguments will. Learn More

Clone this wiki locally