smarty模板简单实例

做smarty一般上网下载一个模板我是下载了一个压缩包, 压缩包—解压--获取其中的---libs文件

 

写一个配置文件init.inc.php

<?php
require("./libs/Smarty.class.php");
$tpl=new Smarty;
$tpl->template_dir='./templates';
$tpl->compile_dir='./templates_c';
$tpl->left_delimiter='<{';
$tpl->right_delimiter='}>';
?>

 

写一个b.php

<?php
include("./init.inc.php");
$a=10;
$tpl->assign("title","this is meinv");
$tpl->assign("title2",$a);
$tpl->assign("title3",true);

//从数据库json,表smarty_user获取
//$mysqli=new mysqli();
//$mysqli->connect("localhost","root",123);
//mysqli->select_db("json");
$mysqli=new mysqli("localhost","root",123,"json");
//insert delete update:返回影响的行数
//select:返回结果集

//mysqli_result类  结果集类
//创建结果集对象不能new ,而是通过调用mysqli的query方法直接返回结果集对象
$result=$mysqli->query("select * from smarty_user");
//使用该对象获取结果集中的数组
//mysql_fetch_array(assoc  row)
//$result->fetch_assoc(row)

 

$row=$result->fetch_assoc();
print_r($row);
//user表里字段包括(id,name,age,email)
$tpl->assign("id",$row['id']);
$tpl->assign("name",$row['name']);
$tpl->assign("age",$row['age']);
$tpl->assign("email",$row['email']);
$tpl->assign("result",$row);


$row=$result->fetch_row();
print_r($row);
$tpl->assign("row",$row);


//自定义数组
$tpl->assign("array1",array("1","2","3"));
$tpl->assign("array2",array(array("a","b"),array("c","d")));


$tpl->assign("array3",array("a"=>"b"));
$tpl->assign("array4",array("a"=>array("b"),array("two"=>"bb")));


class person{
var $name;
var $age;
public function  __construct($name,$age){
$this->name=$name;
$this->age=$age;
}
function say(){
return $this->name."的年龄是".$this->age;
}
}
//分配变量
$tpl->assign("person",new person("zs",20));

//运算
$tpl->assign("num1",10);
$tpl->assign("num2",20);

 

 

$tpl->display("a.html");
?>

 

写一个a.html获取b.php中的代码

 

 

模板文件中的变量分类<br />
一、由php中分配过来的变量<br />
<br />
<{*这是smarty的注释信息*}>
<!-- 这是html注释 -->

这是从php分配过来的标量类型的变量
<{$title}><br />
<{$title2}><br />
<{$title3}><br /><br />
<{$id}><br />
<{$name}><br />
<{$age}><br />
<{$email}><br />

这是关联数组<br />
<{$result['id']}><br />
<{$result['name']}><br />
<{$result['age']}>
<br /><br /><br />


<{$row[0]}><br />
<{$row[1]}><br />
<{$row[2]}><br />
自定义
<{$array1[0]}><br />
<{$array1[1]}><br />
<{$array1[2]}><br />

<{$array2[0][0]}><br />
<{$array2[0][1]}><br />
<{$array2[1][0]}><br /><br /><br />
guanliangshuzu xianshi
<{$array3['a']}><br />

<{$array4.a[0]}><br /><br />

<{$person->say()}>
<{$person->name}>
<{$person->age}><br /><br />
<{$num1+$num2}><br />
<{$num1+$num2*2}><br />

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值