smarty模板使用
来源:互联网 发布:java getname 编辑:程序博客网 时间:2024/05/21 09:52
简单来说,模板的技术就是页面元素与业务逻辑代码分开,然后用模板语言替代页面元素的变量的值输出就可以,相对于J2EE中JSTL和EL表达式稍微麻烦点呵呵,关键还是不习惯这种模板语言!
-->-->-->-->-->-->
1, 在网站根目录下新建smarty文件夹,下载最新版本的Smarty,解压,复制其中的lib文件夹至此目录下。
2,在smarty文件夹下新建新建templates目录,templates里新建cache(缓存),templates(模板文件),templates_c(编译的代码文件), config(配置文件)
3,新建模板文件index.tpl至smarty/tempates/templates文件夹下,代码如下
<html>
<head>
<title>smarty</title>
</head>
<body>
{$hello}
</body>
</html>
4,使用。新建index.php,代码如下
<?php
include_on
$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;
$shuru = "Hello World!";//赋值
$smarty->assign("hello",$shuru);// 替换index.tpl文件中变量名为hello的值
$smarty->left_delimiter = "<{"; //设置左边界符为<{防止和JS冲突导致无法显示页面的错误,默认为{
$smarty->right_delimiter = "}>";
$smarty->display('index.tpl');//显示模板文件
?>
l 数组
$arr=array(hello=>"哈哈",contents=>"我爱何老师!");//数组
$smarty->assign("hello",$arr);//替换index.tpl文件中变量名为hello的值
$smarty->display('index.tpl');
模板中输出
{$hello.hello} {$hello.contents}
l 注释
一般解析的变量放在{}中,那么{*注释内容*}就可以注释
l 引用
网站中的网页一般header和footer是可以共用的,所以只要在每个tpl中引用它们就可以了。
例如模板页修改为,注意所有模板文件都是放在smarty/templates/templates文件夹下
{include file="header.tpl"}
{* body of template goes here *}
{include file="footer.tpl"}
l 判断,elseif之间最好不要有空格,否则会导致不可预料的结果
{if $a==0}
a等于0
{elseif $a<0}
a小于0
{else}
a大于0
{/if}
l 循环 loop的值为遍历的数组,name为循环的名称,但是不支持引用数组,哈哈
{section name=key1 loop=$arr1}
值为:{$arr1[key1]}
{/section}
用foreach from为数组,item数组的值,key为键
{foreach from=$hello item=value key=key }
键:{$key} 值:{$value}<br>
{/foreach}
一,声明
$tpl->cache_dir = "./cache";//*nix服务器上,请确保有可写权限
$smarty->cache_lifetime = 60 * 60 * 24; //设置缓存时间为1天,-1永不过期 0程序每次运行被重建
$smarty->caching = true; //设置缓存方式,默认为false
//----------------------------------------------------
//左右边界符,默认为{},但实际应用当中容易与JavaS
//相冲突,所以建议设成<{}>或其它。
//----------------------------------------------------
$smarty->left_delimiter = "<{";
$smarty->right_delimiter = "}>";
$smarty->compile_dir = "" :
//*nix服务器上,请确保compile_dir里定义的这个目录具有可写可读权限,默认是当前目录下的templates_c
二,变量的使用
assign(string varname, mixed var);//注册变量
{assign var="UserName" value="天涯的海风"}
三,模板目录及显示
$smarty->template_dir = "" ;
//在没有这一句时,Smarty默认的模板路径为当前目录的templates目录
$smarty->display("index.tpl");//显示对应模板目录下的模板文件
四,模板文件跳转到相应的页面
模板文件中的跳转路径,以使用当前模板的PHP文件为准!!!
五,安全问题及其他
php函数
usert($array); //注销数组变量,否则下次添加数据会导致在原数组中继续添加
mysql_free_result($result);//关闭结果集
//中文截取函数 这个有时间再写吧
1,.inc文件会在浏览器加载的时候显示出来,所要要该城.php文件
2,普通for循环
{section name=loop loop=$count}
变量: {$smarty.section.loop.index+1}
{/section}
- Smarty模板使用
- smarty模板使用
- Yii使用smarty模板
- smarty模板使用入门
- Smarty模板使用
- smarty模板的使用
- PHP使用smarty模板引擎
- Smarty模板引擎的使用
- windows上使用smarty模板
- php:smarty模板的使用
- Smarty模板引擎的使用
- MVC中使用smarty模板
- smarty模板简单的使用
- 如何使用smarty模板殷勤
- smarty模板使用详细教程
- PHP 中使用 Smarty 之初:认识 Smarty 模板引擎
- 使用Dreamweaver编辑Smarty tpl模板文件
- smarty模板config_load使用报错原因
- 获取多个复选框的值
- css
- PHP多文件上传(简单实用)
- PHP中正则表达式的使用
- PHP中AJAX的使用
- smarty模板使用
- php+access数据库编程
- eclipse for php调测
- php字符串函数
- php日期函数
- oracle中游标的使用
- 判断文件夹是否存在并创建
- pl-sql语法
- 存储过程事务的使用