fanso模板安装
要求
fanso模板要求PHP 4 >= 4.3.0
基本安装
解压下载到的 fansotemplate.rar 文件
里面有一个demo文件夹和libs文件夹,demo文件夹是示例。
libs文件夹是fanso.class.php类所在的文件夹.我们只要调用这个类就行了。
举个例子:
复制libs文件夹到网站根目录下,
并在网站根目录下建立 templates和templates_c文件夹。
并把templates_c的属性设为 777。
其中templates保存html模板文件templates_c保存编译文件。
require('libs/fanso.class.php');
$fanso = new Fanso; ;
这样就创建了Fanso实例。
fanso模板的语法
先举一个简单的例子来说明 fanso模板的语法
example.php
<?php
require('libs/fanso.class.php');
$fanso = new Fanso;
$phpfans = 'php爱好者';
$fanso->assign('lang',$phpfans);
$fanso->display('example.html');
?> ;
templates/example.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>fanso模板示例</title>
</head>
<body>
{$lang}
</body>
</html> ;
输出结果:
可以看出使用fanso模板只要3步
第一步是 创建Fanso实例
require('libs/fanso.class.php');
$fanso = new Fanso;
第二步是 赋值
$fanso->assign('lang',$phpfans);
第三步是 输出
$fanso->display('example.html');
$lang是通过assign()赋的值,将输出的是$phpfans的值。
可以看到,html文件中模板的语法是以'{'开始'}'结束的。这分别是模板的的$left_delimiter和$right_delimiter。
默认值分别是 '{' 和 '}'。当然我们可以根据自己需要修改它们的值。同时也可以修改 模板文件存放目录 和 编译文件存放目录
下面举个例子:
example1.php
<?php
require('libs/fanso.class.php');
$fanso = new Fanso;
$fanso->template_dir = "tem";//设置模板目录
$fanso->compile_dir = "tem_c"; //设置编译目录
$fanso->left_delimiter = "<{"; //左定界符
$fanso->right_delimiter = "}>";//右定界符
$phpfans = 'php爱好者';
$fanso->assign('lang',$phpfans);
$fanso->display('example1.html');
?> ;
tem/example1.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>fanso模板示例</title>
</head>
<body>
<{$lang}>
</body>
</html> ;
输出结果:
下面举一个数组输出的例子:
example2.php
<?php
require('libs/fanso.class.php');
$fanso = new Fanso;
$web['name'] = 'php爱好者';
$web['url'] = 'http://www.phpfans.net';
$fanso->assign('lang',$web);
$fanso->display('example2.html');
?> ;
templates/example2.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>fanso模板示例</title>
</head>
<body>
{$lang.name}的网址是:<a href="{$lang.url}">{$lang.url}</a>
</body>
</html> ;
输出结果:
可以看出通过assign()赋值后$lang下一级的属性是通过 '.' 获得的,这和javascript很相似。
还有就是当assign()的第一个是数组时,模板中的值将以数组中的索引作为标记。
举个例子
example3.php
<?php
require('libs/fanso.class.php');
$fanso = new Fanso;
$web['name'] = 'php爱好者';
$web['url'] = 'http://www.phpfans.net';
$fanso->assign($web);//在$web为数组时只需一个参数
$fanso->display('example3.html');
?> ;
templates/example3.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>fanso模板示例</title>
</head>
<body>
{$name}的网址是:<a href="{$url}">{$url}</a>
</body>
</html> ;
输出结果:
assign语法
{assign var="name" value="value"}
其中var的值要符合一本命名标准,不能有特殊字符。
举个例子:
example4.php
<?php
require('libs/fanso.class.php');
$fanso = new Fanso;
$phpfans = 'php爱好者';
$fanso->assign('lang',$phpfans);
$fanso->display('example4.html');
?> ;
templates/example4.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>fanso模板示例</title>
</head>
<body>
{assign var="name" value="php爱好者"}
这里将显示模板内部定义的一个变量:{$name}
</body>
</html> ;
输出结果:
if,elseif,else语法
if和elseif可以对 ==,<,>,>=,<=,!=,<>,===判断,多个条件可以通过 &&,||,and,or相连
如果有elseif和else语句,它们必须在{if ...} {/if}内。
示例:
example5.php
<?php
require('libs/fanso.class.php');
$fanso = new Fanso;
$phpfans['one'] = '0';
$phpfans['two'] = '';
$phpfans['three'] = '1';
$phpfans['four'] = 'phpfans';
$fanso->assign('lang',$phpfans);
$fanso->display('example5.html');
?> ;
templates/example5.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>fanso模板示例</title>
</head>
<body>
if,elseif,else的一些语法<br>
{if $lang.one == 0}
php爱好者站_fanso模板<br>
{/if}
{if $lang.one == 0}
条件1成立
{else}
条件1不成立
{/if}
<br>
{if !$lang.one && $lang.two}
if条件成立
{elseif ($lang.one >0 or $lang.four == 'phpfans')&&$lang.two== ''}
elseif条件成立
{else}
else条件成立
{/if}
</body>
</html> ;
输出结果:
section,sectionelse语法
属性名称 | 类型 | 必需 | 默认值 | 描述 |
name | 字符串 | 是 | n/a | section的名称 |
loop | 变量 | 是 | n/a | 要循环的变量 |
start | 整型 | 否 | 0 | 从数组start索引循环 |
step | 整型 | 否 | 1 | 每次循环的间隔 |
其中还有可用属性
index
index_prev
index_next
iteration
first
last
rownum
loop
total
这些属性的用法大家可用在下面的例子中掌握。
示例:
example6.php
<?php
require('libs/fanso.class.php');
$fanso = new Fanso;
$arr['new'][]= array("newsID"=>"001", "newsTitle"=>"第1条新闻");
$arr['new'][]= array("newsID"=>"002", "newsTitle"=>"第2条新闻");
$arr['new'][]= array("newsID"=>"003", "newsTitle"=>"第3条新闻");
$arr['new'][]= array("newsID"=>"004", "newsTitle"=>"第4条新闻");
$fanso->assign('lang',$arr);
$fanso->display('example6.html');
?> ;
templates/example6.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>fanso模板示例</title>
</head>
<body>
{section name=seid loop=$lang.new}
{if $fanso.section.seid.first}
<br><br><table border="1" align="center" width="70%">
<tr>
<td colspan="7">section 语法示例1: section顺序输出</td>
</tr>
<tr>
<td>index</td>
<td>index_prev</td>
<td>index_next</td>
<td>rownum</td>
<td>iteration</td>
<td>newsID</td>
<td width="50%">newsTitle</td>
</tr>
{/if}
<tr>
<td>{$fanso.section.seid.index}</td>
<td>{$lang.new[seid.index_prev]}</td>
<td>{$lang.new[seid.index_next]}</td>
<td>{$fanso.section.seid.rownum}</td>
<td>{$fanso.section.seid.iteration}</td>
<td>{$lang.new[seid].newsID}</td>
<td>
{$lang.new[seid].newsTitle}
</td>
</tr>
{if $fanso.section.seid.last}
<tr>
<td>loop</td>
<td colspan="3">共有{$fanso.section.seid.loop}条记录</td>
<td>total</td>
<td colspan="2">共有输出{$fanso.section.seid.total}条记录</td>
</tr>
</table>
{/if}
{/section}
<br>
{section name=seid1 loop=$lang.new start=1 step=2}
{if $fanso.section.seid1.first}
<table border="1" align="center" width="70%">
<tr>
<td colspan="7">section 语法示例2: start=1,step=2。section从第2条开始隔2条输出</td>
</tr>
<tr>
<td>index</td>
<td>index_prev</td>
<td>index_next</td>
<td>rownum</td>
<td>iteration</td>
<td>newsID</td>
<td width="30%">newsTitle</td>
</tr>
{/if}
<tr>
<td>{$fanso.section.seid1.index}</td>
<td>{$lang.new[seid1.index_prev]}</td>
<td>{$lang.new[seid1.index_next]}</td>
<td>{$fanso.section.seid1.rownum}</td>
<td>{$fanso.section.seid1.iteration}</td>
<td>{$lang.new[seid1].newsID}</td>
<td>{$lang.new[seid1].newsTitle}</td>
</tr>
{if $fanso.section.seid1.last}
<tr>
<td>loop</td>
<td colspan="3">共有{$fanso.section.seid1.loop}条记录</td>
<td>total</td>
<td colspan="2">共有输出{$fanso.section.seid1.total}条记录</td>
</tr>
</table>
{/if}
{sectionelse}
<table>
<tr>
<td colspan="7">没有记录</td>
</tr>
</table>
{/section}
</body>
</html> ;
输出结果:
include 语法
{include file="tpl_url"}
用include进来的html模板会被编译
示例:
example7.php
<?php
require('libs/fanso.class.php');
$fanso = new Fanso;
$phpfans = 'fanso模板include示例';
$fanso->assign('lang',$phpfans);
$fanso->display('example7.html');
?> ;
templates/example7.html
{include file="header.html"}
{$lang}
</body>
</html> ;
templates/header.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
{assign var="title" value="fanso模板v1.0_php爱好者"}
<title>{$title}</title>
</head>
<body>
include 语法示例: 这是include进去的header.html的内容<br><br> ;
输出结果:
include_php 语法
{include_php file="php_url"}
其中php_url的位置是相对于调用模板文件的路径而不是相对模板文件
示例:
example8.php
<?php
require('libs/fanso.class.php');
$fanso = new Fanso;
$phpfans = 'fanso模板include_php示例';
$fanso->assign('lang',$phpfans);
$fanso->display('example8.html');
?> ;
templates/example8.html
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>fanso模板示例</title>
</head>
<body>
{include_php file="include/in.php"}<br>
{$lang}
</body>
</html> ;
include/in.php
<?php
echo "<br>include_php 语法示例: 这是include_php进出的文件in.php输出的内容。";
?> ;
输出结果:
cycle 语法
{cycle values="val1,val2,.."}
val以 ',' 隔开
示例:用cycle控制表格背景颜色
example9.php
<?php
require('libs/fanso.class.php');
$fanso = new Fanso;
$arr['new'][]= array("newsID"=>"001", "newsTitle"=>"第1条新闻");
$arr['new'][]= array("newsID"=>"002", "newsTitle"=>"第2条新闻");
$arr['new'][]= array("newsID"=>"003", "newsTitle"=>"第3条新闻");
$arr['new'][]= array("newsID"=>"004", "newsTitle"=>"第4条新闻");
$fanso->assign('lang',$arr);
$fanso->display('example9.html');
?> ;
templates/example9.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>fanso模板示例</title>
</head>
<body>
<table border="1" align="center" width="50%">
{section name=loop loop=$lang.new}
<tr bgcolor="{cycle values="#eeeeee,#cccccc"}">
<td>{$lang.new[loop].newsID}</td>
<td>
{$lang.new[loop].newsTitle}
</td>
</tr>
{/section}
</table>
</body>
</html> ;
输出结果:
php语法
{php} .. {/php}
示例:
example10.php
<?php
require('libs/fanso.class.php');
$fanso = new Fanso;
$phpfans = 'php爱好者';
$fanso->assign('lang',$phpfans);
$fanso->display('example10.html');
?> ;
templates/example10.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>fanso模板示例</title>
</head>
<body>
{php}
$fanso = 'fanso模板';
echo $fanso.'_';
{/php}
{$lang}
</body>
</html> ;
输出结果:
code语法
{code}...{/code}
code语法与php语法的差别是,code中的变量要当模板变量来解析。php语法中则保留原样。
示例:
example11.php
<?php
require('libs/fanso.class.php');
$fanso = new Fanso;
$web['name'] = 'php爱好者';
$web['url'] = 'http://www.phpfans.net';
$fanso->assign('lang',$web);
$fanso->display('example11.html');
?> ;
templates/example11.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>fanso模板示例</title>
</head>
<body>
{code}
if($lang.name == 'php爱好者') {
echo $lang.name.'的网址是'.$lang.url;
}
{/code}
</body>
</html> ;
输出结果: