PHP——smarty模板(第一天)
smarty.class.php 主要的东西放在类里面
templates 放模板
templates_c 放缓存
类里面 $smarty->assign("author","feng and meizi")
$smarty->display("test.tpl")
请求的是test.php 找的是test.tpl文件 test.tpl:<{$author}>
test.tpl文件将所有内容替换完成之后
<div><?php echo $this->tpl_vars["author"];?></div>
存在templates_c文件里的com_test.tpl.php
test.php 里面有个include()函数将页面com_test.tpl.php包含进来
最后显示的是feng and meizi
配置smarty模板
输出配置文件的文件目录
主文件为test.php
配置文件:init.inc.php
定义模板文件夹 templates test.html
定义临时模板存放的文件夹 templates_c
添加模板扩充插件存放目录 plugins
定义缓存文件存放目录 cache
定义模板配置文件存放目录 configs
自定义配置文件 configns文件夹里面的test.conf
系统自带调节器 |truncate:5:"..." 需要三个参数:第一个参数为自身,不用写
配置文件里用:$smarty->addPluginsDir(ROOT.'plugins/');
调节器是以modifier开头的
命名规则:
function smarty_modifiercompiler_lower($params)
{
if (Smarty::$_MBSTRING) {
return 'mb_strtolower(' . $params[0] . ', \'' . addslashes(Smarty::$_CHARSET) . '\')';
}
// no MBString fallback
return 'strtolower(' . $params[0] . ')';
}
自定义插件 1.新建插件目录
2.配置文件中添加插件目录 $smarty->addPluginsDir(ROOT.'plugins/');
3.目录下新建插件文件modifier.mystyle.php
4.写函数
<?php
function smarty_modifier_mystyle($str,$color="yellow",$size="20")
{
$str = "<span style='color:".$color."; font-size:".$size."px'>".$str."</span>";
return $str;
}
?>
Test.php
<?php //加载初始化文件 require "init.inc.php";//用assign()方法将变量植入到模板内$smarty->assign("title","测试网页");$smarty->assign("content","内容"); $smarty->assign("neirong","this is a test demo");$smarty->assign("shuzu",array("one"=>1,"two"=>2,3,4,5));$smarty->assign("shijian",time());@$smarty->assign("get",$_GET);class Ren {public $name ="张三"; } $ren = new Ren();$smarty->assign("ren",$ren);//用smarty对象中的display()方法将网页输出 $smarty->display("Test.html");?>
Test.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}></title> </head><body><{$content}><br /> <{$neirong}><br /> <{print_r($shuzu)}><br /> <{$shuzu.one}><br /> <{$shuzu.two}> <{$ren->name}> <{$smarty.get.name}><!--引用自定义的配置文件--> <{config_load file="test.conf" section="two"}><div style="background-color:<{ #bg# }>; width:100px; height:100px; font-size:<{#size#}>px">好厉害</div> </body><!--cat连接字符串--> <div><{$neirong|cat:"中国"|cat:"china"}></div><!--截取英文的时候按单词来截,中文不存在这种情况--> <div><{$neirong|truncate:6:""}></div><!--这样就可以分开截英文--> <div><{$neirong|truncate:6:true}></div><!--处理时间函数--><!--default设置默认值--><!--调用自定义的调节器--> <div><{$shijian|date_format:"%Y-%m-%d %H:%M:%S "}></div><div><{$aa|default:"hello"}></div><div><{$neirong|mystyle:green:50}></div></html>
init.inc.php
<?phpdefine("ROOT",str_replace("\\","/",dirname(__FILE__)).'/'); //常量ROOT中指定项目根目录require ROOT.'libs/Smarty.class.php'; //加载Smarty类文件$smarty = new Smarty(); //实例化Smarty对象<br>$smarty -> auto_literal = false; //就可以让定界符号使用空格 $smarty->setTemplateDir(ROOT.'templates/'); //设置所有模板文件存放位置 //$smarty->addTemplateDir(ROOT.'templates2/'); //添加一个模板文件夹 $smarty->setCompileDir(ROOT.'templates_c/'); //设置编译过的模板存放的目录 $smarty->addPluginsDir(ROOT.'plugins/'); //设置为模板扩充插件存放目录 $smarty->setCacheDir(ROOT.'cache/'); //设置缓存文件存放目录 $smarty->setConfigDir(ROOT.'configs'); //设置模板配置文件存放目录$smarty->caching = false; //设置Smarty缓存开关功能 $smarty->cache_lifetime = 60*60*24; //设置缓存模板有效时间一天 $smarty->left_delimiter = '<{'; //设置模板语言中的左结束符 $smarty->right_delimiter = '}>'; //设置模板语言中的右结束符?>
modifier.mystyle.php
<?phpfunction smarty_modifier_mystyle($str,$color="yellow",$size="20") {$str = "<span style='color:".$color."; font-size:".$size."px'>".$str."</span>";return $str; }?>
test.conf
[one] bg=red size=50[two] bg=yellow size=24
转载于:https://www.cnblogs.com/Chenshuai7/p/5262236.html
PHP——smarty模板(第一天)相关推荐
- smarty模板基本语法
smarty基本语法: 1.注释:<{* this is a comment *}>,注意左右分隔符的写法,要和自己定义的一致. <{* I am a Smarty comment, ...
- 写一个迷你版Smarty模板引擎,对认识模板引擎原理非常好(附代码)
前些时间在看创智博客韩顺平的Smarty模板引擎教程,再结合自己跟李炎恢第二季开发中CMS系统写的tpl模板引擎.今天就写一个迷你版的Smarty引擎,虽然说我并没有深入分析过Smarty的源码,但是 ...
- PHP Yii框架中使用smarty模板
第一种方法 按照YII系统的办法生成视图觉得有点麻烦,觉得用smarty更省事.尝试着把smarty模板加进来了. date_default_timezone_set("PRC") ...
- Smarty模板技术学习
模板引擎技术:使得php代码和html代码分离的技术就称为"模板引擎技术" 自定义smarty模板技术实现 <?php//迷你smarty原理 class MiniSmart ...
- PHP 自定义 Smarty 模板引擎类 高洛峰 细说PHP
smarty模板引擎类简单工作原理 利用Smarty 模板引擎类对模板文件中的变量进行编译,编译过程其实就是利用正则表达式翻译成PHP文件.例如 模板文件中{$title} 利用正则表达式找到并替换成 ...
- php 高洛峰 正则,PHP 自定义 Smarty 模板引擎类 高洛峰 细说PHP
smarty模板引擎类简单工作原理 利用Smarty 模板引擎类对模板文件中的变量进行编译,编译过程其实就是利用正则表达式翻译成PHP文件.例如 模板文件中{$title}利用正则表达式找到并替换成 ...
- cms概述 。比较shopex和ecshop区别 。smarty模板引擎的入门
cms概述 为了找到一个合适的cms网站系统,我花了一番功夫搜索了多种cms,包括我用过的和没用过的,知道的和不知道的,当然,必须是开源的.免费的.生成静态页面的.到各自的官方网站,查看了有关资料,下 ...
- 陈力:传智播客古代 珍宝币 泡泡龙游戏开发第52讲:PHP仿smarty模板的实现
陈力:传智播客古代 珍宝币 泡泡龙游戏开发第52讲:PHP仿smarty模板的实现 描述: smarty模板技术为PHP程序设计中处理和界面的分离提供了很大的便利,有必要了解其设计思路和底层机制,本文 ...
- 陈力:传智播客古代 珍宝币 泡泡龙游戏开发第53讲:PHP smarty模板配置及变量操作
陈力:传智播客古代 珍宝币 泡泡龙游戏开发第53讲:PHP smarty模板配置及变量操作 描述: smarty模板技术为PHP程序设计中处理和界面的分离提供了很大的便利.如果分配整型.小数.布尔型. ...
最新文章
- 一场直播教你深挖6万篇论文+50万学者信息,瓜分10万元奖金
- VTK:多个视口用法实战
- Win7下Anaconda3+Tensorflow
- github创建一个新的tag
- 第四节: Quartz.Net五大构件之Trigger通用用法(常用方法、优先级、与job关联等)
- 编写并调试一个单道处理系统的作业调度模拟程序_操作系统系列(2):操作系统发展历史...
- c++11 future promise
- 第12章[12.3] Ext JSGrid中的超链接列及其功能实现
- js in html5,CSS-in-JS 来做的 5 件事情,一般人都不知道!
- centos查看磁盘转速_Linux 磁盘管理
- Centos 启用网卡出现 no link present. Check cable
- Lyndon Word学习笔记
- 中科院计算机考研对外调剂吗,考研调剂信息:2020年中科院接收调剂研究生!...
- VC6.0无法进入调试
- 力扣题目系列:1299. 将每个元素替换为右侧最大元素 -- 一道算法优化入门题
- LeetCode - 709. To Lower Case
- 复选框选中的值怎么传到mysql_PHP中如何将复选框选中的内容提交到mysql的一个字段中...
- Android P版本怎么简单的验证HIDL的Demo例程
- 细说php第四版笔记,细说PHP 学习笔记(二)
- Excel函数公式大全—IF家族函数