smarty基础知识详解
来源:互联网 发布:硬盘 检测 软件 编辑:程序博客网 时间:2024/09/21 06:32
这是一篇写给小白的smarty入门教程。从最基础的部分带你入门smarty
smarty基础原理
一、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>
<div>{$title}</div>
<div>{$content}</div>
</body>
</html>
二、PHP后台代码
<?php//连接数据库,获得具体数据
//1.引入迷你smartrequire ("minismart.class.php");//2实例化Smarty对象$smarty=new minismart();//将字符串信息设置为模板引擎类的属性信息$smarty->assign("title","qqq");$smarty->assign("content","aa");
//3调用compile方法,同时传递ceshi.html模板文件参数
//在该方法中把ceshi.html内部标记替换为php标记$smarty->compile("ceshi.html");
三、模板引擎
<?php//模板引擎类class minismart
{
//给该类声明属性,用于储存外部的变量信息
public $tpl_var=array();
//把外部变量设置成内部变量的一部分
function assign($k,$v)
{
$this->tpl_var[$k]=$v;
}
//"编译模板文件({}标记替换为php标记)"
function compile ($tpl) //compile编译
{ //获得模板文件内部具体内容
$cont= file_get_contents($tpl);//file_get_contents()获取内容
//替换 { ---> <?php echo $this->tpl_var["
$cont=str_replace("{\\$","<?php echo \\$this->tpl_var[\\"",$cont);
//替换 } --->"]; ? >
$cont=str_replace("}","\\"]; ?>",$cont);
//把生成好的编译内容(php + html 混编内容)放入一个文件里面
file_put_contents("./shili.html.php",$cont);
//引入混编文件
include ("./shili.html.php");
}
}
四、混编代码页面
<!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>
<div><?php echo $this->tpl_var["title"]; ?></div>
<div><?php echo $this->tpl_var["content"]; ?></div>
</body>
</html>
原文来自:博客园/By_The_Way
- smarty基础知识详解
- smarty 详解
- Smarty模板技术基础知识
- PHP Smarty模版基础知识
- smarty模板引擎基础知识
- SMARTY分页详解
- SMARTY分页详解
- Smarty循环实例详解
- Smarty 过滤器详解
- smarty --foreach详解
- smarty --foreach详解
- smarty缓存技术详解
- smarty --foreach详解
- smarty --foreach详解
- smarty缓存详解
- 如何使用smarty详解
- Smarty配置详解
- smarty模板引擎(一)基础知识
- JS 字符串操作API
- 小红书架构演进
- Linux进程间通信——使用信号
- Mac下修改Android Studio的默认JDK版本,与eclipse产生的问题冲突
- 根据进程句柄 获得可执行文件路径 的几种方法
- smarty基础知识详解
- (论文+代码)R-FCN:基于区域的全卷积神经网络进行物体检测
- HTML&CSS基础学习笔记1.22-文章布局
- Axure插入动态图表
- Layout _width ,Layout_height和heigh ,width 区别:
- C#控制台基础 返回类型位void的函数用return终止函数
- 手机app连接电脑的JavaWeb
- 【Android】双击事件
- XMLHttpRequest上传文件