smarty简单应用实例


Posted in PHP onNovember 03, 2015

本文讲述了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}

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

PHP 相关文章推荐
PHP+APACHE实现用户论证的方法
Oct 09 PHP
使用Apache的htaccess防止图片被盗链的解决方法
Apr 27 PHP
php使用fsockopen函数发送post,get请求获取网页内容的方法
Nov 15 PHP
php操作xml入门之xml标签的属性分析
Jan 23 PHP
PHP常用处理静态操作类
Apr 03 PHP
ThinkPHP文件缓存类代码分享
Apr 22 PHP
织梦sitemap地图实时推送给百度的教程
Aug 03 PHP
PHP+shell脚本操作Memcached和Apache Status的实例分享
Mar 11 PHP
详解Yii2高级版引入bootstrap.js的一个办法
Mar 21 PHP
phpStudy配置多站点多域名和多端口的方法
Sep 01 PHP
PHP读取并输出XML文件数据的简单实现方法
Dec 22 PHP
php过滤htmlspecialchars() 函数实现把预定义的字符转换为 HTML 实体用法分析
Jun 25 PHP
详解PHP+AJAX无刷新分页实现方法
Nov 03 #PHP
php获取远程文件的内容和大小
Nov 03 #PHP
php获取远程文件内容的函数
Nov 02 #PHP
php+ajax实现无刷新数据分页的办法
Nov 02 #PHP
PHP中Http协议post请求参数
Nov 02 #PHP
浅谈PHP中foreach/in_array的使用
Nov 02 #PHP
php防止用户重复提交表单
Nov 02 #PHP
You might like
动态网站web开发 PHP、ASP还是ASP.NET
2006/10/09 PHP
php数组使用规则分析
2015/02/27 PHP
浅谈php处理后端&amp;接口访问超时的解决方法
2016/10/29 PHP
PHP载入图像imagecreatefrom_gif_jpeg_png系列函数用法分析
2016/11/14 PHP
查询绑定数据岛的表格中的文本并修改显示方式的js代码
2009/12/15 Javascript
javascript向flash swf文件传递参数值注意细节
2012/12/11 Javascript
nullJavascript中创建对象的五种方法实例
2013/05/07 Javascript
jQuery jcrop插件截图使用方法
2013/11/20 Javascript
Linux下编译安装php libevent扩展实例
2015/02/14 Javascript
js实现下拉框选择要显示图片的方法
2015/02/16 Javascript
JavaScript获取表单内所有元素值的方法
2015/04/02 Javascript
js实现鼠标拖动功能
2017/03/20 Javascript
jquery图片放大镜效果
2017/06/23 jQuery
jQuery实现html双向绑定功能示例
2017/10/09 jQuery
图解javascript作用域链
2019/05/27 Javascript
vue中的面包屑导航组件实例代码
2019/07/01 Javascript
Vue中img的src是动态渲染时不显示的解决
2019/11/14 Javascript
JS函数进阶之继承用法实例分析
2020/01/15 Javascript
vue实现路由不变的情况下,刷新页面操作示例
2020/02/02 Javascript
Python自动发邮件脚本
2017/03/31 Python
Python有序查找算法之二分法实例分析
2017/12/11 Python
深入浅析Python传值与传址
2018/07/10 Python
python调用百度语音REST API
2018/08/30 Python
Python读写压缩文件的方法
2020/07/30 Python
移动端Html5页面生成图片解决方案
2018/08/07 HTML / CSS
Html5原创俄罗斯方块(基于canvas)
2019/01/07 HTML / CSS
中国电视购物:快乐购
2017/02/04 全球购物
美国眼镜在线零售商:Dualens
2019/12/07 全球购物
劳动模范事迹材料
2014/01/19 职场文书
生产部岗位职责范文
2014/02/07 职场文书
投资建议书模板
2014/05/12 职场文书
诚信承诺书模板
2014/05/26 职场文书
村长反四风问题个人对照检查材料
2014/09/21 职场文书
政风行风评议心得体会
2014/10/21 职场文书
如何写好活动总结
2019/06/21 职场文书
Python中OpenCV实现查找轮廓的实例
2021/06/08 Python