如何学习smarty,smarty入门
2008-07-29 10:53
337 查看
一、安装
http://smarty.php.net/download.php 下载,解压
1.在你的“localhoat”目录下建立新的目录learn/,在learn下建立目录
smarty/,将刚才解压出来的目录的libs/拷贝到smarty/里,再在smarty/
里新建templates目录,templates里新建cache/,templates/,templates_c/,
config/,如下图所示
--learn
--smarty
--libs
--internals
--plugins
--templates
--cache
--configs
--templates
--templates_c
2.新建一个模板文件:index.tpl 将此文件放在learn/smarty/templates/templates
目录下,代码如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML4.01
Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html";charset="ge2312">
<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");//加载类模板
?>
注:Smarty.class.php文件定义了类$Smarty及几个常量。
3.执行index.php 就能在页面上输出"hello,world"
二、赋值
在模板文件中替换的变量用“{ }”括起来,值的前面还要加“$”号,如
{$hello},这里可以是数组,如下:
*.tpl
Hello,{$exp.name}!Good {$exp.time}
*.php
$hello['name']="Mr.Green";
$hello['time']="morning";
$smarty->assign("exp",$hello);
OUTPUT:Hello,Mr.Green!Good moring
三、引用
网站中的网页,一般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.
{else if $name eq "Wilma"} Welcome Madam.
{else} Welcome,whatever you are
{/if}
注意:在“条件”没有使用“括号”括起来
五、循环
有两种方式:
1.foreach
例1:
{foreach from=$custid item=curr_id}
id: {$curr_id}<br>
{/foreach}
OUTPUT:
id: 1000<br>
id: 1001<br>
id: 1002<br>
例2:
$smarty->assign("contacts", array(array("phone" => "1", "fax" => "2", "cell" => "3"),
array("phone" => "555-4444", "fax" => "555-3333", "cell" => "760-1234")));
*}
{* 键就是数组的下标,请参看关于数组的解释 *}
{foreach name=outer item=contact from=$contacts}
{foreach key=key item=item from=$contact}
{$key}: {$item}<br>
{/foreach}
{/foreach}
OUTPUT:
phone: 1<br>
fax: 2<br>
cell: 3<br>
phone: 555-4444<br>
fax: 555-3333<br>
cell: 760-1234<br>
2.section
例1:
{section name=customer loop=$custid}
id: {$custid[customer]}<br>
{/section}
OUTPUT:
id: 1000<br>
id: 1001<br>
id: 1002<br>
注:若数组的“键”为数字,则使用“[]”;若为字符串则使用“.”
如:$a[1].username 就指的是 $a[1]['username']
六、附
Smarty默认的标记分隔符是“{}”,但是JavaScript肯定包含了这个标记
我们可以用任意字符作分隔符,需要加上以下两句:
$smarty->left_delimiter="{/";
$smarty->right_delimiter="/}";
还可以使用literal。literal的功能就是忽略大括号{},例:
{literal}
<script language="JavaScript">
function isblank(field)
{
if(field value=="") {return false;}
else
{
document.loginform.submit();
return truen;
}
}
</script>
{/literal}
http://smarty.php.net/download.php 下载,解压
1.在你的“localhoat”目录下建立新的目录learn/,在learn下建立目录
smarty/,将刚才解压出来的目录的libs/拷贝到smarty/里,再在smarty/
里新建templates目录,templates里新建cache/,templates/,templates_c/,
config/,如下图所示
--learn
--smarty
--libs
--internals
--plugins
--templates
--cache
--configs
--templates
--templates_c
2.新建一个模板文件:index.tpl 将此文件放在learn/smarty/templates/templates
目录下,代码如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML4.01
Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html";charset="ge2312">
<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");//加载类模板
?>
注:Smarty.class.php文件定义了类$Smarty及几个常量。
3.执行index.php 就能在页面上输出"hello,world"
二、赋值
在模板文件中替换的变量用“{ }”括起来,值的前面还要加“$”号,如
{$hello},这里可以是数组,如下:
*.tpl
Hello,{$exp.name}!Good {$exp.time}
*.php
$hello['name']="Mr.Green";
$hello['time']="morning";
$smarty->assign("exp",$hello);
OUTPUT:Hello,Mr.Green!Good moring
三、引用
网站中的网页,一般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.
{else if $name eq "Wilma"} Welcome Madam.
{else} Welcome,whatever you are
{/if}
注意:在“条件”没有使用“括号”括起来
五、循环
有两种方式:
1.foreach
例1:
{foreach from=$custid item=curr_id}
id: {$curr_id}<br>
{/foreach}
OUTPUT:
id: 1000<br>
id: 1001<br>
id: 1002<br>
例2:
$smarty->assign("contacts", array(array("phone" => "1", "fax" => "2", "cell" => "3"),
array("phone" => "555-4444", "fax" => "555-3333", "cell" => "760-1234")));
*}
{* 键就是数组的下标,请参看关于数组的解释 *}
{foreach name=outer item=contact from=$contacts}
{foreach key=key item=item from=$contact}
{$key}: {$item}<br>
{/foreach}
{/foreach}
OUTPUT:
phone: 1<br>
fax: 2<br>
cell: 3<br>
phone: 555-4444<br>
fax: 555-3333<br>
cell: 760-1234<br>
2.section
例1:
{section name=customer loop=$custid}
id: {$custid[customer]}<br>
{/section}
OUTPUT:
id: 1000<br>
id: 1001<br>
id: 1002<br>
注:若数组的“键”为数字,则使用“[]”;若为字符串则使用“.”
如:$a[1].username 就指的是 $a[1]['username']
六、附
Smarty默认的标记分隔符是“{}”,但是JavaScript肯定包含了这个标记
我们可以用任意字符作分隔符,需要加上以下两句:
$smarty->left_delimiter="{/";
$smarty->right_delimiter="/}";
还可以使用literal。literal的功能就是忽略大括号{},例:
{literal}
<script language="JavaScript">
function isblank(field)
{
if(field value=="") {return false;}
else
{
document.loginform.submit();
return truen;
}
}
</script>
{/literal}
相关文章推荐
- 如何学习smarty,smarty入门(抄别人的)
- java学习笔记之java如何快速入门
- 如何在CEGUI中使用Lua脚本入门学习
- C++入门教程 之 初学者,你应当如何学习C++以及编程
- 如何入门深度学习?
- 如何学习java_java入门方法
- 新手如何学习嵌入式,怎么入门嵌入式,我来教你。。。
- 普通程序员如何入门深度学习?
- ARM+FPGA如何入门,如何学习入门嵌入式
- PHP入门指导:如何学习PHP?
- 如何学习-入门之后
- java入门学习(4)— 类,对象理解,如何创建类,对象
- Python 爬虫如何入门学习?
- 谈谈对计算机专业的层次化理解: 计算机专业如何入门(问题 引导 学习思路)
- Shader 入门笔记(一) 如何学习shader
- 变量模板smarty模板 入门学习
- Smarty入门学习
- java入门:java学习、java路线图、java教程丨如何做一个成功的大数据项目
- LINUX学习之路---入门篇---如何在VM中与主机共享上网
- 如何学习嵌入式?嵌入式如何入门?