在前端开发人员的角度使用smarty。包括定界符、注释、变量和函数。
1.定界符 {}
注意事项:
1.1 任何在定界符以外的都是静态资源都不会被解析。
1.2 ‘{’ 跟‘$’ 不能同游空格
1.3 如果遇到 style 以及js 中 的{}改如何避免。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>learn day one </title>
<style>
{*
1. 空格
2. literal
3. 外部引用
*}
{literal}
b{color: red}
{/literal}
</style>
</head>
<body>
欢迎你<b>{$name}</b>来到smarty模板世界;
</body>
</html>
1.4 默认定界符可以修改
php代码
<?php
// 1.引用smarty类
require './libs/Smarty.class.php';
// 2.实例化snarty 对象
$smarty = new Smarty();
// 3.设置相关属性
$smarty->template_dir = "templates";
$smarty->compile_dir = "templates_c";
// 4.修改定界符
$smarty->left_delimiter='<{';
$smarty->right_delimiter='}>';
$content = "admin";
$smarty->assign('name',$content);
$smarty->display("index.html");
html 代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>learn day one </title>
<style>
<{*
1. 空格
2. literal
3. 外部引用
*}>
<{literal}>
b{color: red}
<{/literal}>
</style>
</head>
<body>
欢迎你<b><{$name}></b>来到smarty模板世界;
</body>
</html>
2.注释
定界符加* 列入 {* *};
3.变量
变量就是在模板中可以使用某一个符号,他保存的一些数据。
变量的来源
3.1 通过PHP程序中的assign函数分配过来 (重点使用的最多)。
# php的数据类型 =》
# 422 阵容。
# 4: 四种变量类型 整形 浮点型 字符串类型 布尔型
# 2: 两种复核类型 数组 对象
# 2: 两种特殊类型 资源 null
#标量 简单说就是表示单个的值 $a = 100,$b='smarty';
#复核类型 意味着的是一个变量里面表示多重的变量类型 $arr = array(100,'smarty');
# 在smarty 中 不适合 资源,对象,null的类型;
# 结论 标量 数组是经常使用的
assign.php代码 如下:
<?php
# php的数据类型 =》
# 422 阵容。
# 4: 四种变量类型 整形 浮点型 字符串类型 布尔型
# 2: 两种复核类型 数组 对象
# 2: 两种特殊类型 资源 null
#标量 简单说就是表示单个的值 $a = 100,$b='smarty';
#复核类型 意味着的是一个变量里面表示多重的变量类型 $arr = array(100,'smarty');
# 在smarty 中 不适合 资源,对象,null的类型;
# 结论 标量 数组是经常使用的
require './libs/Smarty.class.php';
$smarty = new Smarty();
$smarty->template_dir = "templates/";
$smarty->compile_dir = "templates_c";
$title = "assign--title";
$user = array("乔峰","段誉","慕容复","虚竹");
$hero = array("id"=>1,'username'=>"乔峰","job"=>"丐帮帮主");
$smarty->assign("title",$title);
$smarty->assign("totle",100);
$smarty->assign("love",false);
$smarty->assign("user",$user);
$smarty->assign("hero",$hero);
$smarty->display("assign.tpl");
# 猜想 false 显示成什么?
# {}定界符 打印 echo, echo在输出的时候吧变量的值转化成字符串进行输出
# 所以 false ==> '' 进而模板中显示是空白的 不是0
assign.tpl代码 如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>{$title}</title>
<style>
</style>
</head>
<body>
{$title}
</br>
{$totle}
</br>
{$love}
<h2>索引数组</h2>
<ul>
<li>{$user[0]}</li>
<li>{$user[1]}</li>
<li>{$user[2]}</li>
<li>{$user[3]}</li>
</ul>
<h2>关联数组</h2>
<ul>
<li>{$hero['id']}</li>
<li>{$hero['username']}</li>
<li>{$hero['job']}</li>
</ul>
</body>
</html>
3.2 保留变量。
其中保留变量和配置变量无需再php中分配,直接在模板中使用。
3.3 配置变量。