PHP 关于smarty模板引擎的使用

1、需要下载smarty模板引擎的依赖

利用composer工具查询smarty模板引擎 =>利用composer安装smarty模板引擎

2、smarty 模板引擎的初步的使用

  a、引入smarty的类require_once(''./smarty/smarty/libs/Smarty.class.php)

  b、实例化smarty类,如果有配置项的内容,可以指定配置项的总目录

  c、数据绑定

  d、模板替换与展示,具体如下

(php代码部份)

<?php
ini_set('display_errors', true);
header('content-type: text/html; charset=utf8');
require_once('./vendor/smarty/smarty/libs/Smarty.class.php');
require_once('./DB.php');
$res = (new Query('SELECT * FROM `user` WHERE `id`= :id'))->bindValue(':id', 2)->one();
$smarty = new Smarty();
//指定配置项的总目录
$smarty->setConfigDir('./smarty_config');
//测试常量
CONST TEST_TEXT = 'THIS IS TEST';
//注意 assign的参数有(key, value, nocache) key可以字符串或者数组, nocache表示是否缓存
$smarty->assign([
    'id' => $res['id'],
    'name' => $res['user'],
    'pwd' => $res['pwd']
], null , false);
try{
    //模板渲染
    $smarty->display('./temp/home.html');
}catch(SmartyException $e){
    exit($e->getMessage());
}catch(Exception $e){
    exit($e->getMessage());
}
?>

(配置项内容html_config.conf里的内容)

public_header = are you ok???

(html代码部份,在html中引入配置项的内容用如下语法)

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>home</title>
    <style>
        table{
            width: 1200px;
            border-collapse: collapse;
        }
        table td{
            border: 1px solid #000000;
            width: 300px;
            height: 40px;
            text-align: center;
        }
    </style>
</head>
<body>
<div class="container">
    <h1>this is test</h1>
    {config_load file='html_config.conf'}
    <table>
        <tr>
            <td>{#public_header#}-{$id}</td>
            <td>{#public_header#}-{$name}</td>
            <td>{#public_header#}-{$pwd}</td>
            <td>{#public_header#}</td>
        </tr>
        <tr>
            <td>当前时间截:{$smarty.now}</td>
            <td>显示常量: {$smarty.const.TEST_TEXT}</td>
            <td>获得配置项: {$smarty.config.public_header}</td>
            <td>当前路径: {$smarty.current_dir}</td>
            <!--以上路径表示的是模板放置的位置-->
        </tr>
    </table>
</div>
</body>
</html>

3、smarty常量,不用定义即可使用

{$smarty.now}获得当前时间的时间截

{$smarty.current_dir} 获得当前模板放置的位置

{$smarty.const}获得Php文件中定义的常量

{$smarty.config}获得smarty的配置项

smarty的变量调解器

 date_format  =>  格式化时间截

 capitalize  =>  首字母大写

 upper  =>  字符串整体大写

 lower  =>  字符串整体小写

 default  =>  设置默认值

 indent  =>  字符缩进

 string_format  => 对文本进行格式化

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
</head>
<body>
{config_load file="html_config.conf"}
<!--注意这里的分割符前后的位置不能有空格-->
<div>{$smarty.now|date_format: '%Y-%m-%d %H:%M:%S'}</div>   <!--输出 2019-07-02 00:46:51-->
<div>{$smarty.const.TEST_TEXT|lower}</div>  <!--输出 this is test-->
<div>{$smarty.const.TEST_TEXT|upper}</div>  <!--输出 THIS IS TEST-->
<div>{$smarty.config.public_header|capitalize}</div>    <!--输出 Are You Ok???-->
<div>{$smarty.config.public_header|string_format: '%.2s'}</div>    <!--输出 ar-->
<div>{$smarty.config.TEST_TEXT|indent:2:'&'}</div>   <!--输出 &&-->
<div>{$check|default: 'this is default value'}</div>    <!--输出 this is default value-->
<b>{$smarty.get.id}</b>   <!--接收get方式传输过来的id值-->
<b>{$smarty.post.id}</b>   <!--接收post方式传输过来的id值-->
<b>{$smarty.cookies.id}</b>   <!--接收cookies里的id值-->
<b>{$smarty.session.id}</b>   <!--接收session里的id值-->
<b>{$smarty.server.DOCUMENT_ROOT}</b>   <!--接收server对象里的document_root变量-->
</body>
</html>

注:沿用上面的PHP文件

4、对变量进行转义

Valori possibiliDefault
html,htmlall,url,quotes,hex,hexentity,javascript html

html 表示转化成html文本进行运行

url 表示转成url运行的格式相当于htmlencode的功能

quotes 表示对文字里的引号进行转义

hex 表示转义hex格式

javascript 表示转成javascript代码块运行

5、truncate的用法如下案例

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
</head>
<body>
{config_load file='html_config.conf'}
<div>{$test}</div>
<!--输出 this is a test, just a test-->
<div>{$test|truncate:10}</div>
<!--输出 this is...-->
<div>{$test|truncate:10:'---'}</div>
<!--输出 this is--- -->
<div>{$test|truncate:10:'***':true}</div>
<!--输出 this is***-->
<div>{$test|truncate:10:'---':false:true}</div>
<!--输出 thi---est-->
</body>
</html>

 注意truncate的第三个参数与第四个参数存在问题,尽可能在开发中不要去应用

 5、smarty中的流程控制语句

A、foreach    =>   基本语法 {foreach $data as $key => $val}  ...  {foreachelse} ... {/foreach}

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
    <!--目录指向的是当前调用的PHP的路径为当前路径-->
    <link rel="stylesheet" href="./resource/bootstrap.css">

</head>
<body>
{config_load file='html_config.conf'}
<div class="container">
    <table class="table table-condensed table-bordered table-striped table-hover">
        {foreach $data as $key => $val}
        <tr>
            <td>{$val['id']}</td>
            <td>{$val['user']}</td>
            <td>{$val['pwd']}</td>
        </tr>
        {foreachelse}
        <tr>
            <td rowspan="3">对不起,没有你需要的数据</td>
        </tr>
        {/foreach}
    </table>
</div>
</body>
</html>

 B、for循环  =>  基本语法    {for $i = 0 to 6} ...{/for} 默认步长为1  如果需要指定步长,那么语法为{for $i = 0 to 6 step=2}...{/for}步长为2(注意step=2中间不能有空隔)

<div class="container">
    <table class="table table-condensed table-bordered table-striped table-hover">
        <tr>
            {for $i = 0 to 6}
                <td>{$i}</td>
            {/for}
        </tr>
    </table>
</div>
<div class="container">
    <table class="table table-condensed table-bordered table-striped table-hover">
        <tr>
            {for $i = 0 to 6 step=2}
                <td>{$i}</td>
            {/for}
        </tr>
    </table>
</div>

C、if语句   {if ...}...{elseif ...}...{else}...{/if}

<div class="container">
    <table class="table table-condensed table-bordered table-striped table-hover">
        <tr>
            {if $smarty.const.NUM > 20}
            <td>大于20</td>
            {elseif $smarty.const.NUM> 10}
            <td>大于10</td>
            {else}
            <td>其他值</td>
            {/if}
        </tr>
    </table>
</div>

D、include语法的使用,用于引入html部份

原文地址:https://www.cnblogs.com/rickyctbu/p/11106588.html