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/&#3...,
),
array(
"TITLE" => "Apache",
"URL" => "http://www.php.net/&#3...,
),
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/&#3...
));
$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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/463882.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

人生感言

马云说的一些话&#xff0c;还是值得思考的&#xff1a; (1)、细节好的人格局一般比较差 (2)、态度比能力重要&#xff0c;选择同样也比能力重要 (3)、领导比员工多什么&#xff1f; 领导永远不要跟下属比技能&#xff0c;下属肯定比你强&#xff1b;如果不比你强&#x…

当MCU死机了,先把硬件抓过来~

关于软件开发中的偶发性问题&#xff0c;有些处理办法看似不是很难&#xff0c;但其实最重要的还是对问题的敏感度&#xff0c;而这份敏感度就来源于你对整个系统的理解和把握。当你能够尽快缩小问题代码的范围&#xff0c;在一定程度上就已经加快了解决问题的进度。之前我曾提…

青蛙学Linux—NFS

NFS&#xff0c;Network File Syttem&#xff0c;网络文件系统。它允许网络上运行不同操作系统的主机通过网络连接到运行NFS服务的主机上&#xff0c;以实现数据共享。NFS的配置非常简单&#xff0c;经过简单的设置既能快速使用NFS。 使用NFS&#xff0c;首先在服务端运行NFS服…

extjs 学习中

首先找了个js得开发工具&#xff1a;Komodo Edit 但是安装了好多次都没有出现提示&#xff0c;均告失败&#xff01;今天再次鼓起勇气安装试试&#xff1a;首先安装Komodo-Edit-4.3.2-1263; 成功&#xff01;然后&#xff1a; 下载extjs_api_catalogs-2.0.2-ko.xpi &#xff…

火眼睛睛查coredump(stl sort)------永远让比较函数对相同元素返回false

转载&#xff1a;http://blog.csdn.net/stpeace/article/details/51040218#cpp 看看如下代码的一个非常隐晦的错误&#xff0c; 虽然不会每次core dump, 但类似代码迟早会core dump&#xff0c; 好多人遇到过。 此问题极难定位&#xff0c; 看一下吧&#xff1a; [cpp] view pl…

P1648 看守

传送门 以二维的两个点\((x1,y1),(x2,y2)\)为例&#xff0c;那么他们之间的曼哈顿距离肯定为一下四个之一\((x1-x2)(y1-y2)\),\((x2-x1)(y1-y2)\),\((x1-x2)(y2-y1)\),\((x2-x1)(y2-y1)\)&#xff0c;而且为这四个里面最大的 然后搞一搞可以变成下面的样子\((x1y1)-(x2y2)\),\(…

好友让我看这段代码

周末的时候&#xff0c;一个微信好友让我旁边看一段代码在写下面的文章之前&#xff0c;我先简单说下写代码是一件非常有意思的事情&#xff0c;同时也是一件需要我们认真对待的事情&#xff0c;我不认为一定要把代码写的和大神一样看不明白&#xff0c;但是至少要逻辑清晰&…

使用github管理Eclipse分布式项目开发

使用github管理Eclipse分布式项目开发 老关我在前面的博文&#xff08;github管理iOS分布式项目开发&#xff09;中介绍了github管理iOS分布式开发&#xff0c;今天老关将向大家介绍使用github管 理Eclipse分布式项目。事实上我们的516inc团队这在开发一个多移动平台项目&#…

愉快且卓有成效:培养你与人相处的能力

毫无疑问&#xff0c;渊博的学识和不断的创新是事业成功的基础。然而&#xff0c;把一个概念变为成果&#xff0c;离开他人的合作&#xff0c;任何人&#xff0c;无论是伟人还是凡夫&#xff0c;都无法实现。与人合作得是否愉快且卓有成效&#xff0c;完全取决于你与人相处的能…

小玩一个并行多线程MCU—MC3172

