您的位置:首页 > 其它

smarty精品教程四(高级篇)可下载实例

2008-05-13 16:31 477 查看
现我们就学学smarty表态页面语法.

eg4:

4.php

<?php
/*********************************************
QQ:283093141

*********************************************/
include_once('../libs/Smarty.class.php'); //包含smarty类文件

$smarty= new Smarty; //实例化Smarty类

$smarty->template_dir= './templates'; //设置模板目录
$smarty->compile_dir= './templates_c'; //设置编译目录
$smarty->cache_dir= './cache'; //设定缓存目录
$smarty->caching = ture;

$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("4/4.tpl");
?>

4.tpl

<html>
<head><title>流程实例</title><head>
<body>

1.模板中的流程控制<br>
<table border="1" align="center">
{assign var="tbColor" value="green"}
色彩:{$tbColor}<br>

{section name=yy loop=$News}

{if $tbColor == "green"}
<tr bgcolor="{$tbColor}">
{assign var="tbColor" value="orange"}
{else $tbColor == "orange"}
<tr bgcolor = "{$tbColor}">
{assign var="tbColor" value="green"}
{/if}

<td>{$News[yy].newsID}</td>
<td>{$News[yy].newsTitle}</td>
<tr>

{/section}
</table>
<br><br>

2.一行输出多条记录 <br>

<table>
<tr>
{section name=loop loop=$News step=1}
{if $smarty.section.loop.index % 3==0}
</tr>
<tr>
{/if}
<td>{$News[loop].newsID}</td>
<td>{$News[loop].newsTitle}</td>
{/section}
</tr>
</table>
<br><br>
3.foreach:用于循环简单数组<br>
这里将输出一个数组:<br>
{foreach from=$News item=newsID}
新闻编号:{$newsID.newsID}<br>
新闻内容:{$newsID.newsTitle}<br><hr>
{foreachelse}
对不起,数据库中没有新闻输出!
{/foreach}

<br>
<br>
4.section运用

{section name=l loop=$News}
新闻编号:{$News[l].newsID}<br>
新闻标题:{$News[l].newsTitle}<br><hr>
{sectionelse}
对不起,没有任何新闻输入!
{/section}
<hr><b> By fkedwgwy 2008<b><a href="http://fkedwgwy.51.com" >作者</a>
</body>
</html>

这就是smart基础的全部课程,接下来我们就要学习写用smarty的

新闻发布系统了.我会在第一时间把课程分享给大家`````

在我学习smarty的时候,遇到了缓存问题.有时候我们并不一定要生成缓存.那么我们就只要设置

$smarty->caching = false;就可以了.

本实例的下载地址:http://download.csdn.net/source/453628
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: