smarty 教程 及 常用点
1. 简单例子 有助回忆基本知识点
1 define("DIR",dirname(__FILE__)); 2 require_once(DIR."/libs/Smarty.class.php");//引用smarty相对路径 3 4 $smarty = new Smarty(); 5 $smarty->template_dir(DIR."/templates");//模板路径 6 $smarty->complie_dir(DIR."/templates_c");//编译模板的路径 7 $smarty->cache_dir(DIR."/cache");//smarty 缓存目录 8 $smarty->caching = true; //true启用缓存 false禁止使用缓存 9 $smarty->cache_lifetime=3600*24;/缓存页面24小时 10 $smarty->left_delimiter="<{"; 11 $smarty->right_delimiter="}>";//左右分隔符 以上变量均可以在Smarty.class.php中找到,可以在那里进行全局设置。 也可以对某个模板如主页index.html进行单独配置。 12 13 $smarty->assign("xx", "xxx");//分配smarty变量,编译模板进行解析 14 $smarty->display("index.html");//显示解析后的模板
2.重点foreach使用,多级嵌套伪代码,文章分页列表页:page title date 分别表示:页码, 文章标题,发布时间
index.php
$article[0] = array("我是第1篇文章的标题"=>2011,"我是第2篇文章的标题"=>2011,"我是第3篇文章的标题"=>2011); $article[1] = array("我是第4篇文章的标题"=>2012,"我是第5篇文章的标题"=>2012,"我是第6篇文章的标题"=>2012); $article[2] = array("我是第7篇文章的标题"=>2013,"我是第8篇文章的标题"=>2013,"我是第9篇文章的标题"=>2013); $smarty->assign("article", $article); $smarty->display("index.html");
index.html
{foreach from=$article key=page item=title_date} {$page}页有以下这些文章:{foreach from=$title_date key=title item=date} 文章标题:{$title}; 发布时间:{$date} {/foreach}{foreachelse} 此{$page}无文章 {/foreach}
问题1:
通常,使用循环时候是为了得到value值,所以,经常看到省略写法也是正确的。{foreach from=$article item=value} 。但是,此处需要用到key值。
3.重点section 使用
{section}块具有的属性值,分别为:
1. index: 上边我们介绍的"循环下标",默认为0
2. index_prev: 当前下标的前一个值,默认为-1
3. index_next: 当前下标的下一个值,默认为1
4. first: 是否为第一下循环
5. last: 是否为最后一个循环
6. iteration: 循环次数
7. rownum: 当前的行号,iteration的另一个别名
8. loop: 最后一个循环号,可用在section块后统计section的循环次数
9. total: 循环次数,可用在section块后统计循环次数
10. show: 在函数的声明中有它,用于判断section是否显示
index 属性演示 {section name=customer loop=$custid}{$smarty.section.customer.index} id: {$custid[customer]}<br>{/section}OUTPUT:0 id: 1000<br>1 id: 1001<br>2 id: 1002<br> index_prev 属性演示 {section name=customer loop=$custid}{$smarty.section.customer.index} id: {$custid[customer]}<br>{* FYI, $custid[customer.index] and $custid[customer] are identical in meaning *}{if $custid[customer.index_prev] ne $custid[customer.index]}The customer id changed<br>{/if}{/section}OUTPUT:0 id: 1000<br>The customer id changed<br>1 id: 1001<br>The customer id changed<br>2 id: 1002<br>The customer id changed<br>iteration 属性演示 iteration 不像index属性受start、step和max属性的影响,该值总是从1开始(index是从0开始的).rownum 是iteration的别名,两者等同. {section name=customer loop=$custid start=5 step=2}current loop iteration: {$smarty.section.customer.iteration}<br>{$smarty.section.customer.index} id: {$custid[customer]}<br>{* FYI, $custid[customer.index] and $custid[customer] are identical in meaning *}{if $custid[customer.index_next] ne $custid[customer.index]}The customer id will change<br>{/if}{/section}OUTPUT:current loop iteration: 15 id: 1000<br>The customer id will change<br>current loop iteration: 27 id: 1001<br>The customer id will change<br>current loop iteration: 39 id: 1002<br>The customer id will change<br>first 属性演示 如果当前循环第一次执行,first 被设置为true. {section name=customer loop=$custid}{if $smarty.section.customer.first}<table>{/if}<tr><td>{$smarty.section.customer.index} id:{$custid[customer]}</td></tr>{if $smarty.section.customer.last}</table>{/if}{/section}OUTPUT:<table><tr><td>0 id: 1000</td></tr><tr><td>1 id: 1001</td></tr><tr><td>2 id: 1002</td></tr></table>遍历多维数组: {section name=customer loop=$contacts}name: {$contacts[customer].name}<br>home: {$contacts[customer].home}<br>cell: {$contacts[customer].cell}<br>e-mail: {$contacts[customer].email}<p> {/section}loop 用于显示该循环上一次循环时的索引值. 该值可以用于循环内部或循环结束后. {$smarty.section.customer.loop} total 用于显示循环执行总的次数. 可以在循环中或执行结束后调用此属性. {$smarty.section.customer.total}Section 循环 通过如下方式调用变量{$smarty.section.sectionname.varname}.
View Code
其他问题1: 关联数组 与 索引数组 与 对象属性 3种不同获取方式
关联数组:
index.php:$smarty = new Smarty; $smarty->assign('Contacts',array('fax' => '555-222-9876','email' => 'zaphod@slartibartfast.com','phone' => array('home' => '555-444-3333','cell' => '555-111-1234'))); $smarty->display('index.tpl');index.tpl:{$Contacts.fax}<br> {$Contacts.email}<br> {* you can print arrays of arrays as well *} {$Contacts.phone.home}<br> {$Contacts.phone.cell}<br>
索引数组:
index.php:$smarty = new Smarty; $smarty->assign('Contacts',array('555-222-9876','zaphod@slartibartfast.com',array('555-444-3333','555-111-1234'))); $smarty->display('index.tpl');index.tpl:{$Contacts[0]}<br> {$Contacts[1]}<br> {* you can print arrays of arrays as well *} {$Contacts[2][0]}<br> {$Contacts[2][1]}<br>
对象属性获取:
name: {$person->name}<br> email: {$person->email}<br>OUTPUT:name: Zaphod Beeblebrox<br> email: zaphod@slartibartfast.com<br>
其他问题2:
空白变量处理:
{* 传统if else方法解决 *}{if $title eq ""} {else}{$title} {/if}{* 简洁方式 default *}{$title|default:" "}
Smarty模板之间互相引用,值与值之间传递的方式:
1 mainpage.tpl 2 ------------ 3 4 {include file="header.tpl" title="Main Page"} 5 {* template body goes here *} 6 {include file="footer.tpl"} 7 8 9 archives.tpl 10 ------------ 11 12 {config_load file="archive_page.conf"} 13 {include file="header.tpl" title=#archivePageTitle#} 14 {* template body goes here *} 15 {include file="footer.tpl"} 16 17 18 header.tpl 19 ---------- 20 <HTML> 21 <HEAD> 22 <TITLE>{$title|default:"BC News"}</TITLE> 23 </HEAD> 24 <BODY> 25 26 27 footer.tpl 28 ---------- 29 </BODY> 30 </HTML> 31 32 -------- 33 当这个主页[main page]被浏览的时候,‘Main Page'这个标题就会传递给头模板文件[header.tpl],并且结果会被用成为标题。当这个档案页[archives page]被浏览的时候,文件标题将会是‘Archives'。注意在这个档案页例子中,我们用了一个来自这个文件[archives_page.conf]的变量来代替一个硬性的代码变量。当然,要是变量[$title]没有初始化,我们会发现‘BC News'被输出----用那种使用默认值的变量的方法。
转载于:https://www.cnblogs.com/qunshu/p/3175651.html
smarty 教程 及 常用点相关推荐
- Smarty中文手册,Smarty教程,Smarty模板的入门教材
Smarty中文手册,Smarty教程,Smarty模板的入门教材 首先,这份Smarty中文手册的翻译工作是由喜悦国际村村民自发组织的,不代表任何人的意见和观点.对他们的无私奉献精神,我们表示感谢, ...
- Django 3.2.5博客开发教程:一些常用的模板使用方法
一.django static文件的引入方式 1.在django project中创建 static文件夹 2.settings.py中配置要在 STATIC_URL = '/static/' 下边 ...
- linux eth0 目录,教程 | Linux常用命令大全
原标题:教程 | Linux常用命令大全 来源:Linux爱好者 ID:LinuxHub Linux常用命令 目录操作命令 ls 命令名称:ls 命令英文原意:list 命令所在路径:/bin/ls ...
- 大师兄Smarty教程修正版
大师兄Smarty教程修正版,修改了一些网上复制大师兄教程的错误: 整个教程分四大部分: 一.模板设计部分 二.程序设计部分 三.实例篇(使用PHP内置MySQL函数) 四.实例篇(使用phplib的 ...
- 大师兄Smarty教程修正版(1).
教程一:模板设计部分 一.什么是smarty? smarty是一个使用PHP写出来的模板PHP模板引擎,它提供了逻辑与外在内容的分离,简单的讲,目的就是要使用PHP程序员同美工分离,使用的程序员改变程 ...
- 数字IC设计工具教程——VCS常用命令
数字IC设计工具教程--VCS常用命令 文章目录 数字IC设计工具教程--VCS常用命令 编译开关(静态开关) 仿真开关(动态开关) 后处理打开DVE testbench中后处理系统函 覆盖率统计 门 ...
- 【Smarty】Smarty引用、常用内置变量、判断、循环、JavaScript脚本
在<[Smarty]Smarty的下载.配置与Helloworld>(点击打开链接)说明了Smarty的基本运行方式,这里再进一步说明,Smarty的条件结构.循环结构怎么使用.Sessi ...
- Smarty教程[1]
以下文件章为引用 大师兄Smarty教程修正版,修改了一些网上复制大师兄教程的错误: 整个教程分四大部分: 一.模板设计部分 二.程序设计部分 三.实例篇(使用PHP内置MySQL函数) 四.实例篇( ...
- Smarty教程[4]
2.---程序设计部分 在smarty的模板设计部分我简单的把smarty在模板中的一些常用设置做了简单的介绍,这一节主要来介绍一下如何在smarty中开始我们程序设计. 首先来介绍一下在上一 ...
最新文章
- Django的缓存机制
- sqlserver 参数化查询 允许为null_1+Null 居然等于 Null
- Linux-鸟菜-4-关机的正确姿势
- C++算法三:选择排序
- ajax提交整个form表单
- 双系统中ubuntu的安装方法
- Windows10 解决“装了 .NET Framework 4.5.2/4.6.1/4.7.1等等任何版本 或版本更高的更新”问题
- 播放视频时有残影、水纹的原因
- 机器学习笔记(四):kNN算法 | 凌云时刻
- Js日期格式化 年月日时分秒
- golang Gin Validator以及翻译校验
- 第十三届蓝桥杯模拟赛(第三期)试题与题解 C++
- Python Class 05-字符串
- 实验报告:定义一个名为MyRectangle的矩形类,完成如下要求
- magento添加sku_快速提示:如何将优惠券添加到Magento电子商务商店
- 如何参与Github开源社区开发
- 数位 dp 相邻位数字差值的绝对值不能超过 2_维懂百科——绝对值编码器的“绝对式”的定义...
- 022-企业站:纽曼移动端微官网实战
- iSecure Center(V1.1.0)平台使用记录
- 子弹短信怎么就一夜爆火了呢??
热门文章
- Android 水平平分布局和垂直平分布局
- OpenCV入门基础学习
- mysql错误1053 服务没有及时响应启动或控制请求_windows服务安装启动报错误1053:服务没有及时响应启动或控制请求...
- org.apache.axis2.AxisFault: org.apache.axis2.databinding.ADBException: Unexpected subelement profile
- 基于JSP的人事管理系统设计
- 菜鸟webpack教程纠错
- 解决“ERROR:database “xxx“ is being accessed by other users“
- AI绘画发展史(伪):从免费到吃屎;YSDA·自然语言处理课程8K Star;伯克利CS285·深度强化学习课程;前沿论文 | ShowMeAI资讯日报
- 这周在我们的雷达上:僵尸和UX Gaffes
- Metro UI配色方案