亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

完成smarty引入到自己項目中,并對自己創(chuàng)建的模板進行渲染輸出

Original 2019-05-07 14:24:34 286
abstract:<?php //index.php include __DIR__."/config/config.php"; $smarty->assign('rows',$rows); //1、單變量的值 $name = "smarty 課堂練習(xí)"; $smarty->assign('na
<?php
//index.php
include __DIR__."/config/config.php";
$smarty->assign('rows',$rows);
//1、單變量的值
$name = "smarty 課堂練習(xí)";
$smarty->assign('name',$name);
//2、數(shù)組:數(shù)組
$array=['name'=>'波多野結(jié)衣','phone'=>'1221111222','country'=>'日本','birthday'=>'1988-05-24','weight'=>'100','height'=>'163'];
$smarty->assign('arr',$array);

//4、多維數(shù)組
$actor=[
    ['name'=>'波多野結(jié)衣','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中文網(wǎng)';
    public function welcome()
    {
        return '歡迎來到'.$this->site;
    }
}
$test = new Test;
$smarty->assign('test',$test);
//6、自定義函數(shù)
function add($a,$b)
{
    return $a+$b;
}
//7、常量
const SITE_NAME='php中文網(wǎng)';
//8、系統(tǒng)變量
$_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>表中沒有數(shù)據(jù)</h2>
    {/foreach}
</table>
<h3>渲染的單值變量值:{$name}</h3>
<hr>
<h3>渲染的數(shù)組變量值:{$arr.name}</h3>
<hr>
<h3>渲染的多維數(shù)組變量值:{$act.1.name}</h3>
<hr>
<h3>對象屬性和方法值的渲染:屬性:{$test->site},方法:{$test->welcome()}</h3>
<hr>
<h3>自定義函數(shù)的渲染:{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>應(yīng)用配置文件渲染:{$smarty.config.app_name}</h3>
<h3>應(yīng)用配置文件渲染:{$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);

20190507140338.jpg




經(jīng)過本章的學(xué)習(xí),學(xué)會了html+php混編和讀取數(shù)據(jù)庫數(shù)據(jù)到頁面中運用了比如:

<?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代碼寫起來更加簡捷.

途中也學(xué)會了安裝composer,和利用composer下載安裝包到項目中,對smarty配置的模板文件編寫有初步的掌握。

Correcting teacher:查無此人Correction time:2019-05-08 09:08:11
Teacher's summary:完成的不錯??蚣芏鄬W(xué)幾個,對工作有幫助。繼續(xù)加油。

Release Notes

Popular Entries