SMARTY的知识
smarty的原理:

<?php
class Smarty
{
$ldelimiter = "{";//左分隔符
$rdelimiter = "}";//友分隔符 $attr = array();//用来存贮变量的数组 //向模板里面注册变量
function assign($key,$value)
{
$this->attr[$key] = $value;
} //显示模板的方法
function display($url)
{
//1.获取静态模板的内容
$str = file_get_contents($url);
//2.根据正则匹配str里面出现的所有{}里面的内容
//{$a} -> <?php echo $attr[$a] ?> //3.将替换好的页面保存临时文件
touch(".test.php");
file_put_contents(); //4.将临时文件拿到当前页面显示
include(".test.php");
}
}
访问的是PHP文件,其中路径就按照访问的PHP文件来定。但是显示的是html。
test.php
test.php
<?php
include("../init.inc.php");
//注册变量
$smarty->assign("name","张三");
$smarty->assign("arr",array(1,2,3,4,5));
$smarty->assign("test","my name is zhangsan");
$smarty->assign("title",啊啊啊啊啊啊);
//显示模板
$smarty->display("test.html");
test.html
<html>
<head></head>
<body>
<{$name}>
<{$arr.one}>
<{config_load file='test.conf'} section='one'>
<div style="width:20px;height:20px;background-color:<{#color#}>"></div>
<{$smarty.now|date_format:%Y-%m-%d %H:%M:%S}>
<{$test|truncate:5}>//截取字符串
<{date name ="riqi" value="2017-2-3" time=true}>
<{color name="color"}>
<{textarea name="txt" toolbar="full" color="red"}>
<{/textarea}>//块函数要有首和尾
<{cishu num=3}>
<{font da=50}>
电话卡会打架
<{/font}>
</body>
</html> $smarty.const.XXX 取常量
$smarty.config.color 和 #color#一样
test.conf
[one]
color=red
[two]
color=green
自定义变量调节器modifier.keyword.php
<?php
function smarty_modifier_keyword($str,$key)
{
return str_replace($key,"<mark>{$key}</mark>",$str);
}
自定义函数function.cishu.php
//num参数代表输出次数
function smarty_function_cishu($args,$smarty)
{
$num = $args["num"]; $str = ""; for($i=0;$i<$num;$i++)
{
$str = $str."<div style='width:100px;height:100px;background-color:green'></div>";
}
return $str;
}
自定义块函数block.font.php
<?php
function smarty_block_font($args,$content,$smarty,$a)
{
if(!$a)//走头的时候没有内容,走尾的时候把内容改变
{
$da = $args["da"]; return "<span style='font-size:{$da}px'>{$content}</span>";
}
}
显示结果



SMARTY的知识的更多相关文章
- php常见面试(Smarty及相关知识)
1.Smarty简介: 是一种php模板引擎,它分开了后台程序和前台页面内容,使程序员和美工分工合作.还有其它模版引擎实现同样的目的. 2.Smarty使用: 建立php文件: 包含smarty类文件 ...
- smarty模板基础知识
1.定义 Smarty是一个使用php写出来的模板引擎,它分离了逻辑代码和外在的内容,提供了一种易于管理和使用的方法,用来将原本与html代码混杂在一起PHP代码逻辑分离. 简单的讲,目的就是要使PH ...
- smarty学习——高级知识
1.Objects 对象 smarty允许通过模板访问PHP对象.有两种方式来访问它们.一种是注册对象到模板,然后通过类似于用户自定义函数的形式来访问它. 另一种方法给模板分配对象,然后通过访问其它赋 ...
- smarty学习——编程知识
smarty 提供了丰富的api 接口可以方便我们进行操作: 1.clear_all_assign清除所有赋值 2.clear_all_cache清除所有缓存 3.clear_assign清除赋值 4 ...
- smarty模板引擎(一)基础知识
一.基本概念 1.什么是mvc? mvc是一种开发模式,核心思想是:数据的输入.数据的处理.数据显示的强制分离. 2.什么是smarty? smarty是一个php的模板引擎.更明白的 ...
- smarty 快速上手
smarty半小时快速上手入门教程 投稿:shichen2014 字体:[增加 减小] 类型:转载 时间:2014-10-27我要评论 这篇文章主要介绍了smarty半小时快速上手入门教程,以实例的形 ...
- smarty 学习记录
smarty模版是比较大众化的一个模版,在php开发过程当中被很多开发者视为最友好的模版之一,学习smarty课程对于很多培训机构来说也是列入了培训课程之一,那么很多方面就需要我们学习了一. 安装首先 ...
- smarty半小时快速上手入门教程
http://www.jb51.net/article/56754.htm http://www.yiibai.com/smarty/smarty_functions.html http://www. ...
- (转)PHP模板smarty简单入门教程
转之--http://blog.163.com/zf_2011@126/blog/static/166861361201062595057962/ 如何在smarty中开始我们程序设计.PHP代码:- ...
随机推荐
- 【转载】 180623 Conda install 本地压缩包文件tar.bz2
原文地址“ https://blog.csdn.net/qq_33039859/article/details/80785535 ----------------------------------- ...
- ROS学习笔记(一)
运行ROS例程(turtlesim)1)安装turtlesim包sudo apt-get install ros-kinetic-turtlesim2)运行管理器节点roscore3)运行turtle ...
- 用KNN实现iris的4分类问题&测试精度
import matplotlib.pyplot as plt from scipy import sparse import numpy as np import matplotlib as mt ...
- PP篇9 更改计划订单
首先看了下这个函数的版本 还挺新的 S4版本应该是有更新过,里面用了新语法 导入: PLANNEDORDER 计划订单号HEADERDATA 表头数据(来自外部系统) HEA ...
- 实现不同的项目,用不同的git 账号提交
可以全局配置一个git 账户名和密码,然后在具体项目里单独配置一个账户名和密码 例如: git config --global user.name "winyh" git conf ...
- 复习sql server 2000 MSDE2000 ,安装,打补丁,开1433端口。
前言: 由于工作需要,一个仍在使用的古董软件需要安装配置. 原有系统是在主机部署,单机安装sql2000,其他机器(分机)也单机部署软件,部署sql2000,通过dtsrun /f sync.dts( ...
- expect 实现自动交互脚本
1. 说明 在编写脚本时,可能会遇到需要在另一台主机上执行一个命令,或者在本机拷贝另一台主机内的一个文件.如果两台主机之间没有做互信,就会牵扯到用户输入密码的交互过程,这对编写自动脚本来说, 就行不通 ...
- R去掉含有NA的行
只要数据框中含有NA的行都去掉 final[complete.cases(final),] na.omit(final) 过滤某几列 final[complete.cases(final[,5:6]) ...
- Java静态分派和动态分派
前言 动态分派和静态分派机制是Java多态实现的原理.本文将针对这两种机制进行浅析. 静态分派 静态分派机制最典型的代码示例如下 void test() { Father father = new S ...
- Markdown 语法 (转载)
Markdown 语法整理大集合2017 1.标题 代码 注:# 后面保持空格 # h1 ## h2 ### h3 #### h4 ##### h5 ###### h6 ####### h7 // ...