smarty使用

    Smarty是一个php模板引擎。更准确的说,它分开了逻辑程序和外在的内容,提供了一种易于管理的方法。可以描述为应用程序员

和美工扮演了不同的角色,因为在大多数情况下 ,他们不可能是同一个人。例如,你正在创建一个用于浏览新闻的网页,新闻标题,标签

栏,作者和内容等都是内容要素,他们并不包含应该怎样去呈现。在Smarty的程序里,这些被忽略了。模板设计者们编辑模板,组合使用

html标签和模板标签去格式化这些要素的输出(html表格,背景色,字体大小,样式表,等等)。有一天程序员想要改变文章检索的方式(也

就是程序逻辑的改变)。这个改变不影响模板设计者,内容仍将准确的输出到模板。同样的,哪天美工吃多了想要完全重做界面,也不会

影响到程序逻辑。因此,程序员可以改变逻辑而不需要重新构建模板,模板设计者可以改变模板而不影响到逻辑。smarty不尝试将逻辑

完全和模板分开。如果逻辑程序严格的用于页面表现,那么它在模板里不会出现问题。有个建议:让应用程序逻辑远离模板, 页面表现

逻辑远离应用程序逻辑。这将在以后使内容更容易管理,程序更容易升级。

3,smaty引擎的特点

模板编译。为减少开销,Smarty在默认情况下将模板转换为可比较的PHP脚本,使得后续的调用速度更快。Smarty还非常智能,在内容改变后可以重新编译。

缓存。Smarty还提供了缓存模板的可选特性。缓存与编译不同的是,支持缓存不只是能生成缓存的内容,还能防止执行个别逻辑。例如,你可以指定缓存文档的生存时间,比如5分钟,在此期间可以忽略与该模板有关的数据库查询。

高度可配置和可扩展。Smarty的面向对象架构允许修改和扩展其默认行为。此外,从一开始可配置性就是一个设计目标,为用户提供了很大的灵活性,通过内置方法和属性定制Smarty的行为。

安全。Smarty提供了很多安全特性,可以避免服务器和应用程序数据遭到设计人员有意或无意的破坏。

 

 

访问https://www.smarty.net/download

1,下好的压缩包解压之后是这样的:

 

目录中,demo文件夹为示例文件。Libs为程序文件。
/libs/Smarty.class.php   #主文件
/libs/sysplugins/  #内部plugin
/libs /plugins/   #外部plugin,可自由扩充

 

我们只需要用到libs包,把libs包放到自己项目的根目录

 

/cahce/   #放置缓存文件
/configs /   #放置可以载入的配置文件
/templates/   #放置模板文件
/templates_c/    #放置对模板编译后的文件

 

 

 

2,在libs根目录下新建这四个文件夹

根目录 test.php

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/12 0012
 * Time: 下午 12:56
 */
require "./libs/Smarty.class.php";
$smarty = new Smarty;
class Person{
    public $name="smile";
    public $age=25;
}
$person=new Person();
$smarty->assign('address',$person);
$smarty->display('./templates/test.html');

?>

 

templates/test.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
</head>
<body>
<!--html的注释-->
{*smarty模板的注释*}
<!--{assign var='add'  value='www.php.cn'}-->
<!--我们网站的网址是:{$address[2][0]}<br>-->
<!--我们网站的网址是:{$address.2.0}-->

我叫{$address->name}今年{$address->age}岁<br>
</body>
</html>

html注释会显示在网页内,smarty模板注释不会显示在最终页面上

 

使用:{$add}

注意:也可以定义与使用数组。假若上面定义的add为一个一维数组,使用时即:{$add[2]},{$add['aa']},{$add.aa}等等使用方式。

2,保留变量

 

 Smarty中有一个特殊的变量(就是smarty)可以通过这个变量很容易就可以访问到一些环境变量。就像PHP中的超全局变量一样神奇。

      注意:在使用这个保留变量的时候:smarty是对于大小写敏感的,我们需要的是小写的smarty

例子:

一、使用smarty访问PHP中的超全局数组变量:

  1、获取$_GET     {$smarty.get.name}    获取get中的name值

  2、获取$_POST    {$smarty.post.name}   获取post中的name值

  3、获取$_COOKIE  {$smarty.cooke.name}  获取cookie中的name值

  同理,还可以获取$_SERVER, $_ENV 和 $_SESSION等等

注意:虽然Smarty提供了较方便直接访问PHP超全局变量的方法,但必须谨慎使用。 直接访问超全局变量会弄乱应用程序底层代码和模板语法。 最佳的实践是从PHP将需要的变量对模板进行赋值再使用。

二、获取当前时间戳

  {$smarty.now}其原理就是调用了time()函数

三、直接访问PHP常量

  {$smarty.const.常量名}即{$smarty.const.AGE}

PHP定义常量

require"./libs/Smarty.class.php";

$smarty = new Smarty;

