当前位置:首页> PHP教程> PHP精通
关键字
文章内容
让你的Smarty更聪明
 
 
修改时间:[2012/01/07 02:53]    阅读次数:[937]    发表者:[起缘]
 
一、扩展你的Smarty
1、准备功夫
PHP代码:

function Smarty_function_page ( $params, &$Smarty )
{
    
$href = '#';
    
$space = ' ';

    
$frist = NULL;
    
$last = NULL;

    
$page = 5;

    
extract($params);

    if ( !
$row || $row <= 1 ) return '&nbsp;';

    
$pages = $row;
    
$curr_page = $now ? $now : 1;
    
$offset = 2;
    
$from = $curr_page - $offset;
    
$to = $curr_page + $page - $offset - 1;

    if (
$page > $pages )
    {
        
$from = 1;
        
$to = $pages;
    }
    else
    {
        if (
$from < 1)
        {
            
$to = $curr_page + 1 - $from;
            
$from = 1;
            if ( (
$to - $from ) < $page && ( $to - $from ) < $pages )
            {
                
$to = $page;
            }
        }
        elseif (
$to > $pages )
        {
            
$from = $curr_page - $pages + $to;
            
$to = $pages;
            if ( (
$to - $from ) < $page && (  $to - $from) < $pages )
            {
                
$from = $pages - $page + 1;
            }
        }
    }
        
    if (
$frist && ( $curr_page - 1 ) >= 1 ) $p['frist'] = '<a href="' . $href . '1">' . $frist . '</a>';
    if (
$prev && ( $i = $curr_page - 1 ) >= 1 ) $p['prev'] = '<a href="' . $href . $i . '">' . $prev . '</a>';
    for(
$i = $from; $i <= $to; $i ++ )
    {
        if (
$i == $curr_page )
        {
            
$p[$i] = '<a href="' . $href . $i . '" class="nowpage" target="_self">[' . $i . ']</a>';
        }
        else
        {
            
$p[$i] = '<a href="' . $href . $i . '" target="_self">' . $i . '</a>';
        }
    }
    if (
$next && ( $i = $curr_page + 1 ) <= $pages ) $p['next'] = '<a href="' . $href . $i . '" target="_self">' . $next . '</a>';
    if (
$last && ( $curr_page + 1 ) <= $pages ) $p['last'] = '<a href="' . $href . $pages . '" target="_self">' . $last . '</a>';

    return
implode( $space, $p );
}
// end func



将上面的代码命名为"function.page.php"保存到Smarty的plugins目录里

代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> New Document </TITLE> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTENT=""> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> </HEAD> <BODY> {page row=10} {page row=10 now=5} {page row=10 now=5 href="plugins.php?a=1&b=2&page=" frist="第一页" prev="上一页" next="下一页" last="最后页"} {page row=10 now=5 href="plugins.php?a=1&b=2&page=" frist="第一页" prev="上一页" next="下一页" last="最后页"} {page row=10 now=1 href="plugins.php?a=1&b=2&page=" frist="第一页" prev="上一页" next="下一页" last="最后页"} {page row=10 now=10 href="plugins.php?a=1&b=2&page=" frist="第一页" prev="上一页" next="下一页" last="最后页"} </BODY> </HTML>


将上面的代码命名为"plugins.html"保存到Smarty的template目录里

2、测试程序
PHP代码:

$Smarty
->display( 'plugins.html' );



3、使用说明
我懒得打了,对比一下"plugins.html"的5个{page}用法,以及看看显示出来的效果就明白是什么了

4、插件说明
“《Smarty手册》第十六章.以插件扩展Smarty ”的应用。像中文字符截取之类的都可以以plugins扩展Smarty,Smarty自带的截取不支持中文。

二、Smarty自动生成静态页面
如果你的文件扩展名为".html"~~~~~嘿嘿,这不就是静态页面了吗?-_-!

至于怎么取得静态的文件名呢?

PHP代码:

/**
*
*/
class template extends Smarty
{
    
    
/**
     *
    */
    
function template ()
    {
        
$this->Smarty();
    }
// end func
    
    /**
     *
    */
    
function name ( $tpl_file, $cache_id = null, $compile_id = null )
    {
        
        if (!isset(
$compile_id)) $compile_id = $this->compile_id;

        
$_auto_id = $this->_get_auto_id( $cache_id, $compile_id );
        
$_cache_file = $this->_get_auto_filename( $this->cache_dir, $tpl_file, $_auto_id );

        return
basename( $_cache_file );
    }
// end func
} // end class

$Smarty        = new template;
$file_name    = $Smarty->name( 'plugins.html', 'cache_name' );#html文件的名字(不包含路径)

$Smarty->cache_lifetime    = -1;#静态文件永不过期
$Smarty->fetch( 'plugins.html', 'cache_name' );#生成静态html文件


Smarty的功能很多很多(使用了半年,深有体会),还有待大家继续发掘。。。。