Integrating Smarty with the Zend Framework Travello_View_Smarty

Integrating Smarty with the Zend Framework

/Travello/View/Smarty.php

<? php
class Travello_View_Smarty extends Zend_View_Abstract
{
private $_smarty = false ;

public function __construct( $data = array ())
{
parent
:: __construct( $data );

$config = Zend :: registry( ' config ' );

$this -> _smarty = new Smarty();

$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 ' );
}

protected function _run( $template )
{
$this -> _smarty -> display( $template );
}

public function assign( $var )
{
if ( is_string ( $var ))
{
$value = @ func_get_arg ( 1 );

$this -> _smarty -> assign( $var , $value );
}
elseif ( is_array ( $var ))
{
foreach ( $var as $key => $value )
{
$this -> _smarty -> assign( $key , $value );
}
}
else
{
throw new Zend_View_Exception( ' assign()expectsastringorarray,got ' . gettype ( $var ));
}
}

public function escape( $var )
{
if ( is_string ( $var ))
{
return parent :: escape( $var );
}
elseif ( is_array ( $var ))
{
foreach ( $var as $key => $val )
{
$var [ $key ] = $this -> escape( $val );
}

return $var ;
}
else
{
return $var ;
}
}

/*
Printtheoutput
Thenextmethodoutput()isawrapperontherender()methodfromZend_View_Abstract.Itjustsetssomeheadersbeforeprintingtheoutput.
*/
public function output( $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 );

print parent :: render( $name );
}

/*
UseSmartycaching
ThelasttwomethodswerecreatedtosimplyintegratetheSmartycachingmechanismintheViewclass.Withthefirstoneyoucancheckforcachedtemplateandwiththesecondoneyoucansetthecachingonorof.
*/
public function isCached( $template )
{
if ( $this -> _smarty -> is_cached( $template ))
{
return true ;
}

return false ;
}

public function setCaching( $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


*/
?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值