ci中使用smarty

<p>d</p>

 
  
<? php defined ( ' BASEPATH ' ) or die ( ' Access restricted! ' );
/*
* 作者:晋勇
*/
require (APPPATH . ' libraries/smarty/Smarty.class.php ' );
class Cismarty extends Smarty
{
public $ext = ' tpl ' ;
public $dir = '' ;
public $layout = ' layout/main ' ;
/* *
* 构造函数
*
* @access public
* @param array/string $template_dir
* @return obj smarty obj
*/
public function __construct( $template_dir = '' , $compile_dir = '' , $config_dir = '' , $cache_dir = '' )
{
$this -> Smarty();
if ( is_array ( $template_dir )){
foreach ( $template_dir as $key => $value ) {
$this -> $key = $value ;
}
}
else {
$this -> cache_dir = $cache_dir ? $cache_dir : BASEPATH . ' cache/ ' ;
$this -> template_dir = $template_dir ? $template_dir : APPPATH . ' views/ ' ;
$this -> compile_dir = $compile_dir ? $compile_dir : APPPATH . ' tpl_c/ ' ;
$this -> compile_check = true ;
$this -> debugging = false ; // debug模式
$this -> caching = 0 ; // 启用缓存
$this -> cache_lefetime = 6000 ; // 缓存时间s
$this -> left_delimiter = ' <!--{ ' ;
$this -> right_delimiter = ' }--> ' ;
}
}

/* *
* 显示输出页面
* @access public
* @return string
*/
public function show( $tpl ){
$this -> assign( ' jsFiles ' , $this -> getJsHtml());
$this -> assign( ' jsFiles1 ' , $this -> getJsHtml( 1 ));
$this -> assign( ' LAYOUT ' , $this -> dir ? $this -> dir . ' / ' . $tpl . ' . ' . $this -> ext : $tpl . ' . ' . $this -> ext);
$this -> display( $this -> layout . ' . ' . $this -> ext);
}
/* *
* 添加一个CSS文件包含
* @param string $file 文件名
* @access public
* @return void
*/
public function addCss( $file ) {
if ( strpos ( $file , ' / ' ) == false ) {
$file = config_item( ' css ' ) . $file ;
}
$GLOBALS [ ' cssFiles ' ][ $file ] = $file ;
}
/* *
* 添加一个JS文件包含
* @param string $file 文件名
* @access public
* @return void
*/
public function addJs( $file , $btm = NULL ) {
if ( strpos ( $file , ' / ' ) == false ) {
$file = config_item( ' js ' ) . $file ;
}
if ( $btm == NULL ) {
$GLOBALS [ ' jsfiles ' ][ $file ] = $file ;
}
else {
$GLOBALS [ ' jsbtmfiles ' ][ $file ] = $file ;
}
}
/* *
* 取生成的包含JS HTML
* @access public
* @return string
*/
public function getJsHtml( $btm = NULL ) {
$html = '' ;
if ( ! $GLOBALS [ ' jsfiles ' ]) {
return ;
}
$jsFile = $btm ? ' jsbtmfiles ' : ' jsfiles ' ;
if (@ $GLOBALS [ $jsFile ]) {
foreach ( $GLOBALS [ $jsFile ] as $value ) {
$html .= $this -> jsInclude( $value , true ) . " \n " ;
}
return $html ;
}
else {
return ;
}
}
/* *
* 添加html标签
* @param string $tag 标签名
* @param mixed $attribute 属性
* @param string $content 内容
* @return string
*/
public function addTag( $tag , $attribute = NULL , $content = NULL ) {
$this -> js();
$html = '' ;
$tag = strtolower ( $tag );
$html .= ' < ' . $tag ;
if ( $attribute != NULL ) {
if ( is_array ( $attribute )) {
foreach ( $attribute as $key => $value ) {
$html .= ' ' . strtolower ( $key ) . ' =" ' . $value . ' " ' ;
}
}
else {
$html .= ' ' . $attribute ;
}
}
if ( $content ) {
$html .= ' > ' . $content . ' </ ' . $tag . ' > ' ;
}
else {
$html .= ' /> ' ;
}
$this -> output .= $html ;
return $html ;
}
/* *
* 添加html文本
* @param string $content 内容
* @return string
*/
public function addText( $content ) {
$this -> js();
$content = htmlentities ( $content );
$this -> output .= $content ;
return $content ;
}
/* *
* 添加js代码
* @param string $jscode js代码
* @param bool $end 是否关闭js 代码块
* @return void
*/
public function js( $jscode = NULL , $end = false ) {
if ( ! $this -> inJsArea && $jscode ) {
$this -> output .= " \n<mce:script language='JavaScript' type='text/javascript'><!--
\n//<!--[CDATA[\n
" ;
$this -> inJsArea = true ;
}
if ( $jscode == NULL && $this -> inJsArea == true ) {
$this -> output .= " \n//]]-->\n
// --></mce:script>\n
" ;
$this -> inJsArea = false ;
}
else {
$this -> output .= " \t $jscode \n " ;
if ( $end ) {
$this -> js();
}
}
return ;
}
/* *
* 添加js提示代码
* @param string $message 提示内容
* @param bool $end 是否关闭js 代码块
* @return void
*/
public function jsAlert( $message , $end = false ) {
$this -> js( ' alert(" ' . strtr ( $message , ' " ' , ' \\" ' ) . ' "); ' , $end );
}
/* *
* 添加js文件包含
* @param string $fileName 文件名
* @param bool $defer 是否添加defer标记
* @return string
*/
public function jsInclude( $fileName , $return = false , $defer = false ) {
if ( ! $return ) {
$this -> js();
}
$html = ' <mce:script language="JavaScript" type="text/javascript" src=" '
. $fileName . ' " mce_src=" '
. $fileName . ' " ' . ( ( $defer ) ? ' defer ' : '' )
. ' ></mce:script> ' ;
if ( ! $return ) {
$this -> output .= $html ;
}
else {
return $html ;
}
}
/* *
* 添加css文件包含
* @param string $fileName 文件名
* @return string
*/
public function cssInclude( $fileName , $return = false ) {
if ( ! $return ) {
$this -> js();
}
$html = ' <LINK href=" ' . $fileName . ' " mce_href=" ' . $fileName . ' " rel=stylesheet> ' . chr ( 13 );
if ( ! $return ) {
$this -> output .= $html ;
}
else {
return $html ;
}
}
/* *
* 输出html内容
* @param bool $print 是否直接输出,可选,默认返回
* @return void
*/
public function output( $print = false ) {
$this -> js();
if ( $print ) {
echo $this -> output;
$this -> output = '' ;
return ;
}
else {
$output = $this -> output;
$this -> output = '' ;
return $output ;
}
}
}
?>

dd

打开配置文件(config/autoload.php)

$autoload['libraries'] = array('cismarty');//加入cismarty

这样在所有应用中,都可以使用$this->cismarty来调用smarty实例了。

Cismarty类已经在原有的smarty::display基础上做了小的改动,已经支持layout功能

默认加载view/layout/main.tpl作为布局文件。

可以通过$this->cismarty->layout = "xxxx"来更改

来源 http://blog.csdn.net/a600423444/archive/2010/09/18/5892726.aspx

转载于:https://www.cnblogs.com/xwblog/archive/2011/05/02/2034263.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值