热烈祝贺台州朗动科技的站长论坛隆重上线!(2012-05-28)    热烈庆祝伟大的祖国60周年生日 点击进来我们一起为她祝福吧(2009-09-26)    站长论坛禁止发布广告,一经发现立即删除。谢谢各位合作!.(2009-08-08)    热烈祝贺台州网址导航全面升级,全新版本上线!希望各位一如既往地支持台州网址导航的发展.(2009-03-28)    台州站长论坛恭祝各位新年快乐,牛年行大运!(2009-01-24)    台州Link正式更名为台州网址导航,专业做以台州网址为主的网址导航!(2008-05-23)    热烈祝贺台州Link资讯改名为中国站长资讯!希望在以后日子里得到大家的大力支持和帮助!(2008-04-10)    热烈祝贺台州Link论坛改名为台州站长论坛!希望大家继续支持和鼓励!(2008-04-10)    台州站长论坛原[社会琐碎]版块更名为[生活百科]版块!(2007-09-05)    特此通知:新台州站长论坛的数据信息全部升级成功!">特此通知:新台州站长论坛的数据信息全部升级成功!(2007-09-01)    台州站长论坛对未通过验证的会员进行合理的清除,请您谅解(2007-08-30)    台州网址导航|上网导航诚邀世界各地的网站友情链接和友谊联盟,共同引领网站导航、前进!(2007-08-30)    禁止发广告之类的帖,已发现立即删除!(2007-08-30)    希望各位上传与下载有用资源和最新信息(2007-08-30)    热烈祝贺台州站长论坛全面升级成功,全新上线!(2007-08-30)    
便民网址导航,轻松网上冲浪。
台州维博网络专业开发网站门户平台系统
您当前的位置: 首页 » PHP/Perl编程 » smarty教程,程序实现,1-4

smarty教程,程序实现,1-4

论坛链接
  • smarty教程,程序实现,1-4
  • 发布时间:2007-10-05 11:38:22    浏览数:5346    发布者:webptr    设置字体【   
一边学习,一边看官方,把大师兄的smarty重写了一遍,能运行.

example1.php<?php
/*
* @参考 大师兄smarty教程
* @author 马永占
* @email mailmyz*gmail.com
*/

// 加载 Smarty library
define('SMARTY_DIR','smarty/libs/');
define('SMARTY_DIR_OTHER','smarty/');

require('newsmarty.php');

$smarty->assign('name','聪明!');//进行模板变量替换

//编译并显示位于templates下的index.tpl模板
$smarty->display('example1.html');
?>
example2.php<?php
/*
* @参考 大师兄smarty教程
* @author 马永占
* @email mailmyz*gmail.com
*/

// 加载 Smarty library
define('SMARTY_DIR','smarty/libs/');
define('SMARTY_DIR_OTHER','smarty/');

require('newsmarty.php');

//时间的不好用...郁闷
$smarty->assign("str1", "my name is: myz."); //将str1替换成My Name Is: Myz.
$smarty->assign("str2", "我的名字叫:"); //输出: 我的名字叫:MyZ
$smarty->assign("str3", strtotime('-1 day')); //输出2007年06月19日(我的当前时间减一)
//$smarty->assign("str4", ""); //第四句不处理时会显示默认值,如果使用前面这一句则替换为""
$smarty->assign("str5", "前边8个*"); //第五句输出:********前边8个*
$smarty->assign("str6", "MaiLmYz@gmAil.com"); //这里将输出mailmyz@gmail.com
$smarty->assign("str7", "this is myz"); //在模板中显示为:this is myz
$smarty->assign("str8", time());

//编译并显示位于/templates下的index.tpl模板
$smarty->display("example2.html");
?>
example3.php<?php
/*
* @参考 大师兄smarty教程
* @author 马永占
* @email mailmyz*gmail.com
*/

// 加载 Smarty library
define('SMARTY_DIR','smarty/libs/');
define('SMARTY_DIR_OTHER','smarty/');

require('newsmarty.php');

