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的知识的更多相关文章

  1. php常见面试(Smarty及相关知识)

    1.Smarty简介: 是一种php模板引擎,它分开了后台程序和前台页面内容,使程序员和美工分工合作.还有其它模版引擎实现同样的目的. 2.Smarty使用: 建立php文件: 包含smarty类文件 ...

  2. smarty模板基础知识

    1.定义 Smarty是一个使用php写出来的模板引擎,它分离了逻辑代码和外在的内容,提供了一种易于管理和使用的方法,用来将原本与html代码混杂在一起PHP代码逻辑分离. 简单的讲,目的就是要使PH ...

  3. smarty学习——高级知识

    1.Objects 对象 smarty允许通过模板访问PHP对象.有两种方式来访问它们.一种是注册对象到模板,然后通过类似于用户自定义函数的形式来访问它. 另一种方法给模板分配对象,然后通过访问其它赋 ...

  4. smarty学习——编程知识

    smarty 提供了丰富的api 接口可以方便我们进行操作: 1.clear_all_assign清除所有赋值 2.clear_all_cache清除所有缓存 3.clear_assign清除赋值 4 ...

  5. smarty模板引擎(一)基础知识

    一.基本概念 1.什么是mvc?     mvc是一种开发模式,核心思想是:数据的输入.数据的处理.数据显示的强制分离. 2.什么是smarty?     smarty是一个php的模板引擎.更明白的 ...

  6. smarty 快速上手

    smarty半小时快速上手入门教程 投稿:shichen2014 字体:[增加 减小] 类型:转载 时间:2014-10-27我要评论 这篇文章主要介绍了smarty半小时快速上手入门教程,以实例的形 ...

  7. smarty 学习记录

    smarty模版是比较大众化的一个模版,在php开发过程当中被很多开发者视为最友好的模版之一,学习smarty课程对于很多培训机构来说也是列入了培训课程之一,那么很多方面就需要我们学习了一. 安装首先 ...

  8. smarty半小时快速上手入门教程

    http://www.jb51.net/article/56754.htm http://www.yiibai.com/smarty/smarty_functions.html http://www. ...

  9. (转)PHP模板smarty简单入门教程

    转之--http://blog.163.com/zf_2011@126/blog/static/166861361201062595057962/ 如何在smarty中开始我们程序设计.PHP代码:- ...

随机推荐

  1. Python3基础 print 格式化输出 %% 输出%

             Python : 3.7.3          OS : Ubuntu 18.04.2 LTS         IDE : pycharm-community-2019.1.3    ...

  2. webpack 相关文章

    webpack loader原理 由于webpack是基于Node的所以webpack只能识别.js文件,所以针对其他的文件就需要转译,这时候就需要用到我们的loader了. https://blog ...

  3. g++编译时遇到问题undefined reference to

    文件目录结构体为: src 和include 分别用来存放.cpp文件和 .hpp文件 其中:src文件夹下有需要的文件 simulator_client.cpp crc32.cpp : includ ...

  4. 三种构建方式:Makefile、scons、scons cmake+ninja

    三种构建方式: Makefile scons cmake+ninja https://ninja-build.org

  5. How to Plan and Configure YARN and MapReduce 2 in HDP 2.0

    As part of HDP 2.0 Beta, YARN takes the resource management capabilities that were in MapReduce and ...

  6. Tomcat教程(转)

    转载链接: https://www.cnblogs.com/jingmoxukong/p/8258837.html?utm_source=gold_browser_extension 简介 Tomca ...

  7. 清除 sql server 记住密码

    引用:https://www.cnblogs.com/zengbin/p/4307013.html SQL Server 2005 Management Studio %AppData%\Micros ...

  8. java程序cpu问题排查

    方法一: 转载:http://www.linuxhot.com/java-cpu-used-high.html 1.jps 获取Java进程的PID. 2.jstack pid >> ja ...

  9. stochastic noise and deterministic noise

    在机器学习中,导致overfitting的原因之一是noise,这个noise可以分为两种,即stochastic noise,随机噪声来自数据产生过程,比如测量误差等,和deterministic ...

  10. 你应该知道的4个DSP开发支持库

    引言 在dsp开发中,为了节省开发时间和难度,TI将一些成熟的算法封装为模块,供开发者使用.如果能充分利用这些算法支持库,对于加快dsp开发进程与提高代码质量.稳定性有非常大的帮助. Digital ...