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. metal &object c

    https://developer.apple.com/documentation/metal/mtlcommandencoder/1458041-pushdebuggroup PushDebugGr ...

  2. Spring框架学习(6)使用ioc注解方式配置bean

    内容源自:使用ioc注解方式配置bean context层 : 上下文环境/容器环境 applicationContext.xml 1 ioc注解功能 注解 简化xml文件配置 如 hibernate ...

  3. C# SQLite 创建数据库的方法增删查改语法和命令

    SQLite介绍 SQLite是一个开源.免费的小型RDBMS(关系型数据库),能独立运行.无服务器.零配置.支持事物,用C实现,内存占用较小,支持绝大数的SQL92标准. SQLite数据库官方主页 ...

  4. http_load压力测试使用

    介绍:http_load以并行复用的方式运行,用以测试web服务器的吞吐量与负载.但是它不同于大多数压力测试工具,它可以以一个单一的进程运行,一般不会把客户机搞死.还可以测试HTTPS类的网站请求. ...

  5. redis学习笔记——RDB和AOF持久化一

    为防止数据丢失,需要将 Redis 中的数据从内存中 dump 到磁盘,这就是持久化.Redis 提供两种持久化方式:RDB 和 AOF.Redis 允许两者结合,也允许两者同时关闭. RDB 可以定 ...

  6. IPhone打包工具脚本

    (后面就是代码了,我就不翻译了.) #!/usr/bin/perl use File::Copy; my $installPath = $ARGV[]; #the name that displays ...

  7. 站点搭建从零開始(五) WordPress的安装

    前面说了非常多废话.如今最终转到正题.WordPress的安装. 1.WordPress安装非常easy 假设你的server能通过应用中心一键安装WordPress,这一节就非常轻松了,基本上不须要 ...

  8. 采用QHD分辨率使用kinect2_calibration,完成QHD图像校正

    //.................................................................................//采用QHD分辨率使用kinec ...

  9. 算法(第四版)C#题解——2.1

    算法(第四版)C#题解——2.1   写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csh ...

  10. SUSE11SP3--安装svn

    SUSE上安装新版本的subversion是一件很麻烦的事情,涉及到的依赖太多,包括需要更新部分工具的版本. 为了以较小的代价安装subversion,我决定在SUSE11下安装 subversion ...