//--------------------------------------------------------------------------------------
//处理{html_checkboxes name="CheckBox" values=$CheckName checked=$IsChecked output=$value separator="
"}
//--------------------------------------------------------------------------------------
$smarty->assign('CheckName', array(
1001 => '语文',
1002 => '数学',
1003 => '外语'));
$smarty->assign('IsChecked', 1001);


//--------------------------------------------------------------------------------------
//处理{html_radios name="RadioBox" values=$RadioName checked=$IsChecked output=$value separator="
"}
//--------------------------------------------------------------------------------------
$smarty->assign('RadioName', array(
1001 => '语文1',
1002 => '数学1',
1003 => '外语1'));
$smarty->assign('IsChecked', 1001);

//--------------------------------------------------------------------------------------
//{html_select_date}不用处理会自动输出
//--------------------------------------------------------------------------------------


//编译并显示位于/templates下的index.tpl模板
$smarty->display("example3.html");
?>
example4.php<?php
/*
* @参考 大师兄smarty教程
* @author 马永占
* @email mailmyz*gmail.com
*/

// 加载 Smarty library
define('SMARTY_DIR','smarty/libs/');
define('SMARTY_DIR_OTHER','smarty/');

require('newsmarty.php');

$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.html");
//$smarty->display("example4_2.html");
//$smarty->display("example4_3.html");
//$smarty->display("example4_4.html");
?>

newsmarty.php<?php
/*
* @author 马永占
* @email mailmyz*gmail.com
*/

require(SMARTY_DIR.'Smarty.class.php');

$smarty = new Smarty;

$smarty->template_dir = SMARTY_DIR_OTHER.'templates';//设置模板目录
$smarty->config_dir = SMARTY_DIR_OTHER.'config';//设置配置目录
$smarty->compile_dir = SMARTY_DIR_OTHER.'templates_c';//设置编译目录
$smarty->cache_dir = SMARTY_DIR_OTHER.'cache';//设置缓存目录
$smarty->cache_lifetime = 60 * 60 * 24; //设置缓存时间,60秒*60*24
$smarty->caching = false; //设置缓存方式,这个属性告诉Smarty是否要进行缓存以及缓存的方式。
//它可以取3个值,0:Smarty默认值,表示不对模板进行缓存;1:表示Smarty将使用当前定义的cache_lifetime来决定是否结束cache;2:表示Smarty将使用在cache被建立时使用cache_lifetime这个值。
//习惯上使用true与false来表示是否进行缓存。

//----------------------------------------------------
//左右边界符,默认为{},但实际应用当中容易与JavaScript
//相冲突,所以建议设成<{}>或其它。
//----------------------------------------------------
$smarty->left_delimiter = "<{";
$smarty->right_delimiter = "}>";
?>
e_footer.html 底部


e_header.html

标题


example1.html<{* Smarty 注释 *}>
<{include file="e_header.html"}>
Hello, <{$name}>!
<{include file="e_footer.html"}>
      example2.html


<html>
<head><title>smarty示例2</title></head>
<body>
1. 第一句首字母要大写:<{$str1|capitalize}><br>
2. 第二句模板变量 + MyZ:<{$str2|cat:"MyZ"}><br>
3. 第三句输出当前日期:<{$str3|date_format:"%Y年%m月%d日"}><br>
4. 第四句.php程序中不处理,它显示默认值:<{$str4|default:"没有值!"}><br>
5. 第五句要让它缩进8个空白字母位,并使用"*"取替这8个空白字符:<{$str5|indent:8:"*"}><br>
6. 第六句把[email]TEACHerLI@163.com[/email]全部变为小写:<{$str6|lower}><br>
7. 第七句把变量中的myz替换成:myz1:<{$str7|replace:"myz":"myz1"}><br>
8. 第八句为组合使用变量修改器:<{$str8|date_format:"%Y年%m月%d日"|lower}>
</body>
</html>


example3.html

<html>
<head><title>模板中内定的一些函数</title></head>
<body>

<{*下面的这一段相当于在模板内部定义一个变量UserName*}>
<{assign var="UserName" value="myz"}>
这里将显示模板内部定义的一个变量:UserName = <{$UserName}>