大家好转发一篇杂烩君的文章&#xff0c;杂烩君是我同一个高中的老乡&#xff0c;他平时分享的嵌入式知识非常不错。——————大家好&#xff0c;我是杂烩君。最近&#xff0c;朋友送了块小板子&#xff0c;板子上的MCU是个很有意思的东西——并行多线程处理器MC3172 。通俗…

Android 人脸识别签到(一)

因为Android课程设计自己选题&#xff0c;所以作者选了这个相对简单的。本来开始是想做大学课程查签到&#xff0c;拍一张集体照&#xff0c;就可识别哪些人已到&#xff0c;哪些未到。查了一下百度AI开发平台的人脸识别接口&#xff0c;发现V3的接口文档有M:N的人脸识别&#…

[系列文章]上传文件管理控件v2

一、引言&#xff1a; 开发v1的时候&#xff0c;遇到很多困难&#xff08;因为我是新手&#xff09;&#xff0c;于是就上网找了一些资料。 其中&#xff0c;这篇《数据绑定的总结 》文章&#xff08;http://www.cnblogs.com/qingtianyzl/articles/351012.html&#xff09;被我…

[ mongoDB ] - mongoDB的基本操作

mongoDB的基本操作 强烈推荐参考官方用户手册&#xff1a; http://www.mongodb.org/display/DOCS 1)插入&#xff08;insert&#xff09; 插入的value是json对象&#xff0c;以下示例循环添加了10个用户信息&#xff0c;字段可以是字符串、数值、对象、数组等。 通过VUE查看&am…

STL中的multimap---顺便说说如何查找同一关键字对应的所有值

转载&#xff1a;http://blog.csdn.net/stpeace/article/details/44984639 我个人感觉哈&#xff0c; map的应用场景比multimap更多&#xff0c; 不过&#xff0c; 我们还是来学一下multimap。 我们知道&#xff0c; multimap中&#xff0c; 一个关键字可能对应多个不同的值&am…

信号量保护之位带操作

大家好&#xff0c;上篇文章写的一个中断操作变量的问题&#xff0c;鱼鹰帮忙回复了&#xff0c;大家可以再看看这篇文章。好友让我看这段代码CM3位带操作如果存储器系统支持“锁定传送”&#xff08; lockedtransfers&#xff09;&#xff0c;或者总线上只有一个主机&#xff…

LeetCode872. Leaf-Similar Trees

自己的代码&#xff1a; # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val x # self.left None # self.right Noneclass Solution:def allNode(self,root):listNode[]if Not root:return ListNode…

CCScene切换的所有特效(28种)以及设置屏幕横竖屏!

CCScene 对于使用cocos2d的童鞋肯定很熟悉&#xff0c;那么在使用 CCScene *s [CCScenenode];[s addChild: [restartAction() node]];[[CCDirectorsharedDirector]replaceScene:s]; 对scene进行场景&#xff08;CCLayout&#xff09;进行切换时&#xff0c;cocos2d提供了一…

关于MySqlConnection的一个特殊异常

最近给一个客户做一个项目&#xff0c;其中要与另外一个系统进行数据交换&#xff0c;而那个系统使用的是MySql&#xff0c;因此&#xff0c;从网上下载了一个MySql Connector&#xff0c;由于以前没有用过MySql&#xff0c;而且这个MySql服务器位于互联网上&#xff0c;因此&a…

文档设置及使用

原文链接&#xff1a;http://stynzf.blogbus.com/logs/15944928.html在上一篇文档&#xff08;《微软文档管理解决方案2007》之一&#xff1a;安装部署&#xff09;中&#xff0c;讲述了如何安装部署“文档管理解决方案” 这次我们通过将该子网站作为“Windows中文站”的一个文…

SSL和HTTPS

SSL说明&#xff1a; 1&#xff09;简介 SSL (Secure Socket Layer)为Netscape所研发&#xff0c;用以保障在Internet上数据传输之安全&#xff0c;利用数据加密(Encryption)技术&#xff0c;可确保数据在网络上之传输过程中不会被截取。它已被广泛地用于Web浏览器与服务器之间…