Integrating Smarty with the Zend Framework
/Travello/View/Smarty.php
<?php
classTravello_View_SmartyextendsZend_View_Abstract
{
private$_smarty=false;
publicfunction__construct($data=array())
{
parent::__construct($data);
$config=Zend::registry('config');
$this->_smarty=newSmarty();
$this->_smarty->caching=$config->getSetting('smarty','caching');
$this->_smarty->cache_lifetime=$config->getSetting('smarty','cache_lifetime');
$this->_smarty->template_dir=$config->getSetting('smarty','template_dir');
$this->_smarty->compile_dir=$config->getSetting('smarty','compile_dir');
$this->_smarty->config_dir=$config->getSetting('smarty','config_dir');
$this->_smarty->cache_dir=$config->getSetting('smarty','cache_dir');
}
protectedfunction_run($template)
{
$this->_smarty->display($template);
}
publicfunctionassign($var)
{
if(is_string($var))
{
$value=@func_get_arg(1);
$this->_smarty->assign($var,$value);
}
elseif(is_array($var))
{
foreach($varas$key=>$value)
{
$this->_smarty->assign($key,$value);
}
}
else
{
thrownewZend_View_Exception('assign()expectsastringorarray,got'.gettype($var));
}
}
publicfunctionescape($var)
{
if(is_string($var))
{
returnparent::escape($var);
}
elseif(is_array($var))
{
foreach($varas$key=>$val)
{
$var[$key]=$this->escape($val);
}
return$var;
}
else
{
return$var;
}
}
/*
Printtheoutput
Thenextmethodoutput()isawrapperontherender()methodfromZend_View_Abstract.Itjustsetssomeheadersbeforeprintingtheoutput.
*/
publicfunctionoutput($name)
{
header("Expires:Mon,26Jul199705:00:00GMT");
header("Cache-Control:no-cache");
header("Pragma:no-cache");
header("Cache-Control:post-check=0,pre-check=0",FALSE);
printparent::render($name);
}
/*
UseSmartycaching
ThelasttwomethodswerecreatedtosimplyintegratetheSmartycachingmechanismintheViewclass.Withthefirstoneyoucancheckforcachedtemplateandwiththesecondoneyoucansetthecachingonorof.
*/
publicfunctionisCached($template)
{
if($this->_smarty->is_cached($template))
{
returntrue;
}
returnfalse;
}
publicfunctionsetCaching($caching)
{
$this->_smarty->caching=$caching;
}
}
/*
Howtousetheclass
Theusageofthisclassisquitesimple.Inyourbootstrapfileyoucaninitializeitlikethis.The$viewConfigisusedtosetuptheZend_Viewpaths.Aftercreationtheobjectisregisteredintheobjectstoreforlaterusage.
1.initialize
$viewConfig=array();
$viewConfig['scriptPath']=$config->getSetting('framework','view_dir');
$view=newTravello_View_Smarty($viewConfig);
Zend::register('view',$view);
2.Withinyourcontrolleranactionmethodcouldlooklikethis.
publicfunctionindexAction()
{
$temp_file='homepage.htm';
$view=Zend::registry('view');
if(false===$view->isCached($temp_file))
{
$vars=array();
$vars['title']='Page<title>';
$vars['text']='Atextisatext&atextisatext.';
$vars['numbers'][0]=12.9;
$vars['numbers'][1]=29;
$vars['numbers'][2]=78;
$vars=$view->escape($vars);
$view->assign($vars);
}
$view->output($temp_file);
}
Conclusion
Thatwasbasicallyit.Ihaveacoupleofideashowtoextendthissolution,butitalreadydoestheworkforme.Anycommentsarewelcome

*/
?>
本文介绍如何将Smarty模板引擎整合到Zend框架中,通过自定义类实现Smarty配置及缓存机制,并展示了初始化过程及控制器中使用示例。
143

被折叠的 条评论
为什么被折叠?



