QuickSkin简单学习--控制结构
QuickSkin简单学习
3.控制结构
if
if ... endif 结构帮助模板的条件选择。 QuickSkin支持和PHP相同的操作符. 比较操作符, 作为名称暗示,允许你比较两个值.
可以是以下的三种语法:
<!– IF var –> var is not empty! <!– ENDIF var –>
<!– IF name == ”John Doe” –> Your name is John Doe! <!– ENDIF name –>
<!– IF name != ”John Doe” –> Your name is not John Doe! <!– ENDIF name –>
变量也能用在语句中:
<!– IF name == variablename –> Your name match with {variablename} <!– ENDIF name –>
<!– IF name != top.variablename –> Your name doesn’t match with {top.variablename} <!– ENDIF name –>
(var after ENDIF is optional)
if.php:
<?php require_once "class.quickskin.php"; $page = new QuickSkin("if.html"); $page->assign( 'username', 'John Doe' ); $page->assign( 'usergroup', 'ADMIN' ); $page->assign( 'picture', '' ); for ($i=0;$i<10;$i++) { $numbers[]['value'] = $i; } $page->assign( 'numbers', $numbers ); $page->assign( 'mynumber', mt_rand(0,9) ); $page->output(); ?>
模板: if.html:
<!-- IF username --> <H3> Welcome, {username} </H3> <!-- ENDIF --> <!-- IF picture --> <img src="{picture}"> <!-- ENDIF picture --> <!-- IF usergroup == "ADMIN" --> <a href="admin.php"> ADMIN Login </a> <!-- ENDIF usergroup --> <!-- BEGIN numbers --> <!-- IF value == parent.mynumber --> <b>{value}</b> <!-- ELSE --> {value} <!-- ENDIF value --> <!-- END numbers -->
输出结果:
<H3> Welcome, John Doe </H3> <a href="admin.php"> ADMIN Login </a> 0 1 2 3 <b>4</b> 5 6 7 8 9
else
else结构扩展了if 结构来显示模板的表达式为FALSE的情况。
else.php:
<?php require_once "class.quickskin.php"; $page = new QuickSkin("else.html"); $page->assign( 'username', 'John Doe' ); $page->assign( 'usergroup', 'ADMIN' ); $page->assign( 'picture', '' ); $page->output(); ?>
模板: else.html:
<!-- IF username --> <H3> Welcome, {username} </H3> <!-- ENDIF --> <!-- 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, John Doe </H3> Picture not available! <br> <a href="admin.php"> ADMIN Login </a><br>
elseif
elseif结构是else和if 的结合体.
elseif.php:
<?php require_once "class.quickskin.php"; $page = new QuickSkin("elseif.html"); $page->assign( 'usergroup', 'INTERNAL' ); $page->output(); ?>
模板:elseif.html:
<!-- IF usergroup == "ADMIN" --> <a href="admin.php"> Admin Staff Login </a><br> <!-- ELSEIF usergroup == "SUPPORT" --> <a href="support.php"> Support Staff Login </a><br> <!-- ELSEIF usergroup --> <a href="other.php"> Standard Login </a><br> <!-- ELSE --> You don't even have a usergroup! <!-- ENDIF -->
输出结果:
<a href="other.php"> Standard Login </a><br>
begin endbegin end
begin ... end提供了一个数组的迭代方法.每一个数组期待这是一个关联数组并用来分析模板的部分并用来替代模板的 <!– BEGIN –><!– END –> 标记
每个关联数组扩展了一下两个常量:
ROWCNT : 文本的实际数量. (0,1,2,3,...n)
ROWBIT : 不同的位的不用意义. (0,1,0,1,0,1,...)
begin_end.php:
<?php require_once "class.quickskin.php"; $page = new QuickSkin("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(); ?>
模板: begin_end.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>
The Result that is created by the PHP Script is displayed in the following box:
Ouput:
<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>
include
所有在主模板中可用的功能性的东西在子模板中均可使用
语法如下:
<!– INCLUDE templatename.html –>
include.php:
<?php require_once "class.quickskin.php"; $page = new QuickSkin("include.html"); $page->assign( 'header', 'This is the header' ); $page->assign( 'body', 'This is the body' ); $page->assign( 'footer', 'This is the foter' ); $page->output(); ?>
模板: include.html:
<!-- INCLUDE header.html --> {body} <!-- INCLUDE footer.html -->
header.html
<h1>{header}</h1>
footer.html
<br> {footer}
输出结果:
<h1>This is the header</h1> This is the body <br> This is the footer