PHP 模板smarty練習

PHP 模板smarty練習
一.練習環境
PHP 模板smarty練習javascript

smarty_inc爲smarty lib庫
smarty_inc.php導入庫文件php

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

二.smarty變量練習
$smarty->assign 設置傳輸變量
$smarty->display 加載模板
index.phphtml

<?php
include ("smarty_inc.php");
error_reporting(7);//不顯示警告報錯,255列出全部提示,0關閉全部提示

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

index.htmljava

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

</body>
</html>

訪問localhost/smarty/index.php 直接轉到index.html
this is my home!api

三.smarty數組練習
index.php數組

<?php
include ("smarty_inc.php");
error_reporting(7);//不顯示警告報錯,255列出全部提示,0關閉全部提示

$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}
        無內容
    {/section}
</body>
</html>

PHP 模板smarty練習

四.smarty二維數組練習
index.phpmarkdown

<?php
include ("smarty_inc.php");
error_reporting(7);//不顯示警告報錯,255列出全部提示,0關閉全部提示

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

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

index.htmlide

<!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}
        無內容
    {/section}
</body>
</html>

PHP 模板smarty練習

五.smarty標量操做符練習
index.php函數

<?php
include ("smarty_inc.php");
error_reporting(7);//不顯示警告報錯,255列出全部提示,0關閉全部提示

$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>
計算字符數:{$str|count_characters}<br>
鏈接內容:{$str|cat:' i love study php'}<br>
段落數:{$str|count_paragraphs}<br> <!--回車計算段落數-->
計算句數:{$str|count_sentences}<br>
計算單詞數:{$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>
默認顯示值:{$str1|default:'no content'}<br>
轉碼:{$str|escape:url} | {$str|escape:html}<br>
縮進:{$str|indent:5:'.'} | {$str|indent:5:' '}<br>
大寫:{$str|upper}<br>
小寫:{$str|lower}<br>
替換:{$str|replace:'my':'your'} | {$str|replace:'my':'**'}<br><!--屏蔽關鍵詞-->
字符串格式化:{$score|string_format:'%.2f'} | {$score|string_format:'%d'}<br><!--保留小數點2位,保留整數-->
去空格:{$str|strip:''} | {$str|strip:'_'}<br>
去html標籤:{$str|strip_tags}<br>
截取:{$str|truncate:10:'...'}<br>
行寬約束:{$str|wordwrap:10:'<br>'}

</body>
</html>

PHP 模板smarty練習

六.smarty內置函數練習

1.有鍵值數組
index.php

<?php
include ("smarty_inc.php");
error_reporting(7);//不顯示警告報錯,255列出全部提示,0關閉全部提示

$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}
        數組內容:{$teach} - {$name}<br>
    {/foreach}

</body>
</html>

PHP 模板smarty練習

2.無鍵值數組

index.php

<?php
include ("smarty_inc.php");
error_reporting(7);//不顯示警告報錯,255列出全部提示,0關閉全部提示

$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}
        數組內容:{$teach} - {$name}<br>
    {/foreach}

</body>
</html>

PHP 模板smarty練習

index.php

<?php
include ("smarty_inc.php");
error_reporting(7);//不顯示警告報錯,255列出全部提示,0關閉全部提示

$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}
        數組內容:{$name}<br>
    {/foreach}

</body>
</html>

PHP 模板smarty練習

3.if條件語句
index.html

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

條件語句:
    {if name==''}
        沒有設置內容
    {else}
        有設置內容
    {/if}
</body>
</html>

由於index.php沒有設置這個變量,全部顯示沒有設置內容
PHP 模板smarty練習

4.include的使用
在模板下建立一個拼接文件head.html
PHP 模板smarty練習

index.html

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

PHP 模板smarty練習

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

5.literal的使用
literal數據將被看成文本處理,此時模板將忽略其內部的全部字符信息,該特性用於顯示有可能包含大括號等字符信息的javascript腳本,由於在模板設置中php的邊界設定爲大括號,防止衝突。

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

6.Strip的使用strip標記中數據的首尾空格和回車,這樣能夠保證模板容易理解且不用擔憂多餘的空格致使問題,使用會作整行的處理,但內容不變,同時節省了流量,還能夠保護代碼。使用時在html代碼 頭和尾{strip}<html></html>{/strip}

相關文章
相關標籤/搜索