下面的这一行将显示3个checkBox:<br>
<{html_checkboxes name="CheckBox" options=$CheckName selected=$IsChecked output=$CheckName separator="<br />"}>
下面在这一行将显示3个radio:<br>
<{html_radios name="RadioBox" options=$RadioName checked=$IsChecked output=$RadioName separator="<br />"}>


下面显示一个月,日, 年选择框:<br>
<{html_select_date}>

<hr><b>CopyRight(C) By XiaoJun, Li 2004<b><{mailto address="[email]teacherli@163.ccom[/email]" text="联系作者"}>

</body>
</html>



example4.html

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

<{section name=loop 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[loop].newsID}></td>
<td><{$News[loop].newsTitle}></td>
<tr>
<{/section}>
</table>
</body>
</html>



example4_2.html



<html>
<head><title>模板中的流程控制</title><head>
<body>
<table border="1" align="center">
<{assign var="tbColor" value="gray"}>
色彩:<{$tbColor}><br>

<{section name=loop loop=$News}>
<tr bgcolor="<{cycle values="#D4D0C8,#EEEEEE"}>">
<td><{$News[loop].newsID}></td>
<td><{$News[loop].newsTitle}></td>
<tr>
<{/section}>
</table>
</body>
</html>




example4_3.html

<html>
<head>
<title>一行输出多条记录</title>
</head>
<body>
<table>
<tr>
<{section name=loop loop=$News step=1}>
<{if $smarty.section.loop.index % 4==0}>
</tr>
<tr>
<{/if}>
<td><{$News[loop].newsID}></td>
<td><{$News[loop].newsTitle}></td>
<{/section}>
</tr>
</table>
</body>
</html>
<!--
{section name = name loop = $varName[, start = $start, step = $step, max = $max, show = true]}
name: section的名称,不用加$
$loop: 要循环的变量,在程序中要使用assign对这个变量进行操作。
$start: 开始循环的下标,循环下标默认由0开始
$step: 每次循环时下标的增数
$max: 最大循环下标
$show: boolean类型,决定是否对这个块进行显示,默认为true

这里有个名词需要说明:
循环下标:实际它的英文名称为index,是索引的意思,这里我将它译成"下标",主要是为了好理解。它表示在显示这个循环块时当前的循环索引,默认从0开始,受$start的影响,如果将$start设为5,它也将从5开始计数,在模板设计部分我们使用过它,这是当前{section}的一个属性,调用方式为Smarty.section.sectionName.index,这里的sectionName指的是函数原型中的name属性。
{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}也可以配合使用{sectionelse},用来表示传入的数组变量为空时对模板进行的处理。
-->



example4_4.html

<html>
<head><title>这是一个foreach使用的例子</title></head>
<body>
这里将输出一个数组:<br>
<{foreach from=$News item=newsID}>
新闻编号:<{$newsID.newsID}><br>
新闻内容:<{$newsID.newsTitle}><br><hr>
<{foreachelse}>
对不起,数据库中没有新闻输出!
<{/foreach}>
</body>
</html>




----------------------------另外一个工程-----------------------------------------

index.php

<?php
/*
* @参考 大师兄smarty教程
* @author 马永占
* @email mailmyz*gmail.com
*/

// 加载 Smarty library
define('SMARTY_DIR','smarty/libs/');
define('SMARTY_DIR_OTHER','smarty/');

require('newsmarty.php');

define("NUM", 5); //定义每次显示的新闻条数

