smarty简单应用实例_php实例

WBOY
풀어 주다: 2016-06-07 17:11:09
원래의
715명이 탐색했습니다.

本文讲述了smarty简单应用实例。分享给大家供大家参考,具体如下:

<&#63;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');
&#63;>
로그인 후 복사

模板文件: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}

로그인 후 복사

希望本文所述对大家基于smarty模板的php程序设计有所帮助。

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!