|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
刚开始写页面程序,调试完书中的例子。然后就可以尝试编写留言板了, 1、扩大你的Smarty
1、筹办工夫
PHP代码:--------------------------------------------------------------------------------
function Smarty_function_page ( $params, &$Smarty )
{
$href = '#';
$space = ' ';
$frist = NULL;
$last = NULL;
$page = 5;
extract($params);
if ( !$row || $row <= 1 ) return ' ';
$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自带的截取不撑持中文。
__________________
2、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的使用还远不止于此。
One of the unique aspects about Smarty is the template compling. This means Smarty reads the template files and creates PHP scripts from them. Once they are created, they are executed from then on. Therefore there is no costly template file parsing for each request, and each template can take full advantage of PHP compiler cache solutions such as Zend Accelerator (http://www.zend.com) or PHP Accelerator (http://www.php-accelerator.co.uk).
Smarty的特色之一是"模板编译"。意思是Smarty读取模板文件然后用他们创立php剧本。这些剧本创立今后将被履行。因而并没有消费模板文件的语法解析,同时每一个模板可以享用到诸如Zend减速器(http://www.zend.com) 或PHP减速器(http://www.php-accelerator.co.uk)。如许的php编译器高速缓存处理计划。
Some of Smarty's features:
Smaty的一些特色:
It is extremely fast.
十分十分的快!
It is efficient since the PHP parser does the dirty work.
用php剖析器干这个苦差事是无效的
No template parsing overhead, only compiles once.
不需求过剩的模板语法解析,仅仅是编译一次
It is smart about recompiling only the template files that have changed.
仅对修正过的模板文件停止从头编译
You can make custom functions and custom variable modifiers, so the template language is extremely extensible.
可以编纂'自界说函数'和自界说'变量',因而这类模板言语完整可以扩大
Configurable template delimiter tag syntax, so you can use {}, {{}}, <!--{}-->, etc.
可以自行设置模板定界符,所以你可使用{}, {{}}, <!--{}-->, 等等
The if/elseif/else/endif constructs are passed to the PHP parser, so the {if ...} expression syntax can be as simple or as complex as you like.
诸如 if/elseif/else/endif 语句可以被传递到php语法解析器,所以 {if ...} 表达式是复杂的或是复合的,随你喜好啦
Unlimited nesting of sections, ifs, etc. allowed.
假如答应的话,section之间可以无穷嵌套
It is possible to embed PHP code right in your template files, although this may not be needed (nor recommended) since the engine is so customizable.
引擎是可以定制的.可之内嵌php代码到你的模板文件中,固然这能够其实不需求(不保举)
Built-in caching support
内建缓存撑持
Arbitrary template sources
自力模板文件
Custom cache handling functions
可自界说缓存处置函数
Plugin architecture
插件系统布局
因为函数实在是太多了,慢慢的你就会知道,知道有这个函数就可以。 |
|