$conn=new mysqli("localhost", "root", "myz","News");
if (mysqli_connect_errno()!=0)
{
$errno=mysqli_connect_errno();
$error=mysqli_connect_error();
echo $errno.'<br />'.$error.'<br />';
exit;
}
mysqli_query($conn,"SET NAMES 'gbk'");
//这里将处理国内新闻部分
$query = "SELECT iNewsID, vcNewsTitle FROM tb_news_ch ORDER BY iNewsID DESC";
$result = mysqli_query($conn,$query);
if ($result===false)
{
$errno=mysqli_connect_errno();
$error=mysqli_connect_error();
echo $errno.'<br />'.$error.'<br />';
exit;
}
$i = NUM;
if (mysqli_num_rows($result)==0)
{
echo 'no data<br />';
exit;
}
else
{
while(($row = mysqli_fetch_assoc($result)) && $i > 0)
{
$array[] = array("NewsID"=>substr($row["iNewsID"], 0, 40),
"NewsTitle"=>substr($row["vcNewsTitle"], 0, 40));

$i--;
}
$smarty->assign("News_CH", $array);
unset($array);
mysqli_free_result($result);
}
//这里处理国际新闻部分
$query = "SELECT iNewsID, vcNewsTitle FROM tb_news_in ORDER BY iNewsID DESC";
$result = mysqli_query($conn,$query);
if ($result===false)
{
$errno=mysqli_connect_errno();
$error=mysqli_connect_error();
echo $errno.'<br />'.$error.'<br />';
exit;
}
$i = NUM;
if (mysqli_num_rows($result)==0)
{
echo 'no data<br />';
}
else
{
while(($row = mysqli_fetch_assoc($result)) && $i > 0)
{
$array[] = array("NewsID"=>substr($row["iNewsID"], 0, 40),
"NewsTitle"=>substr($row["vcNewsTitle"], 0, 40));

$i--;
}
$smarty->assign("News_IN", $array);
unset($array);
mysqli_free_result($result);
}
//这里将处理娱乐新闻部分
$query = "SELECT iNewsID, vcNewsTitle FROM tb_news_mu ORDER BY iNewsID DESC";
$result = mysqli_query($conn,$query);
if ($result===false)
{
$errno=mysqli_connect_errno();
$error=mysqli_connect_error();
echo $errno.'<br />'.$error.'<br />';
}
$i = NUM;
if (mysqli_num_rows($result)==0)
{
echo 'no data<br />';
}
else
{
while(($row = mysqli_fetch_assoc($result)) && $i > 0)
{
$array[] = array("NewsID"=>substr($row["iNewsID"], 0, 40),
"NewsTitle"=>substr($row["vcNewsTitle"], 0, 40));

$i--;
}
$smarty->assign("News_MU", $array);
unset($array);
mysqli_free_result($result);
}



mysqli_close($conn);

//编译并显示位于/templates下的index.html模板
$smarty->display("index.html");
?>



news.php

<?php
/*
* @参考 大师兄smarty教程
* @author 马永占
* @email mailmyz*gmail.com
*/

// 加载 Smarty library
define('SMARTY_DIR','smarty/libs/');
define('SMARTY_DIR_OTHER','smarty/');

require('newsmarty.php');

$conn=new mysqli("localhost", "root", "myz","News");

$NewsID = $_GET["id"]; //获取新闻编号
$NewsType = $_GET["type"]; //要显示的新闻类型
switch($NewsType)
{
case 1:
$dbName = "tb_news_ch";
break;
case 2:
$dbName = "tb_news_in";
break;
case 3:
$dbName = "tb_news_mu";
break;
}

$query = "SELECT vcNewsTitle, ltNewsContent FROM $dbName";
$result = mysqli_query($conn,$query);
if($row = mysqli_fetch_assoc($result))
{
$smarty->assign("NewsTitle", $row["vcNewsTitle"]);
$smarty->assign("NewsContent", $row["ltNewsContent"]);

mysqli_free_result($result);
$smarty->display("news.html");
}

mysqli_close($conn);

?>



newsmarty.php

<?php
/*
* @author 马永占
* @email mailmyz*gmail.com
*/

require(SMARTY_DIR.'Smarty.class.php');

$smarty = new Smarty;

