assign 設(shè)置傳輸變量$smarty->display 加載模板..."/>
溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

PHP 模板smarty練習(xí)

發(fā)布時(shí)間:2020-06-29 05:30:05 來源:網(wǎng)絡(luò) 閱讀:739 作者:一百個(gè)小排 欄目:web開發(fā)

PHP 模板smarty練習(xí)
一.練習(xí)環(huán)境
PHP 模板smarty練習(xí)

smarty_inc為smarty lib庫
smarty_inc.php導(dǎo)入庫文件

<?php
include_once ("smarty_inc/Smarty.class.php");
$smarty = new Smarty();  //實(shí)例化
$smarty->config_dir="Smarty/Config_File.class.php";
$smarty->caching=false;  //緩存
$smarty->template_dir = "./templates";  //模板文件
$smarty->compile_dir = "./templates_c"; // 設(shè)定編譯文件的存儲(chǔ)路徑
//$smarty->cache_dir = "./smarty_cache";
$smarty->left_delimiter = "{";
$smarty->right_delimiter = "}";
?>

二.smarty變量練習(xí)
$smarty->assign 設(shè)置傳輸變量
$smarty->display 加載模板
index.php

<?php
include ("smarty_inc.php");
error_reporting(7);//不顯示警告報(bào)錯(cuò),255列出所有提示,0關(guān)閉所有提示

$str = 'this is my home!';
$smarty->assign('str',$str);
$smarty->display("index.html");
?>

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>smarty test</title>
</head>
<body>
    {$str}

</body>
</html>

訪問localhost/smarty/index.php 直接轉(zhuǎn)到index.html
this is my home!

三.smarty數(shù)組練習(xí)
index.php

<?php
include ("smarty_inc.php");
error_reporting(7);//不顯示警告報(bào)錯(cuò),255列出所有提示,0關(guān)閉所有提示

$students = ['sunqing','liuyao','yuxx','王舞'];
$smarty->assign('name',$students);

$smarty->display("index.html");

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>smarty test</title>
</head>
<body>

    {section name=stu loop=$name}
        {$name[stu]}
    {sectionelse}
        無內(nèi)容
    {/section}
</body>
</html>

PHP 模板smarty練習(xí)

四.smarty二維數(shù)組練習(xí)
index.php

<?php
include ("smarty_inc.php");
error_reporting(7);//不顯示警告報(bào)錯(cuò),255列出所有提示,0關(guān)閉所有提示

$students[] = ['stu_name'=>'sunqiang'];
$students[] = ['stu_name'=>'liuyao'];
$students[] = ['stu_name'=>'yuxx'];

$smarty->assign('name',$students);
$smarty->display("index.html");

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>smarty test</title>
</head>
<body>

    {section name=stu loop=$name}
        {$name[stu].stu_name}
    {sectionelse}
        無內(nèi)容
    {/section}
</body>
</html>

PHP 模板smarty練習(xí)

五.smarty標(biāo)量操作符練習(xí)
index.php

<?php
include ("smarty_inc.php");
error_reporting(7);//不顯示警告報(bào)錯(cuò),255列出所有提示,0關(guān)閉所有提示

$str = 'this is my home!';
$smarty->assign('str',$str);
$smarty->assign('time',time());
$smarty->assign('score',123.456);

$smarty->display("index.html");

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>smarty test</title>
</head>
<body>

原始字符:{$str}<br>
<hr>
首字母大寫:{$str|capitalize}<br>
計(jì)算字符數(shù):{$str|count_characters}<br>
連接內(nèi)容:{$str|cat:' i love study php'}<br>
段落數(shù):{$str|count_paragraphs}<br> <!--回車計(jì)算段落數(shù)-->
計(jì)算句數(shù):{$str|count_sentences}<br>
計(jì)算單詞數(shù):{$str|count_words}<br>
日期顯示:{$time|date_format:'%Y-%m-%d %H:%M:%S'} | {$smarty.now|date_format:'%Y-%m-%d %H:%M:%S'} | {$smarty.now}<br>
默認(rèn)顯示值:{$str1|default:'no content'}<br>
轉(zhuǎn)碼:{$str|escape:url} | {$str|escape:html}<br>
縮進(jìn):{$str|indent:5:'.'} | {$str|indent:5:'?'}<br>
大寫:{$str|upper}<br>
小寫:{$str|lower}<br>
替換:{$str|replace:'my':'your'} | {$str|replace:'my':'**'}<br><!--屏蔽關(guān)鍵詞-->
字符串格式化:{$score|string_format:'%.2f'} | {$score|string_format:'%d'}<br><!--保留小數(shù)點(diǎn)2位,保留整數(shù)-->
去空格:{$str|strip:''} | {$str|strip:'_'}<br>
去html標(biāo)簽:{$str|strip_tags}<br>
截?。簕$str|truncate:10:'...'}<br>
行寬約束:{$str|wordwrap:10:'<br>'}

