|
init.smarty.php
<?php
define("ROOT", ".");
//解决问题:Warning: strftime() [function.strftime]:
date_default_timezone_set("Asia/Shanghai");
include ROOT."/libs/Smarty.class.php";
$tpl = new Smarty();
//smarty初始化
$tpl->template_dir=ROOT."/templates/";
$tpl->compile_dir=ROOT."/templates_c/";
//配置文件位置
$tpl->config_dir=ROOT."/configs/";
$tpl->left_delimiter="<!--{";
$tpl->right_delimiter="}-->";
?>
mysmarty.php
<?php
/**
*
* 以下所有使用变量都要基于设定的前缀和后缀
*
* 一:模板中的注释
* 1.<!-- -->
* 查看源码可以看到
* 2.<!--{* *}-->
* smarty的注释,源文件和编译后的文件中也是看不到的
* 二:smarty中使用变量
* 1.从php中分配给模板变量
* 动态的数据(php从数据库或者文件,以及算法生成的变量)
* 任何类型 的数据都可以从php分配过来
* 标量:string,int,double,boolean
* 复合:array object NULL
* 关联数组:不是使用而是使用.来访问
*
* 2.在配置文件中给模板变量
* 模板的外观(界面)设计的变量
* 需要指定目录:
* $tpl->config_dir=ROOT."./configs/";
* 创建文件view.conf(存放键值对形式)
* 模板页面需要导入配置文件
* <!--{config_load file="view.conf"}-->
*使用<!--{#name#}-->(#必须紧挨着前缀和后缀)
*使用<!--{$smarty.config.name}-->(#必须紧挨着前缀和后缀)
*设定使用某个域的配置信息:
*[one]aa=11111
*[two]cc=22222
*[three]dd=33333
* <!--{config_load file="view.conf" section="one"}-->
* 3.模板中存在的保留变量
* php全局数组:
* $_GET
* $_POST
* $_SESSION
* $_COOKIE
* $_REQUEST
* $_SERVER
* $_ENV
* $_FILES
* $GLOBALS define("ROOT", ".");
* smarty一些常用的变量,模板中自带的
* <!--{$smarty.get.*}-->
* <!--{$smarty.post.*}-->
* <!--{$smarty.cookie.*}-->
* <!--{$smarty.session.*}-->
* <!--{$smarty.env.*}-->
* <!--{$smarty.server.*}-->
* <!--{$smarty.request.*}-->
* 使用常量
* <!--{$smarty.const.*}-->
* <!--{$smarty.const.__FILE__}-->
* <!--{$smarty.const.ROOT}-->
* <!--{$smarty.now}-->
* <!--{$smarty.now|date_format:"&Y-&m-&d &M:&M:&S"}-->
*
*/
//如果文件加载失败require会停止继续解析php;而include则会继续向下执行
require 'init.smarty.php';
$title="这是一个文字标题";
$mysqli = new mysqli("localhost","root","root","hibernate");
$result = $mysqli->query("select id,name,price from users");
$row = $result->fetch_assoc();
$name = $row["name"];
$_SESSION["username"]="devilzy";
class Person{
var $name;
var $age;
function __construct($name,$age){
$this->age = $age;
$this->name = $name;
}
function say(){
return $this->name."=".$this->age;
}
}
$tpl->assign("title",$title);
$tpl->assign("name",$name);
$tpl->assign("array1",array("第一个","第二个","第三个"));
$tpl->assign("array2",array("one"=>"关联第一个","two"=>"关联第二个"));
$tpl->assign("array3",array("one"=>array("two"=>"我是二维数组")));
$tpl->assign("person",new Person("张三", 10));
$tpl->assign("num1",10);
$tpl->assign("num2",20);
$tpl->assign("page",$_GET["page"]);
//模板文件名可以随便定义:比如:mysmarty.tpl只有内容是html就可以了
$tpl->display("mysmarty.html");
?>
mysmarty.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<!--{config_load file="view.conf" section="one"}-->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><!--{$title}--></title>
</head>
<body>
<!-- 普通 -->
<!--{$name}-->
<!-- 数组 -->
<!--{$array1[0]}-->
<!--{$array1[1]}-->
<!--{$array1[2]}-->
<!-- 关联数组 -->
<!--{$array2.one}-->
<!--{$array2.two}-->
<!-- 二维数组 -->
<!--{$array3.one.two}-->
<!-- 对象 -->
<!--{$person->name}-->
<!--{$person->age}-->
<!--{$person->say()}-->
<!-- 数学运算 -->
<!--{$num1}-->
<!--{$num2}-->
<!--{$num1+$num2*$num2-$num2}-->
<br>
<!--{$page}-->
<!--{$smarty.get.page}-->
<!--{$smarty.session.username}-->
<!--{* 就和php中的$_GET["page"] *}-->
<!--{$smarty.const.ROOT}-->
<!--{$smarty.now}-->
<!--{$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"}-->
<br>
执行的文件:<!--{$smarty.const.__FILE__}-->
<br>
访问的脚本文件:<!--{$smarty.server.SCRIPT_FILENAME}-->
<br>
<!-- 配置文件 -->
<!--{#aa#}-->
<!--{#bb#}-->
<!--{#cc#}-->
<!--{#dd#}-->
<table border="<!--{$smarty.config.boder}-->" width="<!--{#tabwidth#}-->" bordercolor="<!--{#tabboder#}-->">
<tr bgcolor="<!--{#tgcor#}-->">
<td>qqqq</td>
<td>qqqq</td>
<td>qqqq</td>
</tr>
<tr>
<td>qqqq</td>
<td>qqqq</td>
<td>qqqq</td>
</tr>
<tr>
<td>qqqq</td>
<td>qqqq</td>
<td>qqqq</td>
</tr>
</table>
</body>
</html>
configs/view.conf boder=10
tabwidth=800
tabboder=red
tgcor=yellow
[one]
aa=11111
bb=33
[two]
cc=22222
[three]
dd=33333 |
|
|