$smarty->template_dir = SMARTY_DIR_OTHER.'templates';//设置模板目录
$smarty->config_dir = SMARTY_DIR_OTHER.'config';//设置配置目录
$smarty->compile_dir = SMARTY_DIR_OTHER.'templates_c';//设置编译目录
$smarty->cache_dir = SMARTY_DIR_OTHER.'cache';//设置缓存目录
$smarty->cache_lifetime = 60 * 60 * 24; //设置缓存时间,60秒*60*24
$smarty->caching = false; //设置缓存方式,这个属性告诉Smarty是否要进行缓存以及缓存的方式。
//它可以取3个值,0:Smarty默认值,表示不对模板进行缓存;1:表示Smarty将使用当前定义的cache_lifetime来决定是否结束cache;2:表示Smarty将使用在cache被建立时使用cache_lifetime这个值。
//习惯上使用true与false来表示是否进行缓存。

//----------------------------------------------------
//左右边界符,默认为{},但实际应用当中容易与JavaScript
//相冲突,所以建议设成<{}>或其它。
//----------------------------------------------------
$smarty->left_delimiter = "<{";
$smarty->right_delimiter = "}>";
?>



news.sql



-- phpMyAdmin SQL Dump
-- version 2.10.1
-- http://www.phpmyadmin.net
--
-- 主机: localhost
-- 生成日期: 2007 年 06 月 21 日 10:53
-- 服务器版本: 5.0.27
-- PHP 版本: 5.2.1

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

--
-- 数据库: `news`
--

-- --------------------------------------------------------

--
-- 表的结构 `tb_news_ch`
--

CREATE TABLE `tb_news_ch` (
`iNewsID` int(11) NOT NULL auto_increment,
`vcNewsTitle` varchar(50) NOT NULL,
`ltNewsContent` longtext NOT NULL,
PRIMARY KEY (`iNewsID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

--
-- 导出表中的数据 `tb_news_ch`
--


-- --------------------------------------------------------

--
-- 表的结构 `tb_news_in`
--

CREATE TABLE `tb_news_in` (
`iNewsID` int(11) NOT NULL auto_increment,
`vcNewsTitle` varchar(50) NOT NULL,
`ltNewsContent` longtext NOT NULL,
PRIMARY KEY (`iNewsID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

--
-- 导出表中的数据 `tb_news_in`
--


-- --------------------------------------------------------

--
-- 表的结构 `tb_news_mu`
--

CREATE TABLE `tb_news_mu` (
`iNewsID` int(11) NOT NULL auto_increment,
`vcNewsTitle` varchar(50) NOT NULL,
`ltNewsContent` longtext NOT NULL,
PRIMARY KEY (`iNewsID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

--
-- 导出表中的数据 `tb_news_mu`
--




footer.html

<center> 底部 </center>
</body>
</html>


header.html

<html>
<head>
<title>标题</title>
</head>
<body>


index.html

<{include file="header.html"}>
<table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#B9E9FF">
<tr>
<td valign="top" width="295" bgcolor="#B9E9FF">
<{section name=loop loop=$News_CH}>
<li><a href="news.php?type=1&id=<{$News_CH[loop].NewsID}>" class="newsfont"><{$News_CH[loop].NewsTitle}></a>
<{/section}>
</td>
</tr>
</table>
<table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#B9E900">
<tr>
<td width="295" valign="top">
<{section name=loop loop=$News_IN}>
<li><a href="news.php?type=2&id=<{$News_IN[loop].NewsID}>" class="newsfont"><{$News_IN[loop].NewsTitle}></a>
<{/section}>
</td>
</tr>
</table>
<table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#B9AADD">
<tr>
<td width="295" valign="top">
<{section name=loop loop=$News_MU}>
<li><a href="news.php?type=3&id=<{$News_MU[loop].NewsID}>" class="newsfont"><{$News_MU[loop].NewsTitle}></a>
<{/section}>
</td>
</tr>
</table>
<{include file="footer.html"}>


news.html

<title><{$NewsTitle}>---power by myz</title>
<div align="center"><{$NewsTitle}></div>
<p><{$NewsContent}></p>
娱乐休闲专区A 影视预告B 音乐咖啡C 英语阶梯D 生活百科
网页编程专区E AMPZF HTMLG CSSH JSI ASPJ PHPK JSPL MySQLM AJAX
Linux技术区 N 系统管理O 服务器架设P 网络/硬件Q 编程序开发R 内核/嵌入
管理中心专区S 发布网址T 版主议事U 事务处理