温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

smarty的简单应用方式

发布时间:2021-08-06 19:18:24 来源:亿速云 阅读:117 作者:chen 栏目:开发技术

这篇文章主要介绍“smarty的简单应用方式”,在日常操作中,相信很多人在smarty的简单应用方式问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”smarty的简单应用方式”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

本文讲述了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的简单应用方式”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI