您的位置:首页 > 其它

SmartTemplate学习入门

2015-05-25 13:02 141 查看
php最简单的模板

Array的变量是由SmartTemplate内建函数assign()来赋值的

具体语法如下

assign ( 模版中的变量, 要替换的内容 )



assign ( Array内容 )

和其他程序的变量一样,smartTemplate的变量是由特殊的{}所包含的。里面的内容可以是String,Array,Int,或者是Long Text等等(基本上php支持的)

来个例子:

<?php

$template = new SmartTemplate("template.html");

$text = "菜鸟捡到宝了";

$template->assign( "TITLE", $text );

$template->output();

?>

模版

<html> {TITLE} </html>

输出

<html> 菜鸟捡到宝了</html>

再来个例子:

在只有一个Array的情况下,可以直接省略前面的array handle,就象在使用javascript时,document.window.close()可以省略为window.close()

<?php

$user = array(

"NAME" => "Kissmumu",

"GROUP" => "Admin",

"AGE" => "25",

);

$template = new SmartTemplate("user.html");

$template->assign( $user );

$template->output();

?>

模版

Name: {NAME}

Group: {GROUP}

Age: {AGE}

输出

Name: Kissmumu

Group: Admin

Age: 25

例子3

使用SmartTemplate的循环函数<!-- begin Array名 -->XXXXXX<!-- end Array名>

他的功能类似foreach(),只要有东西,就一直循环显示

<?php

$links = array(

array(

"TITLE" => "PHP",

"URL" => "http://www.php.net/...,

),

array(

"TITLE" => "Apache",

"URL" => "http://www.php.net/...,

),

array(

"TITLE" => "MySQL",

"URL" => "http://www.mysql.com/&...,

),

);

$template = new SmartTemplate("links.html");

$template->assign( "links", $links );

$template->output();

?>

HTML模版

<html>

<h3> Sample Links </h3>

<!-- BEGIN links -->

<a href="../{URL}"> {TITLE} </a>

<!-- END links -->

</html>

输出

<html>

<h3> Sample Links </h3>

<a href="../http://www.php.net/"> PHP </a>

<a href="../http://www.apache.org/"> Apache </a>

<a href="../http://www.mysql.com/"> MySQL </a>

</html>

再看看SmartTemplate的逻辑控制结构

If和end If

语法:

<!-- IF 变量 --> 变量已被赋值! <!-- ENDIF 变量 -->

如果IF后面直接跟变量,变量为Null时会返回0,否则返回1

<!-- IF name=="kissmumu" --> My name is kissmumu! <!-- ENDIF name -->

“==”判断是否相等,如果相等返回1,不相等返回0

<!-- IF name!="kissmumu" --> My name is not kissmumu! <!-- ENDIF name -->

“!=”判断是否不等,如果成立返回1,相等则返回0

例子:

<?php

require_once "class.smarttemplate.php";

$page = new SmartTemplate("if.html");

$page->assign( "username", "kissmumu" );

$page->assign( "usergroup", "ADMIN" );

$page->assign( "picture", "" );

$page->output();

?>

模板:

<!-- IF username --> <H3> Welcome, {username} </H3> <!-- ENDIF username -->

<!-- IF picture --> <img src="{picture}"> <!-- ENDIF picture -->

<!-- IF usergroup="ADMIN" -->

<a href="../admin.php"> ADMIN Login </a><br>

<!-- ENDIF usergroup -->

输出

<H3> Welcome, kissmumu </H3>

<a href="../admin.php"> ADMIN Login </a><br>

IF的子句 else

如果else子句出现在一个逻辑循环中,当if的条件不成立时则会被运行。

例子

<?php

require_once "class.smarttemplate.php";

$page = new SmartTemplate("else.html");

$page->assign( "username", "kissmumu" );

$page->assign( "usergroup", "ADMIN" );

$page->assign( "picture", "" );

$page->output();

?>

模版

<!-- IF username -->

<H3> Welcome, {username} </H3>

<!-- ENDIF username -->

<!-- IF picture -->

<img src="{picture}">

<!-- ELSE -->

Picture not available! <br>

<!-- ENDIF picture -->

<!-- IF usergroup="ADMIN" -->

<a href="../admin.php"> ADMIN Login </a><br>

<!-- ELSE -->

You are in guest mode!

<!-- ENDIF usergroup -->

输出

<H3> Welcome, kissmumu </H3>

Picture not available! <br>

<a href="../admin.php"> ADMIN Login </a><br>

elseif

elseif是else和if组合起来的一种结构,其意义为"除此之外如果..."

以下是一个例子

<?php

require_once "class.smarttemplate.php";

