Loading Smarty on Demand in Zend Framework

My last projects are all build with Zend Framework and almost all of my previous projects were created with custom framework using Smarty. I wanted to implement Smarty in Zend Framework and I search through the web to find proper resolution for my problem. I found this article and another one inspired by the first. I tested both approaches but they were not what I was looking for. Inspired by both articles I created my own Smarty – Zend framework implementation.

First creating custom Controller:

[code lang="php"]
_helper->viewRenderer->setNoRender(true);    
    
    }
    
    /**
     *  LOADING SMARTY ON DEMAND
     * 
     */
    public static function LoadSmarty()
    {
        
    include ('smarty/Smarty.class.php');    

    $smarty = new Smarty();
    
    $smarty->template_dir =  template dir ;
    $smarty->compile_dir  = compile dir;
    $smarty->config_dir   = config dir 
    $smarty->cache_dir    = cache dir;
    $smarty->plugins_dir = and plugins ;
    
    
    
    /**
     *  SMARTY CONFIGURATION
     * 
     */
    $smarty->debugging = false;
    $smarty->force_compile = true;
    $smarty->compile_check = true;

    
    /**
     *  SMARTY PLUGINS 
     * 
     */ 
    //include various smarty plugins 
    
    Zend_Registry::set('smarty', $smarty);
    
            
    }
    
}


?>
[/code]

This is example of the custom Base Controller, if we want to include Smarty in particular Controller we extend Base Controller

[code lang="php"]


class IndexController extends Base_Controller { public function indexAction() {

$this->IncludeSmarty();
$smarty = Zend_Registry::get('smarty');
$smarty->assign('title', 'Test Page index');


/** DEFINE PAGE TEMPLATES */
$smarty->display('includes/header.tpl');
$smarty->display('demo.tpl');
$smarty->display('includes/footer.tpl');


}

/** * FUNCTION FOR LOADING SMARTY * * @return smarty object */

public static function IncludeSmarty()
{       
Base_Controller::LoadSmarty();
}

} [/code]

If we are building some pages, that doesn’t require Smarty or we want it to use Zend_View component the only difference is that the custom controller extends the default Zend_Controller

[code lang="php"]


class IndexController extends Zend_Controller_Action // ALL LOGIC HERE [/code]

With this approach Smarty will be used only when it is needed and.If we completely skip Zend_View in our custom framework we can avoid the frustrating for me default Zend Framework View directory. structure.

  1. A fantastic read….very literate and informative. Many thanks….where is your RSS button ?

Leave a Reply