</body>
</html>

PHP 模板smarty練習(xí)

六.smarty內(nèi)置函數(shù)練習(xí)

1.有鍵值數(shù)組
index.php

<?php
include ("smarty_inc.php");
error_reporting(7);//不顯示警告報(bào)錯(cuò),255列出所有提示,0關(guān)閉所有提示

$arr_str = ['teacher1'=>'sq','teacher2'=>'ly','teacher3'=>'yxx'];
$smarty->assign('teacher_name',$arr_str);

$smarty->display("index.html");

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>smarty test</title>
</head>
<body>

    {foreach from=$teacher_name item=name key=teach}
        數(shù)組內(nèi)容:{$teach} - {$name}<br>
    {/foreach}

</body>
</html>

PHP 模板smarty練習(xí)

2.無鍵值數(shù)組

index.php

<?php
include ("smarty_inc.php");
error_reporting(7);//不顯示警告報(bào)錯(cuò),255列出所有提示,0關(guān)閉所有提示

$arr_str = ['sq','ly','yxx'];
$smarty->assign('teacher_name',$arr_str);

$smarty->display("index.html");

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>smarty test</title>
</head>
<body>

    {foreach from=$teacher_name item=name key=teach}
        數(shù)組內(nèi)容:{$teach} - {$name}<br>
    {/foreach}

</body>
</html>

PHP 模板smarty練習(xí)

index.php

<?php
include ("smarty_inc.php");
error_reporting(7);//不顯示警告報(bào)錯(cuò),255列出所有提示,0關(guān)閉所有提示

$arr_str = ['sq','ly','yxx'];
$smarty->assign('teacher_name',$arr_str);

$smarty->display("index.html");

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>smarty test</title>
</head>
<body>

    {foreach from=$teacher_name item=name}
        數(shù)組內(nèi)容:{$name}<br>
    {/foreach}

</body>
</html>

PHP 模板smarty練習(xí)

3.if條件語句
index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>smarty test</title>
</head>
<body>

條件語句:
    {if name==''}
        沒有設(shè)置內(nèi)容
    {else}
        有設(shè)置內(nèi)容
    {/if}
</body>
</html>

因?yàn)閕ndex.php沒有設(shè)置這個(gè)變量,所有顯示沒有設(shè)置內(nèi)容
PHP 模板smarty練習(xí)

4.include的使用
在模板下創(chuàng)建一個(gè)拼接文件head.html
PHP 模板smarty練習(xí)

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>smarty test</title>
</head>
<body>
{include file='head.html'}
條件語句:
    {if name==''}
        沒有設(shè)置內(nèi)容
    {else}
        有設(shè)置內(nèi)容
    {/if}
</body>
</html>

PHP 模板smarty練習(xí)

注:
{include file=‘d:\www\index2.php’} 可以是別的目錄下的引入文件
{include file='head.html' title=‘menu’}引入head.html,將title的值傳給head.html的中的變量title

5.literal的使用
literal數(shù)據(jù)將被當(dāng)作文本處理,此時(shí)模板將忽略其內(nèi)部的所有字符信息,該特性用于顯示有可能包含大括號(hào)等字符信息的javascript腳本,因?yàn)樵谀0逶O(shè)置中php的邊界設(shè)定為大括號(hào),防止沖突。

{literal}
    <script language=javascript>
    ......
    </script>
{/literal}

6.Strip的使用
strip標(biāo)記中數(shù)據(jù)的首尾空格和回車,這樣可以保證模板容易理解且不用擔(dān)心多余的空格導(dǎo)致問題,使用會(huì)做整行的處理,但內(nèi)容不變,同時(shí)節(jié)省了流量,還可以保護(hù)代碼。
使用時(shí)在html代碼 頭和尾
{strip}
<html>
</html>
{/strip}

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI