最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
smarty入门教程四
时间:2022-07-02 10:49:12 编辑:袖梨 来源:一聚教程网
example4.tpl
CODE:[Copy to clipboard]
| {$News[loop].newsID} | {$News[loop].newsTitle} |
example4.php
PHP:[Copy to clipboard]
/*********************************************
*
* 文件名: example4.php
* 作 用: 显示实例程序4
*
* 作 者: 大师兄
* Email: [email protected]
* 修 正: forest
*********************************************/
require_once ("./comm/Smarty.class.php");
$smarty = new Smarty();
$smarty->template_dir = './templates/';
$smarty->compile_dir = './templates_c/';
$smarty->config_dir = './configs/';
$smarty->cache_dir = './cache/';
$smarty->caching = false;
$array[]= array("newsID"=>"001", "newsTitle"=>"第1条新闻");
$array[]= array("newsID"=>"002", "newsTitle"=>"第2条新闻");
$array[]= array("newsID"=>"003", "newsTitle"=>"第3条新闻");
$array[]= array("newsID"=>"004", "newsTitle"=>"第4条新闻");
$array[]= array("newsID"=>"005", "newsTitle"=>"第5条新闻");
$array[]= array("newsID"=>"006", "newsTitle"=>"第6条新闻");
$array[]= array("newsID"=>"007", "newsTitle"=>"第7条新闻");
$array[]= array("newsID"=>"008", "newsTitle"=>"第8条新闻");
$smarty->assign("News", $array);
$smarty->display("example4.tpl");
?>
example4.php输出:
==================================================
| 001 | 第1条新闻 |
| 002 | 第2条新闻 |
| 003 | 第3条新闻 |
| 004 | 第4条新闻 |
| 005 | 第5条新闻 |
| 006 | 第6条新闻 |
| 007 | 第7条新闻 |
| 008 | 第8条新闻 |
===================================================
模板文件中使用:
PHP:[Copy to clipboard]
{if $tbColor == "#D4D0C8"}
{assign var="tbColor" value="#EEEEEE"}
{else $tbColor == "#EEEEEE"}
{assign var="tbColor" value="#D4D0C8"}
{/if}
这一语句块进行设置每一行的背景颜色, {assign var="tbColor" value="#D4D0C8"}还记的吧,是例3中设置模板内部变量的定义方法,
使用模板内置的流程控制语句有时可以极大程度上提高程序的控制能力,下面一个例子是phpx.com中曾经有位朋友问过的,我将它作为
实例放在这里供大家学习.
例4我用来说明{if / elseif /else /if}的使用方法,如果单为了实现隔行的目的,大家可以使用这一句就行了:
PHP:[Copy to clipboard]
{section name=rows loop=$data}
{/section}
例5: 使用模板内置流程控制语句进行一行多单元格内容输出, 也就是在视觉上smarty每记输出几条记录:
相关文章
- 国风美少年穿搭指南 - 2026新中式风格造型推荐 06-13
- 官方公告发布 - 权威信息一手掌握 06-13
- SKR音效制作教程 - 2026最新版音频处理技巧 06-13
- 万恶之源的真正含义解析 - 深度解读文化语境与现实影响 06-13
- 无人商店运营模式解析 - 2026年智能零售新趋势 06-13
- 大猪蹄子是什么意思 - 网络流行语解析 06-13