smarty模板配置代码详细说明及如何注册自己的smarty函数
下面为smarty模板的配置文件,smarty配置的详细说明以及如何注册自己所需要的smarty函数 config.inc.php
<?php
/**
* Smarty 调用
* www.dafei.net
*/
/*** 新建 Smarty 对象 */
require_once (dirname(__FILE__).'/Smarty/libs/Smarty.class.php');
$smarty = new Smarty;
//$Smarty->caching = true; //决定是否缓存文件执行是生成的文件
//$Smarty->cache = true; //开启缓存
//$Smarty->cache_lifetime = 300; //缓存时间 单位秒
$smarty->compile_check = true; //每次执行时查看模板的内容是否改变
$smarty->debugging = false; //debugging 控制台 这是一个开关参数 如打开将显示 Smarty 变量和运行状态的调试窗口
/** 下面这几个目录可以根据自己的情况设置 **/
$smarty->template_dir = dirname(__FILE__).'/../templates'; //模板目录
$smarty->config_dir = dirname(__FILE__).'/../libs/config'; //默认的 config 文件目录名 默认为 ./configs
$smarty->compile_dir = dirname(__FILE__).'/../libs/templates_c'; //是 Smarty 默认的编译模板目录名 默认为 ./templates_c
$smarty->cache_dir = dirname(__FILE__).'/../libs/cache'; //默认的模板缓存目录名 默认为 ./cache
$smarty->left_delimiter = '<!--{'; //模板定界符 默认 {}
$smarty->right_delimiter = '}-->';
/******************************* 注册自己需要的smarty函数 ********************************/
//截取字符串 调用 { $str|smarty_gbstr:20:"..." }
function smarty_gbstr( $str, $length = -1, $s = "" )
{
$temp = strip_tags( $str );
if( ord( $temp[$length-1] ) > 128 ) {
for( $i = $j = 0; $i < strlen( $temp ) && $i < $length; $i++ ) {
if( $i == $length -1 && ord( $temp[$i] ) > 128 )
$length--; // 如果最后一字节是中文,使截取长度减一以免截断
if( ord( $temp[$i] ) > 128 )
$i++;
}
}
if( strlen( $temp ) > $length ) {
return substr( $temp, 0, $length ).$s;
}
if( $length == -1 )
return $temp;
return substr( $temp, 0, $length );
}
$smarty->register_function("smarty_gbstr", "smarty_gbstr"); //注册函数
最新文章
- Android local.properties 文件读取
- 在xampp中配置dvwa
- 安装Java的IDE Eclipse时出现java.net.SocketException,出现错误Installer failed,show.log
- Visual Studio前端开发工具/扩展
- LoadRunner 脚本学习 -- 动态储存方式和静态储存方式
- 网页绘制图表 Google Charts with JavaScript #2 ....与ASP.NET网页结合 (ClientScriptManager.RegisterStartupScript 方法)
- 【转】Eclipse Java注释模板设置详解
- Google 开源项目风格指南阅读笔记(C++版)
- eclipse怎么恢复原状
- 性能测试工具 wrk 使用教程
- html + css3 demo
- layui(七)——rate组件常见用法总结
- Scala 继承
- nil Nil NULL NSNull 之间的区别
- 图的基本算法(BFS和DFS)
- Android Usb Camera HAL框架
- SQL学习笔记2
- git —— Feature分支
- 好用的eclipse properties插件
- 「Python」socket指南