摘要:<?php /** * smarty配置文件 */ require __DIR__.'/../vendor/autoload.php'; $smt=new Smarty(); $smt->setTemplateDir(__DIR__.'/../temp'); $smt->setCompileD
<?php /** * smarty配置文件 */ require __DIR__.'/../vendor/autoload.php'; $smt=new Smarty(); $smt->setTemplateDir(__DIR__.'/../temp'); $smt->setCompileDir(__DIR__.'/../comp'); $smt->setCacheDir(__DIR__.'/../cache'); $smt->setConfigDir(__DIR__.'/../config'); $smt->setCaching(false); $smt->setCacheLifetime(60*60*24*7);
<?php
/*逻辑文件*/
header("Content-type:text/html;charset=utf-8");
$str='demo2';
$arr=[1,2,3];
$arr2=[
[1,2,3,4],
[2,3,4,5],
[3,4,1,2],
[4,1,2,3],
];
class obj
{
public $str='obj';
public function fun(){
return 'obj.fun';
}
}
const DEFINE = 'define';
function fun ()
{
return 'fun';
}
$_GET['name']='name';
$obj=new obj();
require __DIR__.'/config/config.php';
$smt->assign('str',$str);
$smt->assign('arr',$arr);
$smt->assign('arr2',$arr2);
$smt->assign('obj',$obj);
$smt->display('demo2.html');<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<ol>
<li>{$str}</li>
<li>{$arr.0},{$arr.1},{$arr.2}</li>
<li>{$arr2.0.0},{$arr2.1.1},{$arr2.2.2},{$arr2.3.3}</li>
<li>{$obj->str}</li>
<li>{$obj->fun()}</li>
<li>{$smarty.const.DEFINE}</li>
<li>{fun()}</li>
<li>{$smarty.get.name}</li>
</ol>
</body>
</html>使用smarty可以减少大量的php代码,还可以将php和前台的开发分开,代码也变得优雅易维护
批改老师:韦小宝批改时间:2019-01-17 17:54:54
老师总结:像smarty模板引擎很多后端框架都在用 方便的就是在输出变量的时间比较方便 但是生成变量还是要在后台进行比较好