smarty簡單應(yīng)用實(shí)例
本文講述了smarty簡單應(yīng)用實(shí)例。分享給大家供大家參考,具體如下:
<?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服務(wù)器連接 mysql_select_db("lava_guess"); //鏈接數(shù)據(jù)庫 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); //執(zhí)行sql語句,返回結(jié)果 //把記錄集轉(zhuǎn)換為數(shù)組 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} 新聞編號(hào):{$newsArray[loop].newsID}<br> 新聞標(biāo)題:{$newsArray[loop].newsTitle}<br><hr> {sectionelse} 對不起,沒有任何新聞輸入! {/section}
希望本文所述對大家基于smarty模板的php程序設(shè)計(jì)有所幫助。
相關(guān)文章
Yii中的relations數(shù)據(jù)關(guān)聯(lián)查詢及統(tǒng)計(jì)功能用法詳解
這篇文章主要介紹了Yii中的relations數(shù)據(jù)關(guān)聯(lián)查詢及統(tǒng)計(jì)功能用法,結(jié)合實(shí)例形式分析了關(guān)聯(lián)查詢命名空間及評(píng)論統(tǒng)計(jì)功能相關(guān)技巧,需要的朋友可以參考下2016-07-07PhpStorm2020 + phpstudyV8 +XDebug的教程詳解
這篇文章主要介紹了PhpStorm2020 + phpstudyV8 +XDebug的教程詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09Zend Framework實(shí)現(xiàn)具有基本功能的留言本(附demo源碼下載)
這篇文章主要介紹了Zend Framework實(shí)現(xiàn)具有基本功能的留言本,結(jié)合實(shí)例形式較為詳細(xì)的分析了Zend Framework實(shí)現(xiàn)留言本的具體步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-03-03php中PDO方式實(shí)現(xiàn)數(shù)據(jù)庫的增刪改查
PDO是mysql數(shù)據(jù)庫操作的一個(gè)公用類了,我們不需要進(jìn)行自定類就可以直接使用pdo來操作數(shù)據(jù)庫了,但是在php默認(rèn)配置中pdo是未開啟所以我們必須先在php.ini中開啟它才可以使用。2015-05-05將PHP程序中返回的JSON格式數(shù)據(jù)用gzip壓縮輸出的方法
這篇文章主要介紹了將PHP中返回的JSON格式數(shù)據(jù)用gzip壓縮輸出的方法,文中示例環(huán)境為Linux系統(tǒng)與Apache服務(wù)器,需要的朋友可以參考下2016-03-03Zend Framework框架的zend_cache緩存使用方法(zend框架)
這篇文章主要介紹了Zend_Cache文件緩存的基本操作,簡單的示例,,需要的朋友可以參考下2014-03-03