摘要:<?php //index.php include __DIR__."/config/config.php"; $smarty->assign('rows',$rows); //1、单变量的值 $name = "smarty 课堂练习"; $smarty->assign('na
<?php
//index.php
include __DIR__."/config/config.php";
$smarty->assign('rows',$rows);
//1、单变量的值
$name = "smarty 课堂练习";
$smarty->assign('name',$name);
//2、数组:数组
$array=['name'=>'波多野结衣','phone'=>'1221111222','country'=>'日本','birthday'=>'1988-05-24','weight'=>'100','height'=>'163'];
$smarty->assign('arr',$array);
//4、多维数组
$actor=[
['name'=>'波多野结衣','phone'=>'1221111222','country'=>'日本','birthday'=>'1988-05-24','weight'=>'100','height'=>'163'],
['name'=>'大桥未久','phone'=>'14455554444','country'=>'日本','birthday'=>'1987-05-24','weight'=>'90','height'=>'158'],
['name'=>'京香','phone'=>'16644442222','country'=>'日本','birthday'=>'1987-03-26','weight'=>'106','height'=>'165'],
];
$smarty->assign('act',$actor);
//5、对象属性和方法
class Test
{
public $site='PHP中文网';
public function welcome()
{
return '欢迎来到'.$this->site;
}
}
$test = new Test;
$smarty->assign('test',$test);
//6、自定义函数
function add($a,$b)
{
return $a+$b;
}
//7、常量
const SITE_NAME='php中文网';
//8、系统变量
$_POST['user_name']='超级管理员';
$_GET['page']=10;
$smarty->display('index.html');
?>
<!----------------------------------------------原生html php混编------------------------------------------------------>
<!--<!doctype html>-->
<!--<html>-->
<!--<head>-->
<!-- <meta charset="UTF-8">-->
<!-- <meta name="viewport"-->
<!-- content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">-->
<!-- <meta http-equiv="X-UA-Compatible" content="ie=edge">-->
<!-- <title>演员信息表</title>-->
<!--</head>-->
<!--<body>-->
<?php //if(count($rows)>0):?>
<!-- <table border="1" cellpadding="5" cellspacing="0" width="60%">-->
<!-- <caption><h2>演员信息表</h2></caption>-->
<!-- <tr bgcolor="#90ee90">-->
<!-- <th>ID</th>-->
<!-- <th>姓名</th>-->
<!-- <th>电话</th>-->
<!-- <th>国籍</th>-->
<!-- <th>出生日期</th>-->
<!-- <th>胸围</th>-->
<!-- <th>身高</th>-->
<!-- <th>入职时间</th>-->
<!-- </tr>-->
<!-- --><?php //foreach ($rows as $row) : ?>
<!-- <tr>-->
<!-- <td>--><?php //echo $row['uid'] ?><!--</td>-->
<!-- <td>--><?php //echo $row['name'] ?><!--</td>-->
<!-- <td>--><?php //echo $row['phone']?><!--</td>-->
<!-- <td>--><?php //echo $row['country'] ?><!--</td>-->
<!-- <td>--><?php //echo $row['birthday'] ?><!--</td>-->
<!-- <td>--><?php //echo $row['weight'] ?><!--</td>-->
<!-- <td>--><?php //echo $row['height'] ?><!--</td>-->
<!-- <td>--><?php //echo date('Y 年 m 月 d 日',$row['add_time']) ?><!--</td>-->
<!-- </tr>-->
<!-- --><?php //endforeach; ?>
<!-- </table>-->
<?php //else:?>
<?php //endif;?>
<!--</body>-->
<!--</html>--><!--index.html 模板文件-->
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>模板文件渲染页面</title>
</head>
<body>
<!--smarty语句嵌入html-->
<table border="1" cellpadding="5" cellspacing="0" width="60%">
<caption><h2>演员信息表</h2></caption>
<tr bgcolor="#90ee90">
<th>ID</th>
<th>姓名</th>
<th>电话</th>
<th>国籍</th>
<th>出生日期</th>
<th>胸围</th>
<th>身高</th>
<th>入职时间</th>
</tr>
{foreach $rows as $row}
<tr>
<td>{$row.uid}</td>
<td>{$row.name}</td>
<td>{$row.phone}</td>
<td>{$row.country}</td>
<td>{$row.birthday}</td>
<td>{$row.weight}</td>
<td>{$row.height}</td>
<td>{$row.add_time|date_format:"%Y-%m-%d"}</td>
</tr>
{foreachelse}
<h2>表中没有数据</h2>
{/foreach}
</table>
<h3>渲染的单值变量值:{$name}</h3>
<hr>
<h3>渲染的数组变量值:{$arr.name}</h3>
<hr>
<h3>渲染的多维数组变量值:{$act.1.name}</h3>
<hr>
<h3>对象属性和方法值的渲染:属性:{$test->site},方法:{$test->welcome()}</h3>
<hr>
<h3>自定义函数的渲染:{add({$act.0.height},{$act.1.height})}</h3>
<hr>
<h3>常量值的渲染:{$smarty.const.SITE_NAME}</h3>
<hr>
<h3>对post值的渲染:{$smarty.post.user_name}</h3>
<hr>
<h3>对get值的渲染:{$smarty.get.page}</h3>
<hr>
{*读取配置文件*}
{config_load file="app.conf"}
<h3>应用配置文件渲染:{$smarty.config.app_name}</h3>
<h3>应用配置文件渲染:{$smarty.config.page_title}</h3>
<hr>
</body>
</html><?php
/**config.php配置文件
* smarty 模板引擎配置文件
*/
require __DIR__ .'/../libs/Smarty.class.php';
$pdo = new PDO('mysql:host=127.0.0.1;dbname=php_io','root','root');
$stmt=$pdo->prepare("SELECT *FROM user");
$stmt->execute();
$rows=$stmt->fetchAll(PDO::FETCH_ASSOC);
$smarty = new smarty();
//配置文件配置:必选
//模板文件所在目录
$smarty->setTemplateDir(__DIR__.'/../temp');
//模板编译文件所在目录
$smarty->setCompileDir(__DIR__.'/../temp_c');
//缓存目录
$smarty->setCacheDir(__DIR__.'/../cache');
//配置目录
$smarty->setConfigDir(__DIR__.'/../config');
//可选配置
$smarty->setLeftDelimiter('{');
$smarty->setRightDelimiter('}');
//配置缓存
$smarty->setCaching(false);
$smarty->setCacheLifetime(60*60*24*7);
经过本章的学习,学会了html+php混编和读取数据库数据到页面中运用了比如:
<?php //if(count($rows)>0):?>
<!-- <table border="1" cellpadding="5" cellspacing="0" align="center" width="60%">-->
<!-- <caption><h2>演员信息表</h2></caption>-->
<!-- <tr bgcolor="#90ee90">-->
<!-- <th>ID</th>-->
<!-- <th>姓名</th>-->
<!-- <th>电话</th>-->
<!-- <th>国籍</th>-->
<!-- <th>出生日期</th>-->
<!-- <th>胸围</th>-->
<!-- <th>身高</th>-->
<!-- <th>入职时间</th>-->
<!-- </tr>-->
<!-- --><?php //foreach ($rows as $row) : ?>
<!-- <tr>-->
<!-- <td>--><?php //echo $row['uid'] ?><!--</td>-->
<!-- <td>--><?php //echo $row['name'] ?><!--</td>-->
<!-- <td>--><?php //echo $row['phone']?><!--</td>-->
<!-- <td>--><?php //echo $row['country'] ?><!--</td>-->
<!-- <td>--><?php //echo $row['birthday'] ?><!--</td>-->
<!-- <td>--><?php //echo $row['weight'] ?><!--</td>-->
<!-- <td>--><?php //echo $row['height'] ?><!--</td>-->
<!-- <td>--><?php //echo date('Y 年 m 月 d 日',$row['add_time']) ?><!--</td>-->
<!-- </tr>-->
<!-- --><?php //endforeach; ?>
<!-- </table>-->
<?php //else:?>
<?php //endif;?>以上代码中的Php语句形式可以消除大括号,这样php+html代码写起来更加简捷.
途中也学会了安装composer,和利用composer下载安装包到项目中,对smarty配置的模板文件编写有初步的掌握。
批改老师:查无此人批改时间:2019-05-08 09:08:11
老师总结:完成的不错。框架多学几个,对工作有帮助。继续加油。