1. 程式人生 > >PHP 模板smarty練習

PHP 模板smarty練習

入庫 ces del dex words title love 回車 標量

PHP 模板smarty練習
一.練習環境
技術分享圖片

smarty_inc為smarty lib庫
smarty_inc.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.php

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

$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 直接轉到index.html
this is my home!

三.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>

技術分享圖片

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

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

技術分享圖片

五.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:‘&nbsp;‘}<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練習