Integrating Smarty with the Zend Framework Travello_View_Smarty

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

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


*/
?>
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值