$page = new SmartTemplate("elseif.html");

$page->assign( "usergroup", "INTERNAL" );

$page->output();

?>

模版文件

<!-- IF usergroup="ADMIN" -->

<a href="../admin.php"> 管理员登陆 </a><br>

<!-- ELSEIF usergroup="SUPPORT" -->

<a href="../support.php"> 帮助人员登陆</a><br>

<!-- ELSEIF usergroup -->

<a href="../other.php"> 普通方式登陆 </a><br>

<!-- ELSE -->

You don"t even have a usergroup!

<!-- ENDIF -->

输出

<a href="../other.php"> 普通方式登陆 </a><br>

Begin...End

这个语句用于读取一个整数索引矩阵(Numerical Array,以数字为索引的数组)的值.而每个整数矩阵的子矩阵则成为以字符串为索引的矩阵(Associative Array)然后在<!-- begin --> 和 <!-- end -->之间的语句将会被读取并且重复执行.

附加:每个associative array(字符串为索引的矩阵)会有两个附加的值

ROWCNT : 此元素在父矩阵中的具体位置 (0,1,2,3,...n) (就是目前在第几个矩阵)

ROWBIT : 矩阵序号的最后一位. (0,1,0,1,0,1,...)

下面是一个例子

<?php

require_once "class.smarttemplate.php";

$page = new SmartTemplate("begin_end.html");

$users = array(

array( "NAME" => "John Doe", "GROUP" => "ADMIN" ),

array( "NAME" => "Jack Doe", "GROUP" => "SUPPORT" ),

array( "NAME" => "James Doe", "GROUP" => "GUEST" ),

array( "NAME" => "Jane Doe", "GROUP" => "GUEST" ),

);

$page->assign( "users", $users );

$page->output();

?>

HTML模版

<style type="text/css">

.col0 { background-color: #D0D0D0; }

.col1 { background-color: #F0F0F0; }

</style>

<table border="1" cellpadding="2" cellspacing="0">

<tr>

<th> No </th>

<th> Username </th>

<th> Usergroup </th>

</tr>

<!-- BEGIN users -->

<tr class="col{ROWBIT}">

<td> {ROWCNT} </td>

<td> {NAME} </td>

<td> {GROUP} </td>

</tr>

<!-- END users -->

</table>

最后的输出

<style type="text/css">

.col0 { background-color: #D0D0D0; }

.col1 { background-color: #F0F0F0; }

</style>

<table border="1" cellpadding="2" cellspacing="0">

<tr>

<th> No </th>

<th> Username </th>

<th> Usergroup </th>

</tr>

<tr class="col0">

<td> 0 </td>

<td> John Doe </td>

<td> ADMIN </td>

</tr>

<tr class="col1">

<td> 1 </td>

<td> Jack Doe </td>

<td> SUPPORT </td>

</tr>

<tr class="col0">

<td> 2 </td>

<td> James Doe </td>

<td> GUEST </td>

</tr>

<tr class="col1">

<td> 3 </td>

<td> Jane Doe </td>

<td> GUEST </td>

</tr>

</table>

小结smartTemplate的方法

注:以下列出的方法并不会在模版设计中出前,他们属于smartTemplate的代码编辑部分,但是如果为了实现更深一步的模版设计,下面的内容是肯定有用的.

基础方法:assign (中文意思:赋值)

语法:

assign ( 变量名, 混合内容 )

或者

assign ( 矩阵变量 )

Append(附加)

语法:append ( 变量名, 内容 )

对所提供的变量附加提供的内容

例子:

<?php

$page = new SmartTemplate("links.html");

$page->append("links" => array(

"TITLE" => "PHP",

"URL" => "http://www.php.net/...

));

$page->append("links" => array(

"TITLE" => "Apache",

"URL" => "http://www.apache.org/&...

));

$page->append("links" => array(

"TITLE" => "MySQL",

"URL" => "http://www.mysql.com/&...

));

$page->output();

?>

模版

<html>

<h3> Sample Links </h3>

<!-- BEGIN links -->

<a href="../{URL}"> {TITLE} </a>

<!-- END links -->

</html>

最终输出

<html>

<h3> Sample Links </h3>

<a href="../http://www.php.net/"> PHP </a>

<a href="../http://www.apache.org/"> Apache </a>

<a href="../http://www.mysql.com/"> MySQL </a>

</html>

一个附加字符串的例子:

<?php

$page = new SmartTemplate("template.html");

$page->append("TITLE" => "Hello ");

$page->append("TITLE" => "World ");

$page->append("TITLE" => "!");

$page->output();

?>

模板

<html> {TITLE} </html>

输出将会得到

<html> Hello World ! </html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: