SmartTemplate学习入门一
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>
轉載于:https://www.cnblogs.com/fengju/archive/2008/05/15/6174134.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的SmartTemplate学习入门一的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: flutter offset_Flutt
- 下一篇: node获取图片路径_使用软连接来解决