smarty
发布日期:2021-10-02 15:44:23 浏览次数:2 分类:技术文章

本文共 2922 字,大约阅读时间需要 9 分钟。

一、

$name=array('one','tow','three','four');
foreach输出
<{foreach item=item from=$name}>
<{$item}>
<{/foreach}>
结果
one tow three four
section输出
<{section name=name loop=$name}>
<{$name[name]}>
<{/section}>
结果one tow three four
二、
$name=array('a'=>'one','b'=>'tow','c'=>'three','d'=>'four');
这种情况下,用foreach可以输出正常
用section没有输,这就是说,section在处理一维数组是,不能处理带非数值索引的
例:可以处理array('a','b'),array(1=>'a',2=>'b'),不能处理array('a'=>'a','b'=>'b')这样的
三、
$name=array(array('title'=>'a','body'=>'b'),array('title'=>'c','body'=>'d'));
以下都可以输出
<{foreach key=key item=item from=$name}>
<{$item.title}><br>
<{/foreach}>
<{section name=name loop=$name}>
<{$name[name].title}>
<{/section}>
四、
$name=array(array('a','b'),array(‘c','d'));
以下都 可输出
<{foreach key=key item=item from=$name}>
<{$item.0}><br>
<{/foreach}>
<{section name=name loop=$name}>
<{$name[name].0}>
<{/section}>
======================================================
include语句,将其它模板引入当前模板
<{include file='header.tpl'}>
两个特性
<{include file='header.tpl' assign='header'}>则不会输,而是将文件的内容给变量$header
<{include file='header.tpl' title='我是标题' }>加载模板同时把$title传给模板,注意变量title不能带引号
以此方式传递的变量只能在所导入的文件中使用
insert用它导入不会被缓存的数据
使模板的一部分不被缓存. 如果打开了缓存, insert 函数却不会被缓存,每次调用页面它们都会被动态加载,即使是在缓存页面中. 该特性可以广泛应用于广告条、投票、实时天气预报、搜索结果、反馈信息等区域.
用法,在脚本中
function insert_time($char)
{
echo ($char['char']);
echo date('H:i:s');
}
在模板中
<{insert name='time' char='现在时间'}>
注意的是所有的insert函数以insert_命名
literal用于原样显示
<{literal}>
<{$name}>
<{/literal}>
会输出<{$name}>而不会替换
php
在模板中嵌入php脚本
include_php
导入带php代码的脚本
=========================================================================
配置文件
#global
appname='配置文件的测试';
[jjq]
title='佳佳泉'
content='佳佳泉的内容'
[sina]
title='新浪济南'
content='新浪济南的内容'
说明[]称为节,节之外的是全局的,这些项必须在节之前定义,配置文件放在配置(config_dir)指定的目录中
字符串多行时,保存在三个引号中
name="""我是
                               两行"""
使用配置文件
<{config_load file='app.config'}>加载配置文件的全局变量
<{config_load file='app.config' section='jjq'}>加载特定的节
引用配置变量
用#或$smarty.config
<{#title#}>
<{$smarty.config.title}>
========================================================================================
缓存可以生成静态 面,与编译的区别
编译减少将模板转换为php脚本的开销,但仍需要在逻辑层执行获取数据的命令
缓存则直接生成静态页,减 少上面所述的开销
编译默认是开启的,而缓存必须由开发人员显式开启
$smarty->caching=1;
$smarty->cache_lifetime=60;
技巧:如果你想给某些模板设定它们自己的缓存生存时间,你可以在调用display()或fetch()函数之前,通过设置$caching = 2,然后设置$cache_lifetime为一个唯一值来实现.
is_cached
if(!$smarty->is_cached("index.tpl")) {
// do database calls, assign vars here
// 调用,并对变量进行赋值
}
$smarty->display("index.tpl");
================================================================
6.接收变量函数
$smarty.get.id:接收get过来的id的值
{
{$smarty.get.id}}
$smarty.post.id:接收post过来的id的值
{
{$smarty.post.id}}
$smarty.request.id:接收request过来的id的值
{
{$smarty.request.id}}
$smarty.cookies.id:接收cookie中名为id的值
{
{$smarty.cookies.id}}
$smarty.session.id:接收session中名为id的值
{
{$smarty.session.id}}
$smarty.const.FNAME:取出php中定义名为FNAME的常量:
{
{$smarty.const.FNAME}}
$smarty.now:取出当前UNIX时间戳
:{
{$smarty.now}}
6这部分的原文连接http://bbs.it1jia.com/viewthread.php?tid=24

转载地址:https://blog.csdn.net/jinyeweiyang/article/details/41286323 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:IOS 基于AV Foundation框架开发简单音乐播放器
下一篇:IOS拼接MP3,歌曲文件合成。

发表评论

最新留言

很好
[***.229.124.182]2024年04月13日 15时46分25秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

spring boot 与 Ant Design of Vue 实现删除组织(二十六) 2021-06-30
spring boot 与 Ant Design of Vue 实现获取用户列表(二十七) 2021-06-30
spring boot 与 Ant Design of Vue 实现新增用户(二十八) 2021-06-30
spring boot 与 Ant Design of Vue 实现修改用户(二十九) 2021-06-30
spring boot 与 Ant Design of Vue 实现删除用户(三十) 2021-06-30
spring boot 与 Ant Design of Vue 鉴权体系登录的实现(三十一) 2021-06-30
spring boot 与 Ant Design of Vue 鉴权体系获取用户信息的实现(三十二) 2021-06-30
Druid连接池实现自定义场景的多数据库的连接 2021-06-30
CentOs7命令行(静默)的方式安装oracle数据库 2021-06-30
基于VMware安装CentOs7的镜像 2021-06-30
PL/SQL数据库管理工具的使用 2019-04-27
史上最简单的spring-boot集成websocket的实现方式 2019-04-27
带你玩转属于自己的spring-boot-starter系列(一) 2019-04-27
带你玩转属于自己自己的spring-boot-starter系列(二) 2019-04-27
带你玩转属于自己的spring-boot-starter系列(三) 2019-04-27
基于SnowFlake算法如何让分库分表中不同的ID落在同一个库的算法的实现 2019-04-27
基于springboot的ShardingSphere5.X的分库分表的解决方案之分库解决方案(二) 2019-04-27
基于springboot的ShardingSphere5.X的分库分表的解决方案之分表解决方案(一) 2019-04-27
基于springboot的ShardingSphere5.X的分库分表的解决方案之关联查询解决方案(三) 2019-04-27
基于springboot的ShardingSphere5.X的分库分表的解决方案之基于seata的分布式事务的解决方案(十五) 2019-04-27