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结构是elseif 的结合体.

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

转载于:https://www.cnblogs.com/imvkmark/archive/2010/02/24/2291520.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值