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

<?php

define("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

<?php

function 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

PHP——smarty模板(第一天)的更多相关文章

  1. 12月15日下午Smarty模板函数

    1.{$var=...} 这是{assign}函数的简写版,你可以直接赋值给模版,也可以为数组元素赋值. <{$a = 10}><!--赋值语句--> <{$a}> ...

  2. 12月15日smarty模板基本语法

    smarty基本语法: 1.注释:<{* this is a comment *}>,注意左右分隔符的写法,要和自己定义的一致. <{* I am a Smarty comment, ...

  3. Smarty模板函数

    1.{$var=...} 这是{assign}函数的简写版,你可以直接赋值给模版,也可以为数组元素赋值. <{$a = 10}><!--赋值语句--> <{$a}> ...

  4. 写一个迷你版Smarty模板引擎,对认识模板引擎原理非常好(附代码)

    前些时间在看创智博客韩顺平的Smarty模板引擎教程,再结合自己跟李炎恢第二季开发中CMS系统写的tpl模板引擎.今天就写一个迷你版的Smarty引擎,虽然说我并没有深入分析过Smarty的源码,但是 ...

  5. Smarty模板技术学习

    模板引擎技术:使得php代码和html代码分离的技术就称为"模板引擎技术" 自定义smarty模板技术实现 <?php //迷你smarty原理 class MiniSmar ...

  6. smarty模板原理

    smarty模板原理   模板主要是用来让前端和后端分离的,前台页面只是一个前台页面,后台页面用php代码写逻辑,写完逻辑拿到前台显示. 一.写法 一般需要以下:写3个页面: 1.显示页面aa.htm ...

  7. Smarty模板引擎技术

    Smarty模板引擎技术 什么是模板引擎? 什么是Smarty模板引擎? 为何选择Smarty模板引擎? 如何使用Smarty模板引擎? 一.历史背景 场景一:回顾之前编写PHP项目的方式 //链接数 ...

  8. Ci框架整合smarty模板引擎

    Ci框架整合smarty模板引擎 备注:下载smarty时,最好选择2.6版本,其他测试有坑,ci可以是2.2或其他 大体思路:将smarty封装成ci框架的一个类,然后重新配置一下smarty,这样 ...

  9. smarty模板设计

      一.什么是smarty? smarty是一个使用PHP写出来的模板PHP模板引擎,它提供了逻辑与外在内容的分离,简单的讲,目的就是要使用PHP程序员同美工分离,使用的程序员改变程序的逻辑内容不会影 ...

随机推荐

  1. JS 图片转Base64

    JS 图片转Base64 有时候需要向HTML中插入一张图片,可苦于上线后找不到一个合适的网盘来存储这些图片,有没有一种办法能将图片转换成文字,然后直接插入HTML中呢,通过Base64编码就可以解决 ...

  2. 让网页在ie浏览器下以最高版本解析网页

    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta ht ...

  3. 安装PIL的坑

    今天在centos中使用pip安装PIL死活不成功,报错: Could not find a version that satisfies the requirement PIL (from vers ...

  4. Node.js mm131图片批量下载爬虫1.01 增加断点续传功能

    这里的断点续传不是文件下载时的断点续传,而是指在爬行页面时有时会遇到各种网络中断而从中断前的页面及其数据继续爬行的过程,这个过程和断点续传原理上相似故以此命名.我的具体做法是:在下载出现故障或是图片已 ...

  5. 云计算之路-试用Azure:制作虚拟机自定义镜像

    虚拟机自定义镜像(Image)是一个很有用的功能,可以在一台虚拟机上配置好基本的系统环境,然后做个镜像,以后创建虚拟机直接从这个镜像创建,会省掉很多重复的配置工作. 阿里云与UCloud都有这个功能, ...

  6. iOS12适配指南

    1.重复文件报错 build Phases-> 删除多余的info.plist 2.library not found for -lstdc++ 苹果在XCode10和iOS12中移除了libs ...

  7. 【树莓派】【转载】Raspberry Pi (树莓派)折腾记

    在网上看到一篇对树莓派折腾记录比较详细的文章,时间比较早,但是有些东西没变. 对于新手而言,还是有点参考价值.文章参见:http://skypegnu1.blog.51cto.com/8991766/ ...

  8. PHP投票实现24小时间隔投票

    l  设置cookie,浏览器都有禁用或者清除cookie的功能 l  设置session,关闭浏览器就没了 所以,我们只能尽量防止重复投票现象 session_start(); //获取ip地址 i ...

  9. css zoom与scale区别

    转自:http://www.zhangxinxu.com/wordpress/2015/11/zoom-transform-scale-diff/ 1.zoom的缩放是相对于左上角的:而scale默认 ...

  10. 【DB2】不同编码格式下的汉字所占字节

    UTF-8 (8-bit Unicode Transformation Format) 是一种针对Unicode的可变长度字符编码,又称万国码,它包含全世界所有国家需要用到的字符,是国际编码,通用性强 ...