smarty 模板php,Smarty模板快速入门
在PHP的世界里已经出现了各式各样的模板类,但就功能和速度来说Smarty还是一直处于领先地位,因为Smarty的功能相对强大,所以使用起来比其他一些模板类稍显复杂了一点。现在就用30分钟让您快速入门。
一. 安装
首先打开网页http://smarty.php.net/download.php,下载最新版本的Smarty。解压下载的文件(目录结构还蛮复杂的)。接下来我演示给大家一个安装实例,看过应该会举一反三的。
(1) 我在根目录下建立了新的目录learn/,再在learn/里建立一个目录smarty/。将刚才解压缩出来的目录的libs/拷贝到smarty/里,再在smarty/里新建templates目录,templates里新建cache/,templates/,templates_c/, config/.
(2) 新建一个模板文件:index.tpl,将此文件放在learn/smarty/templates/templates目录下,代码如下:
HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
Smarty
{$hello}
新建index.php,将此文件放在learn/下:
//引用类文件
require 'smarty/libs/Smarty.class.php';
$smarty = new Smarty;
//设置各个目录的路径,这里是安装的重点
$smarty->template_dir = "smarty/templates/templates";
$smarty->compile_dir = "smarty/templates/templates_c";
$smarty->config_dir = "smarty/templates/config";
$smarty->cache_dir = "smarty/templates/cache";
//smarty模板有高速缓存的功能,如果这里是true的话即打开caching,但是会造成网页不立即更新的问题,当然也可以通过其他的办法解决
$smarty->caching = false;
$hello = "Hello World!";
//赋值
$smarty->assign("hello",$hello);
//引用模板文件
$smarty->display('index.tpl');
?>
(3) 执行index.php就能看到Hello World!了。
二. 赋值
在模板文件中需要替换的值用大括号{}括起来,值的前面还要加$号。例如{$hello}。这里可以是数组,比如{$hello.item1},{$hello.item2}…
而PHP源文件中只需要一个简单的函数assign(var , value)。
简单的例子:
*.tpl:
Hello,{$exp.name}! Good {$exp.time}
*.php:
$hello[name] = “Mr. Green”;
$hello[time]=”morning”;
$smarty->assign(“exp”,$hello);
output:
Hello,Mr.Green! Good morning
三. 引用
网站中的网页一般header和footer是可以共用的,所以只要在每个tpl中引用它们就可以了。
示例:*.tpl:
{include file="header.tpl"}
{* body of template goes here *}
{include file="footer.tpl"}
四. 判断
模板文件中可以使用if else等判断语句,即可以将一些逻辑程序放在模板里。"eq", "ne", "neq", "gt", "lt", "lte", "le", "gte" "ge", "is even", "is odd", "is not even", "is not odd", "not", "mod", "div by", "even by", "odd by","==","!=",">", "="这些是if中可以用到的比较。看看就能知道什么意思吧。
示例:
{if $name eq "Fred"}
Welcome Sir.
{elseif $name eq "Wilma"}
Welcome Ma'am.
{else}
Welcome, whatever you are.
{/if}
五. 循环
在Smarty里使用循环遍历数组的方法是section,如何赋值遍历都是在模板中解决,php源文件中只要一个assign就能解决问题。
示例:
{* this example will print out all the values of the $custid array *}
{section name=customer loop=$custid}
id: {$custid[customer]}
{/section}
OUTPUT:
id: 1000
id: 1001
id: 1002
六. 常见问题
Smarty将所有大括号{}里的东西都视为自己的逻辑程序,于是我们在网页中想插入javascript函数就需要literal的帮忙了,literal的功能就是忽略大括号{}。
示例:
{literal}
function isblank(field) {
if (field.value == '')
{ return false; }
else
{
document.loginform.submit();
return true;
}
}
{/literal}
smarty 模板php,Smarty模板快速入门相关推荐
- go语言 html 模板语法,go语言快速入门:template模板
使用过AngularJS应该都会对ng-model的方式印象深刻,类似于Spring的值绑定,使用起来非常的方便.而go的标准库中也有相关的功能为Web开发提供简单但却有非常有用的功能,这就是temp ...
- Smarty中文手册,Smarty教程,Smarty模板的入门教材
Smarty中文手册,Smarty教程,Smarty模板的入门教材 首先,这份Smarty中文手册的翻译工作是由喜悦国际村村民自发组织的,不代表任何人的意见和观点.对他们的无私奉献精神,我们表示感谢, ...
- php smarty 模版引擎,php模板引擎Smarty入门 - 小俊学习网
如果要实现前端模板和后台逻辑业务处理相分离,MVC的思想开发网站的话,可使用模板引擎来完成.来学习php模板引擎Smarty,它分开了逻辑程序和外在的内容,提供了一种易于管理的方法. 1. 什么是模板 ...
- php smarty入门,smarty 快速入门
smarty 快速入门 smarty 定义:一个开源的模板引擎 模板引擎是为了使用户界面与业务数据分离而产生的,它可以生成特定格式的文档,用于网站的模板引擎就会生成一个标准的HTML文档. 功能 将网 ...
- PHP模板引擎Smarty概述
PHPChina资讯:Smarty是由Monte Ohrt和Andrei Zmievski写出的PHP模板引擎.它不同于其他的模板引擎,比如FastTemplate或者patTemplate,其中主要 ...
- php基础(09):php模板引擎Smarty
1.概述: 官方:Smarty是一个PHP的模板引擎.业务逻辑和显示逻辑分离,是Smarty的一个设计理念. 啥意思?就是准备数据,给smarty显示.smarty里面写好html,css,js等前端 ...
- PHP模板引擎Smarty详解
Smarty是由Monte Ohrt和Andrei Zmievski写出的PHP模板引擎.它不同于其他的模板引擎,比如FastTemplate或者patTemplate,其中主要原因就是Smarty会 ...
- php smarty继承,PHP Smarty 模板 模板继承 {extends}
{extends} 模板继承中,你可以在子模板内使用{extends}标签来扩展父模板. 1.{extends}必须放在模板的第一行. 2.如果子模板要用{extends}来扩展父模板,那么它只能有 ...
- php模板技术smarty,PHP模板技术Smarty
基本信息 讲师: 高洛峰 时长:60分钟 集数:9 每集定价:20 元 描述: 如果你正在设计一个交互式的网站,你一定会关注两个主要的问题,就是界面美工和应用程序.在大多数的项目组中,开发一个Web程 ...
- php模板引擎循环start,PHP模板引擎Smarty内建函数section,sectionelse用法详解
本文实例讲述了PHP模板引擎Smarty内建函数section,sectionelse用法.分享给大家供大家参考,具体如下: section 是 Smarty 模板中除了 foreach 以外的另一种 ...
最新文章
- layui跳转html如何带参数,Layui跳转页面代码(可携带复杂参数)
- Swift之Vision 图像识别框架
- 学点 C 语言(20): 数据类型 - 指针
- Git的安装(附安装包)
- a href=#与 a href=javascript:void(0) 的区别
- 性能监控工具javamelody与spring的集成
- 【ICLR2020】会议的16篇最佳深度学习论文
- 可展开的UITableViewCell
- C#中Socket用法,多个聊天和单一聊天。
- HDU - 5950 Recursive sequence(矩阵快速幂)
- 北京大学 | 软件工程理论与实践
- html5实现直接下载文件
- web.xml中配置启动时加载的servlet,load-on-starup
- echart——时间点和时间段综合显示
- 【JZOJ】3424. 粉刷匠
- Windows下的SysWow64和System32
- 华为模拟器eNSP配置DHCP自动分配IP地址
- Mysql插入数据 Incorrect string value: '\xF0\x9F\x98\x84
- 伪原创文章需要注意的地方有哪些
- 无损数据压缩算法c语言,短短的C代码,帮忙,看看压缩算法,感激不尽