class Person{
public $name-"smile";

public $age-25;Sperson-new Person()
define("CL",男');

$smarty->assign(tpl_var:'address',Sperson)
$smarty->display(template:'./templates/test.html');

 

配置全局变量与段落变量

 

1,从配置文件中读取普通变量

 

在configs文件夹下新建Smarty.conf文件

 

里面写上变量:

 

pageTitle = "This is mine"
bodyBgColor = "#eeeeee"

 

引入模板文件:

{config_load file=". /configs/Smarty. conf"}

 

capitalize(将变量里的所有单词首字大写)

test.php:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Police begin campaign to rundown jaywalkers.');
$smarty->display('test.html');

test.html:
{$articleTitle}
{$articleTitle|capitalize}

输出:
Police begin campaign to rundown jaywalkers.
Police Begin Campaign To Rundown Jaywalkers.

 

count_characters(计算变量里的字符数)

test.php:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Cold Wave Linked to Temperatures.');
$smarty->display('test.html');

test.html:
{$articleTitle}
{$articleTitle|count_characters}
{$articleTitle|count_characters:true} //空格也计入数

输出:
Cold Wave Linked to Temperatures.
29
33

cat[连接字符串]

cat(将cat里的值连接到给定的变量后面)

test.php:
$smarty = new Smarty;
$smarty->assign('articleTitle', "Psychics predict world didn't end");
$smarty->display('test.html');

test.html:
{$articleTitle|cat:" yesterday."}

输出:
Psychics predict world didn't end yesterday.

count_paragraphs[计算段数]

count_paragraphs[计算段数]

test.php:
$smarty = new Smarty;
$smarty->assign('articleTitle', "War Dims Hope for Peace. Child's Death Ruins
Couple's Holiday.\n\nMan is Fatally Slain. Death Causes Loneliness, Feeling of Isolation.");
$smarty->display('test.html');

test.html:
{$articleTitle}
{$articleTitle|count_paragraphs}

输出:
War Dims Hope for Peace. Child's Death Ruins Couple's Holiday.
Man is Fatally Slain. Death Causes Loneliness, Feeling of Isolation.
2

count_sentences[计算句数]

count_sentences[计算句数]

test.php:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Two Soviet Ships Collide - One Dies. Enraged Cow Injures Farmer with Axe.');
$smarty->display('test.html');

test.html:
{$articleTitle}
{$articleTitle|count_sentences}

输出:
Two Soviet Ships Collide - One Dies. Enraged Cow Injures Farmer with Axe.
2

count_words[计算词数]

count_words[计算词数]

test.php:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Dealers Will Hear Car Talk at Noon.');
$smarty->display('test.html');

test.html:
{$articleTitle}
{$articleTitle|count_words}

输出:
Dealers Will Hear Car Talk at Noon.
7

date_format[格式化日期]

格式化从函数strftime()获得的时间和日期。
Unix或者mysql等的时间戳记(parsable by strtotime)都可以传递到smarty。
设计者可以使用date_format完全控制日期格式。
如果传给date_format的数据是空的,将使用第二个参数作为时间格式。  

test.php:
$config['date'] = '%I:%M %p';
$config['time'] = '%H:%M:%S';
$smarty->assign('config', $config);
$smarty->assign('yesterday', strtotime('-1 day'));

 

test.html:
{$smarty.now|date_format}<br>
{$smarty.now|date_format:"%D"}<br>
{$smarty.now|date_format:$config.date}<br>
{$yesterday|date_format}<br>
{$yesterday|date_format:"%A, %B %e, %Y"}<br>
{$yesterday|date_format:$config.time}<br>

 

需要注意一点修改php.ini配置:

加上date.timezone = Asia/Shanghai

不然会报错:

It is not safe to rely on the system’s timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function.

 

输出:
微信图片_20180312174000.png

 

date_format支持格式:

%a - 当前区域星期几的简写

%A - 当前区域星期几的全称

%b - 当前区域月份的简写

%B - 当前区域月份的全称

%c - 当前区域首选的日期时间表达

%C - 世纪值(年份除以 100 后取整,范围从 00 到 99)

%d - 月份中的第几天,十进制数字(范围从 01 到 31)

%D - 和 %m/%d/%y 一样

%e - 月份中的第几天,十进制数字,一位的数字前会加上一个空格(范围从 ' 1' 到 '31')

%g - 和 %G 一样,但是没有世纪

%G - 4 位数的年份

%h - 和 %b 一样

%H - 24 小时制的十进制小时数(范围从 00 到 23)

%I - 12 小时制的十进制小时数(范围从 00 到 12)

%j - 年份中的第几天,十进制数(范围从 001 到 366)

%k - 小时,24 小时格式,没有前导零

%l - 小时,12 小时格式,没有前导零

%m - 十进制月份(范围从 01 到 12)

%M - 十进制分钟数

%n - 换行符

%p - 根据给定的时间值为 `am' 或 `pm',或者当前区域设置中的相应字符串

%r - 用 a.m. 和 p.m. 符号的时间

%R - 24 小时符号的时间

%S - 十进制秒数

%t - 制表符

%T - 当前时间,和 %H:%M:%S 一样

%u - 星期几的十进制数表达 [1,7],1 表示星期一

%U - 本年的第几周,从第一周的第一个星期天作为第一天开始

%V - 本年第几周的 ISO 8601:1988 格式,范围从 01 到 53,第 1 周是本年第一个至少还有 4 天的星期,星期一作为每周的第一天。(用 %G 或者 %g 作为指定时间戳相应周数的年份组成。)

%w - 星期中的第几天,星期天为 0

%W - 本年的第几周数,从第一周的第一个星期一作为第一天开始

%x - 当前区域首选的时间表示法,不包括时间

%X - 当前区域首选的时间表示法,不包括日期

%y - 没有世纪数的十进制年份(范围从 00 到 99)

%Y - 包括世纪数的十进制年份

%Z - 时区名或缩写

default[默认值]

default[默认值]

test.php:

$smarty = new Smarty;
$smarty->assign('articleTitle', 'Dealers Will Hear Car Talk at Noon.');
$smarty->display('test.html');

test.html:
{$articleTitle|default:"no title"}
{$myTitle|default:"no title"}

输出:
Dealers Will Hear Car Talk at Noon.
no title

 

escape[编码]

 

escape[编码]

 

test.php:
$smarty = new Smarty;
$smarty->assign('articleTitle', "'Stiff Opposition Expected to Casketless Funeral Plan'");
$smarty->display('test.html');

test.html:
{$articleTitle}
{$articleTitle|escape}
{$articleTitle|escape:"html"} {* escapes & " ' < > *}
{$articleTitle|escape:"htmlall"} {* escapes ALL html entities *}
{$articleTitle|escape:"url"}
{$articleTitle|escape:"quotes"}
<a href="mailto:{$EmailAddress|escape:"hex"}">{$EmailAddress|escape:"hexentity"}</a>

输出:
'Stiff Opposition Expected to Casketless Funeral Plan'
&#039;Stiff Opposition Expected to Casketless Funeral Plan&#039;
&#039;Stiff Opposition Expected to Casketless Funeral Plan&#039;
&#039;Stiff Opposition Expected to Casketless Funeral Plan&#039;
%27Stiff+Opposition+Expected+to+Casketless+Funeral+Plan%27
\'Stiff Opposition Expected to Casketless Funeral Plan\'
<a href="mailto:%62%6f%62%40%6d%65%2e%6e%65%74">&#x62;&#x6f;&#x62;&#x40;&#x6d;&#x65;&#x2e;&#x6e;&#x65;&#x74;</a>

 

indent[缩进]

indent[缩进]

在每行缩进字符串,默认是4个字符。
作为可选参数,你可以指定缩进字符数。
作为第二个可选参数,你可以指定缩进用什么字符代替。

特别提示:使用缩进时如果是在HTML中,则需要使用& n b s p;(空格)来代替缩进,否则没有效果。

test.php:
$smarty = new Smarty;
$smarty->assign('articleTitle',
               'NJ judge to rule on nude beach.
Sun or rain expected today, dark tonight.
Statistics show that teen pregnancy drops off significantly after 25.'
               );
$smarty->display('test.html');

test.html:
{$articleTitle}
{$articleTitle|indent}
{$articleTitle|indent:10}
{$articleTitle|indent:1:"\t"}

输出:
NJ judge to rule on nude beach.
Sun or rain expected today, dark tonight.
Statistics show that teen pregnancy drops off significantly after 25.

    NJ judge to rule on nude beach.
    Sun or rain expected today, dark tonight.
    Statistics show that teen pregnancy drops off significantly after 25.

          NJ judge to rule on nude beach.
          Sun or rain expected today, dark tonight.
          Statistics show that teen pregnancy drops off significantly after 25.

    NJ judge to rule on nude beach.
    Sun or rain expected today, dark tonight.
    Statistics show that teen pregnancy drops off significantly after 25.

lower [小写]

lower [小写] 

test.php:
$smarty = new Smarty;
$smarty->assign('articleTitle', 'Two Convicts Evade Noose, Jury Hung.');
$smarty->display('test.html');

test.html:
{$articleTitle}
{$articleTitle|lower}

输出:
Two Convicts Evade Noose, Jury Hung.
two convicts evade noose, jury hung.

nl2br[换行符替换成<br /> ]

nl2br[换行符替换成<br /> ]

test.php:
$smarty = new Smarty;
$smarty->assign('articleTitle', "Sun or rain expected\ntoday, dark tonight");
$smarty->display('test.html');

test.html:
{$articleTitle|nl2br}

输出:
Sun or rain expected<br />today, dark tonight
 

 

转载于:https://www.cnblogs.com/yuanscn/p/11243164.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值