Smarty变量
先来看一下smarty的注释方法
然后运行,查看一下源代码
html和smarty的注释区别就是源代码中能看见html的注释代码,而看不到smarty里面的注释代码
再来看看,smarty中能读字符串,看看能不能把数组读出来
php文件中
<?php include("../init.inc.php"); $smarty->assign("ceshi","你好"); //注册变量的方法 $arr = array(1,2,3,4,5); $smarty->assign("shuzu",$arr); $smarty->display("test.html");
html文件中
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <{$ceshi}> <{$shuzu[0]}> </body> </html>
看一下运行的结果
把数组成功取出来了
再来看取关联数组
php文件
<?php include("../init.inc.php"); $smarty->assign("ceshi","你好"); //注册变量的方法 $arr = array("one"=>"hello","two"=>"world"); $smarty->assign("shuzu",$arr); $smarty->display("test.html");
html文件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <{$ceshi}> <{$shuzu["one"]}> </body> </html>
运行一下
也成功的输出了
smarty中也支持点语法,如图
.one也可以成功输出
取two的值就.two
取到的值
再来看看对象支不支持
<?php include("../init.inc.php"); $smarty->assign("ceshi","你好"); //注册变量的方法 class Ren { public $name="zhangsan"; } $r=new Ren(); $smarty->assign("r",$r); $smarty->display("test.html");
html中
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <{$ceshi}> <{$r->name}> </body> </html>
运行后
说明这个变量是支持任何类型的
有一个标签,可以让里面的内容不参与解析
运行后
被原样输出了
这个标签可以加在js或css外层,为了防止解析出错
再来试一下,通过配置文件来做元素的样式
配置文件是写在这个目录下的
自定义一个文件,censhi 配置文件的后缀都是.conf
里面随便写两个样式
然后还是用test.php和test.html 这两个文件来做
html里面的内容
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <{config_load file='ceshi.conf'}> <!--调了一个函数,传了一个参数--> <div style="width:100px; height:100px; background-color:<{#color#}>"> </div> </body> </html>
php文件中
<?php include("../init.inc.php"); $smarty->display("test.html");
运行后
在从配置文件中改个颜色试试
其他两个文件的内容不用动
运行后
配置文件中还可以这样写
相当于把这些样式分了一些类
来看一下配置文件中有两个color该怎么取
如果取第二个的话在引入配置文件时后面还需要加上这样一句话
运行后看看
取到了two下面的color,为红色
还可以这样改
运行后也是红色
把上面改成one,那么取到的就是绿色
所以这两种方法都可以用
{$smarty.const} 取常量
举个例子来看看
php文件中
<?php include("../init.inc.php"); define("AA","nihao"); $smarty->display("test.html");
html中
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <{config_load file='ceshi.conf' section='one'}> <!--调了一个函数,传了一个参数--> <div style="width:100px; height:100px; background-color:<{$smarty.config.color}>"> </div> <{$smarty.const.AA}> </body> </html>
看看运行的结果
取到了AA的值
所以获取常量的方法就是
$smarty.const.常量名
这个运行后
输出的是右分隔符
把r换成l试试
运行后
输出的是左分隔符
再看一下取时间怎么取
php文件中
<?php include("../init.inc.php"); $sj=time(); $smarty->assign("sj","$sj"); $smarty->display("test.html");
html文件中
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <{$sj}> </body> </html>
先来运行一下
取到的是时间戳
php文件不用改变
html文件中要这样写才能把时间调出来
运行后
时间按照代码的格式显示出来
这是变量调节器,相当于一个函数
smarty可以支持自定义变量调节器
在这个目录下有很多函数
modifier打头的是变量调节器
function打头的是函数
block打头的是快函数
如果要使用自定义变量调节器的话,可以自己新建一个文件
这个文件就是新建的,要注意格式要对,modifier是打头的,中间是随便给他起的一个名字
然后在里面写上简单的内容,可以参照它原来有的变量调节器文件来写
然后php文件中
html文件中
运行一下
mark标签是标记的意思
如果把html文件中的mark去掉的话
运行后
就没有黄色标记了
调节器文件中,至少有一个参数,多了不限
这个也可以把当前时间调出来
还是用原来的文件,把数据都改一下就可以
php文件不用改
html文件
运行后
当前时间调取成功
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?