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"); //注册函数

原文地址:https://www.cnblogs.com/legend-song/p/3441032.html