查看: 5225|回复: 1
打印 上一主题 下一主题

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

[复制链接]
跳转到指定楼层
1#
发表于 2007-10-5 11:38:22 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
台州网址导航
一边学习,一边看官方,把大师兄的smarty重写了一遍,能运行.

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

  7. // 加载 Smarty library
  8. define('SMARTY_DIR','smarty/libs/');
  9. define('SMARTY_DIR_OTHER','smarty/');

  10. require('newsmarty.php');

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

  12. //编译并显示位于templates下的index.tpl模板
  13. $smarty->display('example1.html');
  14. ?>
复制代码
example2.php
  1. <?php
  2. /*
  3. * @参考 大师兄smarty教程
  4. * @author 马永占
  5. * @email mailmyz*gmail.com
  6. */

  7. // 加载 Smarty library
  8. define('SMARTY_DIR','smarty/libs/');
  9. define('SMARTY_DIR_OTHER','smarty/');

  10. require('newsmarty.php');

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

  20. //编译并显示位于/templates下的index.tpl模板
  21. $smarty->display("example2.html");
  22. ?>
复制代码
example3.php
  1. <?php
  2. /*
  3. * @参考 大师兄smarty教程
  4. * @author 马永占
  5. * @email mailmyz*gmail.com
  6. */

  7. // 加载 Smarty library
  8. define('SMARTY_DIR','smarty/libs/');
  9. define('SMARTY_DIR_OTHER','smarty/');

  10. require('newsmarty.php');

  11. //--------------------------------------------------------------------------------------
  12. //处理{html_checkboxes name="CheckBox" values=$CheckName checked=$IsChecked output=$value separator="
  13. "}
  14. //--------------------------------------------------------------------------------------
  15. $smarty->assign('CheckName', array(
  16. 1001 => '语文',
  17. 1002 => '数学',
  18. 1003 => '外语'));
  19. $smarty->assign('IsChecked', 1001);


  20. //--------------------------------------------------------------------------------------
  21. //处理{html_radios name="RadioBox" values=$RadioName checked=$IsChecked output=$value separator="
  22. "}
  23. //--------------------------------------------------------------------------------------
  24. $smarty->assign('RadioName', array(
  25. 1001 => '语文1',
  26. 1002 => '数学1',
  27. 1003 => '外语1'));
  28. $smarty->assign('IsChecked', 1001);

  29. //--------------------------------------------------------------------------------------
  30. //{html_select_date}不用处理会自动输出
  31. //--------------------------------------------------------------------------------------


  32. //编译并显示位于/templates下的index.tpl模板
  33. $smarty->display("example3.html");
  34. ?>
复制代码
example4.php
  1. <?php
  2. /*
  3. * @参考 大师兄smarty教程
  4. * @author 马永占
  5. * @email mailmyz*gmail.com
  6. */

  7. // 加载 Smarty library
  8. define('SMARTY_DIR','smarty/libs/');
  9. define('SMARTY_DIR_OTHER','smarty/');

  10. require('newsmarty.php');

  11. $array[]= array("newsID"=>"001", "newsTitle"=>"第1条新闻");
  12. $array[]= array("newsID"=>"002", "newsTitle"=>"第2条新闻");
  13. $array[]= array("newsID"=>"003", "newsTitle"=>"第3条新闻");
  14. $array[]= array("newsID"=>"004", "newsTitle"=>"第4条新闻");
  15. $array[]= array("newsID"=>"005", "newsTitle"=>"第5条新闻");
  16. $array[]= array("newsID"=>"006", "newsTitle"=>"第6条新闻");
  17. $array[]= array("newsID"=>"007", "newsTitle"=>"第7条新闻");
  18. $array[]= array("newsID"=>"008", "newsTitle"=>"第8条新闻");


  19. $smarty->assign("News", $array);

  20. $smarty->display("example4.html");
  21. //$smarty->display("example4_2.html");
  22. //$smarty->display("example4_3.html");
  23. //$smarty->display("example4_4.html");
  24. ?>

复制代码
newsmarty.php
  1. <?php
  2. /*
  3. * @author 马永占
  4. * @email mailmyz*gmail.com
  5. */

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

  7. $smarty = new Smarty;

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

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


  2.   
复制代码
e_header.html


  1. 标题


  2.   
复制代码
example1.html
  1. <{* Smarty 注释 *}>
  2. <{include file="e_header.html"}>
  3. Hello, <{$name}>!
  4. <{include file="e_footer.html"}>
复制代码
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享分享 分享淘帖
台州维博网络(www.tzweb.com)专门运用PHP+MYSQL/ASP.NET+MSSQL技术开发网站门户平台系统等。
2#
 楼主| 发表于 2007-10-5 11:38:41 | 只看该作者
台州网址导航
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. 第六句把TEACHerLI@163.com全部变为小写:<{$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="teacherli@163.ccom" 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>
台州维博网络(www.tzweb.com)专门运用PHP+MYSQL/ASP.NET+MSSQL技术开发网站门户平台系统等。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

网站推广
关于我们
  • 台州朗动科技(Tzweb.com)拥有多年开发网站平台系统门户手机客户端等业务的成功经验。主要从事:政企网站,系统平台,微信公众号,各类小程序,手机APP客户端,浙里办微应用,浙政钉微应用、主机域名、虚拟空间、后期维护等服务,满足不同企业公司的需求,是台州地区领先的网络技术服务商!

Hi,扫描关注我

Copyright © 2005-2026 站长论坛 All rights reserved

Powered by 站长论坛 with TZWEB Update Techonolgy Support

快速回复 返回顶部 返回列表