smarty简单测试例子

  1. 云栖社区>
  2. 博客列表>
  3. 正文

smarty简单测试例子

javaboy2012 2009-05-21 13:52:00 浏览754 评论0

摘要: smarty简单测试例子      <?phprequire 'smarty/libs/Smarty.class.php';$smarty = new Smarty;$smarty->template_dir="smarty/templates/templates";$smarty->compil...

smarty简单测试例子

 

 

 <?php
require 'smarty/libs/Smarty.class.php';
$smarty = new Smarty;
$smarty->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

 

 

  <html>
  <head>
  <metahttp-equiv="Content-Type" content="text/html;charset=utf-8">
  <title>Smarty</title></head>
  <body>
<hr>
{$hello}

 

<hr>


  {section name=loop loop=$msglist}
  id: {$msglist[loop].id}--{$msglist[loop].title}----{$msglist[loop].receiver}<br>
  {/section}


<hr>

{section name=loop loop=$newsArray}
新闻编号:{$newsArray[loop].newsID}<br>
新闻标题:{$newsArray[loop].newsTitle}<br><hr>
{sectionelse}
对不起,没有任何新闻输入!
{/section}

 

【云栖快讯】阿里云栖开发者沙龙(Java技术专场)火热来袭!快来报名参与吧!  详情请点击

网友评论