template_dir=smarty/templates/templates;$smarty->compile_dir=smarty/templates/templates_c;$smarty->config_dir=smarty/templates/config;$smarty->cache_dir=smarty/templates/cache; $hello = hello world!;$smarty->compile_check = true;//$smarty->debugging = true;$smarty->debugging = false;$smarty->caching=true;$conn=mysql_connect(localhost, root,root); //打开mysql服务器连接mysql_select_db(lava_guess); //链接数据库mysql_query(set names utf8); //解决中文乱码问题$sql_list=select * from t_sys_msg order by id desc limit 0,10;$result_list=mysql_query($sql_list); //执行sql语句,返回结果//把记录集转换为数组while($rs_list=mysql_fetch_array($result_list)) { $msg_array[]=$rs_list;}$array[] = array(newsid=>1, newstitle=>第1条新闻); $array[] = array(newsid=>2, newstitle=>第2条新闻); $array[] = array(newsid=>3, newstitle=>第3条新闻); $array[] = array(newsid=>4, newstitle=>第4条新闻); $array[] = array(newsid=>5, newstitle=>第5条新闻); $array[] = array(newsid=>6, newstitle=>第6条新闻);$smarty->assign(newsarray, $array);$smarty->assign(hello,$hello);//赋值$smarty->assign(msglist,$msg_array);$smarty->display('index.tpl');?>
模板文件:index.tpl
smarty{$hello} {section name=loop loop=$msglist} id: {$msglist[loop].id}--{$msglist[loop].title}----{$msglist[loop].receiver}
{/section}{section name=loop loop=$newsarray}新闻编号:{$newsarray[loop].newsid}
新闻标题:{$newsarray[loop].newstitle}
{sectionelse}对不起,没有任何新闻输入!{/section}
希望本文所述对大家基于smarty模板的php程序设计有所帮助。