smarty使用
来源:互联网 发布:php表格代码 编辑:程序博客网 时间:2024/04/30 09:19
smarty模版是比较大众化的一个模版,在php开发过程当中被很多开发者视为最友好的模版之一,学习smarty课程对于很多培训机构来说也是列入了培训课程之一,那么很多方面就需要我们学习了
一. 安装
首先打开网页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目录下,代码如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type" c>
<title>Smarty</title>
</head>
<body>
{$hello}
</body>
</html>
新建index.php,将此文件放在learn/下:
<?php
//引用类文件
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 notodd", "not", "mod", "div by", "evenby", "odd by","==","!=",">","<","<=",">="这些是if中可以用到的比较。看看就能知道什么意思吧。
示例:
{if $name eq"Fred"}
WelcomeSir.
{elseif $name eq"Wilma"}
WelcomeMa'am.
{else}
Welcome,whatever you are.
{/if}
五. 循环
在Smarty里使用循环遍历数组的方法是section,如何赋值遍历都是在模板中解决,php源文件中只要一个assign就能解决问题。
示例:
{* this examplewill print out all the values of the $custid array *}
{secti loop=$custid}
id: {$custid[customer]}<br>
{/section}
OUTPUT:
id: 1000<br>
id: 1001<br>
id: 1002<br>
六. 常见问题
Smarty将所有大括号{}里的东西都视为自己的逻辑程序,于是我们在网页中想插入javascrīpt函数就需要literal的帮忙了,literal的功能就是忽略大括号{}。
示例:
{literal}
<scrīptlanguage=javascrīpt>
function isblank(field) {
if (field.value == '')
{ return false; }
else
{
document.loginform.submit();
return true;
}
}
</scrīpt>
{/literal}
一. 安装
首先打开网页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目录下,代码如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type" c>
<title>Smarty</title>
</head>
<body>
{$hello}
</body>
</html>
新建index.php,将此文件放在learn/下:
<?php
//引用类文件
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 notodd", "not", "mod", "div by", "evenby", "odd by","==","!=",">","<","<=",">="这些是if中可以用到的比较。看看就能知道什么意思吧。
示例:
{if $name eq"Fred"}
WelcomeSir.
{elseif $name eq"Wilma"}
WelcomeMa'am.
{else}
Welcome,whatever you are.
{/if}
五. 循环
在Smarty里使用循环遍历数组的方法是section,如何赋值遍历都是在模板中解决,php源文件中只要一个assign就能解决问题。
示例:
{* this examplewill print out all the values of the $custid array *}
{secti loop=$custid}
id: {$custid[customer]}<br>
{/section}
OUTPUT:
id: 1000<br>
id: 1001<br>
id: 1002<br>
六. 常见问题
Smarty将所有大括号{}里的东西都视为自己的逻辑程序,于是我们在网页中想插入javascrīpt函数就需要literal的帮忙了,literal的功能就是忽略大括号{}。
示例:
{literal}
<scrīptlanguage=javascrīpt>
function isblank(field) {
if (field.value == '')
{ return false; }
else
{
document.loginform.submit();
return true;
}
}
</scrīpt>
{/literal}
0 0
- smarty使用
- smarty使用
- Smarty For Sae(sae中使用smarty)
- 【smarty】smarty保留变量的使用
- Smarty模板使用
- ZF下使用 Smarty
- Smarty使用自定义资源
- [PHP]Smarty的使用
- SMARTY安装与使用
- CodeIgniter,smarty的使用
- smarty模板使用
- ecshop之smarty使用
- smarty中使用变量
- smarty使用变量
- smarty中使用变量
- smarty使用变量
- smarty使用变量
- smarty的使用变量
- C# WINFORM 捕获最小化事件
- Android入门:Activity四种启动模式
- jdk 安装
- python小点心--struct 封装变长字符串
- 关于windows server 2003服务器搭建正式dedecms运行环境的问题
- smarty使用
- Apache Shiro 使用手册(四)Realm 实现
- 如何分辨内网ip和外网ip
- 细说mybatis的缓存机制
- recvfrom(int s, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen);/函数简单了解
- Android应用是否可以只有一个Service或Broadcast Reciver,而没有Activity?
- Oracle 查询一个月内的活动
- 代码托管平台
- iOS Core Animation Advanced Techniques的学习笔记(五)