菜鸟学template的心得!

看这么多人问模板。我就把最近我学模板的心得说说。
用模板基本就分4步:
1:声明模板文件:用的方法:set_file()
2:声明变量:set_file()如果没有可以不声明。
*3:模板分析:parse()
4:输出:p()

例子:

all.php
这个是我给单位做的其中菜单上面的部分都是一样的就用了这个all.php然后其他的都引用这个。废话不说谈谈下面模板的使用。


CODE:
session_start();
include (
"template.php");//引入phplib模板

//设置变量
$width="768px";//宽度
$body_color="#ffffff";//背景颜色
$color="#333366";//边框颜色
$font_color="#000000";//欢迎条字体颜色
//变量设置完毕

//声明模板类。其中的参数是相对根目录的放模板文件的路径。
$t=new Template("/webPage/tem");

/*$set_file();模板文件的声明。其中如果就声明一个模板就可以这样写:
$t->set_file("head","head.tem");
其中:
head可是随便命名就是个变量。
head.tem是我的模板文件。扩展名可以写别的。没限制。就在这里模板文件里写html代码。和设置变量进行替换。

=>:这是数组的赋值符号。      
*/
$t->set_file(array("head"=>"head.tem","set_end"=>"end.tem","s_up"=>"border_up.tem","s_down"=>"border_down.tem"));

//也是声明变量
$title="金运博远";

//在这里声明在模板文件里的变量名字,为了后面的分析替换用。
//单个变量的写法和上面的一样。
//其中:"title"等就是在模板文件里写成这个形式{title}要替换的变量
$t->set_var(array("title"=>$title,"width"=>$width,"body_color"=>$body_color,"border_color"=>$color,"font_color"=>$font_color));

//分析模板
//两个参数。第一个随便写。第二个就是在set_file里声明的变量。
//pparse()是分析并输出。
//parse()是分析不输出。
$t->pparse("ps_head","head");
$t->parse("up","s_up");
$t->parse("down","s_down");

$t->parse("end","set_end");

//补:由于我在这里没用到基本的方法输出在此说明:
//这里的参数就是在parse()里声明的第一个参数。
$t->p("end");
?>

其中的很实用的技术block下次在写吧。呵呵。菜鸟又学新技术了。上面有不对的地方希望前辈指点。
希望:我还不了解template的具体工作情况。希望前辈教教!

PHP代码
<?php   
require_once("include/config_base.php");   
require 'libs/Smarty.class.php';   
$db = new DedeSql(false);   
$smarty = new Smarty;   
$smarty->compile_check = true;   
$db->SetQuery("Select id,title From #@__archives where iscommend=11 order by id desc limit 0,8");   
$db->Execute();   
while($nrow = $db->GetObject())   
{   
    $news_list[]=array("id"=>$nrow->id,"title"=>mb_substr($nrow->title,0,15,'gbk'));   
}   
$str=@file_get_contents("http://weather.265.com/weather.htm");   
$str=str_replace("<scrīpt>","</body>",$str);   
$smarty->assign("news ",$ news_list);   
$smarty->assign("weatherstr",$str);   
$smarty->display('index.html');   
?>   

Index.html
循环{section name=loop loop=$news}
<a href="plus/view.php?aid={$news[loop].id}">{$news[loop].title}</a><br />
{/section}
天气{$weatherstr }

posted on 2009-12-03 10:03  14的路  阅读(418)  评论(0编辑  收藏  举报

导航

友情链接:源码下载