smarty引入并输出常见数据类型

Original 2019-02-28 14:07:45 308
abstract:/* * config.php文件代码 */<?php/** * Smarty 配置文件 */require __DIR__."/../smarty-3.1.33/libs/Smarty.class.php";//创建smarty模板引擎对象$smarty = new Smarty();//echo '默认模板目录是:'.pr

/*

 * config.php文件代码

 */

<?php
/**
* Smarty 配置文件
*/
require __DIR__."/../smarty-3.1.33/libs/Smarty.class.php";
//创建smarty模板引擎对象
$smarty = new Smarty();

//echo '默认模板目录是:'.print_r($smarty->getTemplateDir(),true).'<hr>';
//echo '默认模板编译目录是:'.$smarty->getCompileDir();

//配置四个目录
//模板文件所在目录
$smarty->setTemplateDir(__DIR__.'/../temp');
//编译文件所在目录
$smarty->setCompileDir(__DIR__.'/../temp_c');
//配置缓存目录
$smarty->setCacheDir(__DIR__.'/../cache');
//配置目录
$smarty->setConfigDir(__DIR__.'/../config');


//可选配置
$smarty->setLeftDelimiter('{');//变量左定界符
$smarty->setRightDelimiter('}');//变量右定界符


//配置缓存
//$smarty->setCaching(false);//关闭缓存
//$smarty->setCacheLifetime(60*60*24);//设置缓存有效期


/*

 * demo.php文件代码

 */

<?php
//开启session
session_start();
//加载smarty
require __DIR__.'/config/config.php';

//1.显示单值变量:标量
$name = '小龙女';

//2.数组:索引数组
$courses = ['html5','css3','jquery','php','mysql'];

//3.数组:关联数组
$book = ['name'=>'PHP开发从入门到放弃','price'=>69,'publish'=>'2018-04-22'];

//4.多维数组
$books = [
   ['name'=>'PHP开发从入门到放弃','price'=>69,'publish'=>'2018-04-22'],
   ['name'=>'mysql性能分析','price'=>99,'publish'=>'2018-07-12'],
   ['name'=>'javascript高级程序设计','price'=>129,'publish'=>'2016-10-22'],
];

//5.对象
class Test
{
   public $site = 'PHP中文网';
   public function welcome()
   {
       return '欢迎来我家!';
   }
}
$test = new Test();

//6.自定义函数
function add($a, $b){
   return $a+$b;
}

//7.常量,不需要赋值,直接在摸板中输出
const SITE_NAME = 'php中文网';

//8.系统变量,不需要赋值,直接在摸板中输出
$_POST['username'] = '超级管理员';
$_GET['page'] = 10;
$_SESSION['pass'] = sha1('123456');



//模板赋值
$smarty->assign('name', $name);
$smarty->assign('courses', $courses);
$smarty->assign('book',$book);
$smarty->assign('books',$books);
$smarty->assign('test',$test);


//模板渲染
$smarty->display('demo3.html');


/*

 * demo.html模板文件代码

 */

{* 注释:显示变量 *}
<h3>我的女神:{$name}</h3>{*//变量名与定界符之间不能有空格*}

{*显示索引数组元素*}
<p>前端课程:{$courses[0]},{$courses[1]},{$courses[2]}</p>
<p>前端课程:{$courses.0},{$courses.1},{$courses.2}</p>

{*显示关联数组元素*}
<p>书名:《{$book.name}》,价格:{$book.price}元,出版时间:{$book.publish}</p>

{*显示多维数组元素*}
<p>书名:《{$books.1.name}》,价格:{$books.1.price},出版时间:{$books.1.publish}</p>

<ul>
   <li>书名:《{$books.1.name}》,价格:{$books.1.price},出版时间:{$books.1.publish}</li>
   <li>书名:《{$books.1.name}》,价格:{$books.1.price},出版时间:{$books.1.publish}</li>
   <li>书名:《{$books.1.name}》,价格:{$books.1.price},出版时间:{$books.1.publish}</li>
</ul>


{*显示对象中的成员*}
<p>站点名称:{$test->site}</p>
<h3>{$test->welcome()}</h3>


{*访问自定义函数*}
<p>求和:{add(5,8)}</p>
<p>求和:{add($books.1.price,$books.2.price)}&nbsp;//参数中可引用变量</p>


{*显示常量*}
<p>站点常量:{$smarty.const.SITE_NAME}</p>


{*显示系统变量*}
<p>post提交的用户名:{$smarty.post.username}</p>
<p>get提交的用户名:{$smarty.get.page}</p>
<p>session会话中的密码:{$smarty.session.pass}</p>

Correcting teacher:天蓬老师Correction time:2019-02-28 14:34:01
Teacher's summary:smarty, 模板引擎,用来在服务器沉浸html页面非常方便,语法也简单, 适合前端学习与理解

Release Notes

Popular Entries