注释

{* 注释内容 *}

输出

比如,现在php文件中有一个数组,并且用assign方法赋值给了一个模板文件test.tpl

$arr=array('title'=>'smarty的学习', 'author'=>'小明')

$smarty->assign('arr', $arr);```

然后我们在模板中输出:

{$arr.title} // 方法一

{$arr['title']} // 方法二```

变量调节器

变量调节器,写在变量后面,用竖线|隔开。

1 - 首字母大写 capitalize

示例:{$articleTitle|capitalize}

php文件:

$smarty->assign('articletitle', 'i ate an apple');

$smarty->display('test.tpl');```

tpl文件:```{$articletitle|capitalize}```

输出:```I Ate An Apple```

**2 - 字符串连接 cat**

示例:```{$articleTitle|cat:"yesterday."}```

php文件:

$smarty->assign('articletitle', 'i ate an apple ');

$smarty->assign('yesterday', 'yesterday');

$smarty->assign('point', '.');

$smarty->display('test.tpl');tpl文件:{$articletitle|cat:"yesterday.":"point"}```

输出:i ate an apple yesterday.

3 - 日期格式化 date_format

示例:{$yesterday|date_format}

{$yesterday|date_format:" :"}

php文件:

$smarty->assign('time', time()); //time()为php内置函数,用于获取unix格式的时间

$smarty->display('test.tpl');```

tpl文件:```{$time|date_format}```

输出:`Feb 07, 2017`

or

tpl文件:```{$time|date_format:"%B %e, %Y %H:%M:%S"}```

输出:`Feb 07, 2017 08:05:08 //注意:时间不对是因为没有设置时区,当前时间为格林威治时间`

**4 - 为未赋值或为空的变量指定默认值default**

示例:```{$articleTitle|default:"no title"}```

php文件:

$smarty->assign('articletitle', ' ');

$smarty->display('test.tpl');tpl文件:{$articletitle|default:"no title"}```

输出:no title

5 - 转码 escape

用于html转码,url转码,在没有转码的变量上转换单引号,十六进制转码,十六进制美化,或者js转码。more为html转码。

php文件:

$smarty->assign('url', 'www.chuying-he.com');

$smarty->display('test.tpl');```

tpl文件:```{$url|escape:"url"} //escape:后面跟着的是转码方式```

输出:`www.chuying-he.com`

**6 - 小写 lower 大写 upper**

将变量字符串小 / 大写

示例:

```{$articletitle|lower}```

```{$articletitle|upper}```

php文件:

$smarty->assign('articletitle', 'Happy New Year');

$smarty->display('test.tpl');```

tpl文件:

{$articletitle|lower}

{$articletitle|upper}```

输出:

happy new year

HAPPY NEW YEAR

**7 - 将所有换行符替换成
nl2br**

将所有换行符替换成
,与PHP中的nl2br()函数一样

示例:```{$articletitle|nl2br}```

php文件:

$smarty->assign('articletitle', 'Happy

New

Year');

$smarty->display('test.tpl');```

tpl文件:

{$articletitle|nl2br}```

输出:

Happy

New

Year

**8 - 其他函数**

可参见手册,但原则上应该通过php或者CSS直接处理完毕,在赋值到Smarty中,尽量少用Smarty函数

##Smarty的条件判断语句

Smarty中,```eq```表示```==```,```neq```表示```!=```,```gt```表示```>```,```lt```表示```

{if isset($name) && $name == 'Blog'}

{* do something }

{elseif $name == $foo}

{ do something *}

{/if}

##Smarty的循环语句

**第一种循环 section**

```{section}```, ```{sectionelse}``` 是Smarty的内置函数,详细文档点击[这里](http://www.smarty.net/docs/zh_CN/language.function.section.tpl)。

php文件:

$articlelist=array(

array(

"title" => "My first article",

"author" => "Alex",

"content" => "I'm a web developer."

),

array(

"title" => "Work Note",

"author" => "John Doe",

"content" => "Where is my home?"

)

)

$smarty->assign("articlelist", $articlelist);

$smarty->display("test.tpl");

tpl文件:

{section name=article loop=$articlelist}

{$articlelist[article].title}

{$articlelist[article].author}

{$articlelist[article].content}

{/section}

输出:

My first article Alex I'm a web developer.

Work Note John Doe Where is my home?```

第二种循环 foreach

{foreach} {foreachelse}用于循环数组,语法上比{section}更加简单清晰,并且可以使用非数字下标。

php文件:同上

tpl文件:

{foreach item=article from=$articlelist}

{$article.title}

{$article.author}

{$article.content}

{foreachelse}

NOTHING IN ARRAY

{/foreach}

输出:同上

注意:{foreachelse}后面的内容会在当前数组中没有内容时显示

Smarty的文件引用

在PHP中,有两种引入方式:include和require。在Smarty中只有include这一种。把别的模板引入进来。

语法:{include file='page_header.tpl' sitename="sugar"}

解释:file为要导入的模板名,sitename中的值会传递到page_header.tpl里面去,要注意的是,sitename这个值 能且只能 在page_header.tpl里面使用。假如page_header.tpl中有同名变量,该变量值会被sitename的值替代

Smarty类和对象的赋值与使用

assign除了能赋值一个变量 or 数组,还能把一个类的对象以变量的形式赋值到smarty的模板当中去。

php文件:

class My_Object{

function methl($params){

return $params[0].' is already '.$params[1];

}

}

$myobj=new My_Object;

$smarty->assign('myobj',$myobj); // 将对象$myobj传入test.tpl模板的名为'myobj'的对象中

$smarty->display('test.tpl');

tpl文件:

{$myobj->meth1(array('cake','done'))} // 这里的$myobj是从php文件中传入的对象,该对象可使用类中的methl方法

输出:

cake is already done

Smarty函数的使用

尽管Smarty中有大量的变量调节器,也还是有用户希望使用其他方法,比如:

1. 使用php内置函数

使用php内置函数

php文件:

$smarty->assign('time', time());

$smarty->display('test.tpl');

tpl文件:

// date()为php的内置函数

// 第一次尝试:{$time|date("Y-m-d")};

// ---- 注意:该写法被解释成:date这个函数有两个参数,第一个参数为$time,第二个参数为"Y-m-d" ----

// 而php的date方法应该是第一个参数为日期格式,第二个参数为unix时间戳,所以我们要变换顺序如下:

{"Y-m-d"|date($time)};

输出:

2017-02-08```

**2. 使用自定义函数,并使用 ```registerPlugin``` 注册到smarty模板里面使用**

![使用 ```registerPlugin``` ](http://upload-images.jianshu.io/upload_images/2662224-27e613ab79acb6fe.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

**注意**:```registerPlugin```的第一个参数除了function,还有modifier,block等,下面在smarty的插件制作中会详细讲到

php文件:

function test($params){

$p1=$params['p1'];

$p2=$params['p2'];

return 'the first para is '.p1.', the second para is '.p2;

}

// 第一个参数表示被注册程序的类型(plugin/modifier/block),第二个参数表示注册到smarty中后函数的名字,第三个参数表示要传入的参数名。

$smarty->registerPlugin('function', 'f_test', 'test');

$smarty->display('test.tpl');

tpl文件:

{f_test p1='abc' p2='edf'}; // {函数名 第一个参数 第二个参数} 即,将两个参数传送到函数f_test中

输出:

```the first para is abc, the second para is edf```

**3. 自定义插件**

在第一和第二种方法中,我们发现,在最终的模板调用函数的时候,方法不同,格式也不同,这是非常不友好的书写方式,为了解决该问题,我们需要一样新的东西:smarty插件。

*-------- 什么是插件 --------*

插件是遵循原来的系统主体接口的编写规范,编写出来的程序,它能够调用主系统的数据和函数库。他的好处是可执行力强,可重复利用率高,本身的增删改不影响主系统的结构。

Smarty中也提供了这种插件机制,能够在不改变smarty主程序的前提下,增加更多丰富的功能。 Smarty的本质其实是function函数,它的书写方式完全遵照php的函数语法。

*-------- Smarty插件类型 --------*

- functions 函数插件

- modifiers 修饰插件

- block functions 区块函数插件

*-------- 插件制作方法 --------*

1. 使用```registerPlugin```方法注册写好的自定义函数(即方法2)

2. 将写好的插件放入Smarty解压目录的lib下的plugins目录中

3. php内置函数,可以作为修饰插件(变量调节器插件)的形式在模板中使用

*-------- 在```wamp/www/smarty/smarty/plugins```目录中,可以看到许多php文件 --------*

- 以```function```开头类似```function.counter.php```为函数插件

- 以```modifier```开头类似```modifier.capitalize.php```为修饰插件

- 以```block```开头类似```block.textformat.php```为区块函数插件

注意:命名规则为```插件类型.插件名.php```

*-------- functions 函数插件举例 --------*

// 名为 function.test1.php 的函数插件

// 命名规则: smarty_function_插件名(要和文件中的插件名一样)

function smarty_function_test1($params){

$width = $params['width'];

$height = $param['height'];

$area = $width * $height;

return $area;

}

?>```

// test.php文件

$smarty->display('area.tpl');

// area.tpl 模板文件

//smarty对该语句的处理是:调用test函数,然后将两个参数放在一个数组里面传递到test函数中array('width'=>150, 'height'=>200)

{test1 width=150 height=200}

-------- modifier 函数插件举例 --------

// 名为modifier.test2.php的modifier插件

// 命名规则: smarty_modifier_插件名(要和文件中的插件名一样)

// 该modifier定义两个参数,第一个为unic时间戳,第二个为时间形式

function smarty_modifier_test2($utime, $format){

return date($format, $utime);

}

?>```

// test.php文件

$smarty->assign('time',time());

$smarty->display('datetime.tpl');```

// datetime.tpl 模板文件

{$time|test2:'Y-m-d H:i:s'}```

*-------- block 函数插件举例 --------*

// 名为block.test3.php的block插件

function smarty_block_test3($params, $content){

$replace=$params['replace'];

$maxnum=$params['maxnum'];

if($replace=='true'){

$content=str_replace(',', ',', $content); // 将中文的逗号替换成英文的逗号

$content=str_replace('。','.', $content); // 将中文的句号替换成英文的句号

}

$content=substr($content, 0, $maxnum);

return $content;

}

?>```

// test.php文件

$smarty->assign('str','Hello, my name is HanMeiMei。What is your name?');

$smarty->display('content.tpl');```

// content.tpl 模板文件

{test3 replace='true' maxnum=29}

{$str}

{/test3}```

php smarty 语法,5. Smarty基本语法相关推荐

  1. Smarty中文手册,Smarty教程,Smarty模板的入门教材

    Smarty中文手册,Smarty教程,Smarty模板的入门教材 首先,这份Smarty中文手册的翻译工作是由喜悦国际村村民自发组织的,不代表任何人的意见和观点.对他们的无私奉献精神,我们表示感谢, ...

  2. php smarty变量调节器,Smarty模板变量与调节器实例详解

    本文实例讲述了Smarty模板变量与调节器.分享给大家供大家参考,具体如下: Smarty中assign说明 可能有人在学习smarty的时候已经学习了一些php框架,如tp.laravel.Yii等 ...

  3. PHP Smarty 模板,PHP Smarty模板引擎

    前言:概念上的东西就简单过一下,细节上的东西就不深究了.作下读书笔记,且该知识点是后续学习MVC框架的一部分. 1.什么是模板引擎 1.1.网站页面模板:即每个页面仅是一个板式,包括结构.样式和页面布 ...

  4. smarty mysql demo_PHP Smarty模版简单使用方法

    本文实例讲述了PHP Smarty模版简单使用方法.分享给大家供大家参考,具体如下: Index.php: require('../libs/Smarty.class.php'); $smarty = ...

  5. 爬虫之xpath语法-常用节点选择语法

    爬虫之xpath语法-常用节点选择语法 可以通过通配符来选取未知的html.xml的元素 1.1 选取未知节点的语法 通配符 描述 * 匹配任何元素节点. node() 匹配任何类型的节点. 1.2 ...

  6. python语法错误概述_python语法错误

    广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! 我是python中的新手,当我想在模块中编译代码时,我遇到语法错误:invail ...

  7. 【Groovy】Groovy 代码创建 ( 使用 Java 语法实现 Groovy 类和主函数并运行 | 按照 Groovy 语法改造上述 Java 语法规则代码 )

    文章目录 一.创建 Groovy 代码文件 二.使用 Java 语法实现 Groovy 类和主函数并运行 三.按照 Groovy 语法改造上述 Java 语法规则代码 一.创建 Groovy 代码文件 ...

  8. php smarty安装,php smarty 安装 、配置、使用 及缓存cache的配置使用

    cache 使用: cache配置: $smarty->cache_dir = "/caches/";  //缓存目录 $smarty->caching = true; ...

  9. php smarty 分页类,Smarty分页类Smartypaginate

    不罗嗦了,直接上范例 function.paginate_first.php.function.paginate_last.php.function.paginate_prev.php.functio ...

  10. php yaf smarty,Yaf 结合用户自定义的视图(模板)引擎Smarty(Yaf + Smarty)

    Yaf 结合用户自定义的视图(模板)引擎Smarty(Yaf + Smarty) 来源:互联网 作者:佚名 时间:2015-08-06 07:55 对完成某个任务进行计时可使用progress_tim ...

最新文章

  1. [轉]Webdings字体图案
  2. 【PHP】微信官方代码Log调试输出类,面向对象设计模式!来看看,你会有收益!...
  3. jwt:介绍以及创建token
  4. leetcode 461. 汉明距离(位运算)
  5. 算法基础之搜索和经典排序
  6. AI领域都在用Python即将被淘汰?网友预测未来的编程语言不会是TA
  7. 2022经营新增长第一课
  8. windows下IDEA全面红色,但是能编译,不能智能提示
  9. “腾讯基因”讨论:为什么我常说做to C的人很难去做to B?
  10. 收藏:产品经理和技术经理等的OKR模板大全
  11. win10系统:虚拟桌面的创建、切换、删除快捷键
  12. linux中tac命令详解,详解Linux中输出文件内容的rev与tac命令使用
  13. ubuntu卸载程序
  14. 最近在B站重新看学习视频小结
  15. App三种启动场景:冷启动、热启动、温启动
  16. golang对接支付宝支付
  17. linux如何给手机刷recovery,给安卓手机刷Recovery的方法
  18. 速营社团队分享互联网发展过程那些疯狂的时代
  19. 解决Retrofit和RxJava 抛出异常报错问题
  20. 通讯录管理系统-C++课程设计(试错版)

热门文章

  1. 大型集团法务工作中的电子签章应用场景
  2. SQl盲注原理及其简单演示
  3. ubuntu18使用wine安装TIM和微信
  4. linux中unzip命令无法使用解决方法
  5. 机器人对话常用语模板_小a电话机器人免费咨询
  6. 计算机软件配置项csci
  7. uc如何HTML编辑,电脑端UC浏览器如何对书签进行编辑
  8. 最全,从小白到交互设计大牛的105条设计原则-附PDF资料
  9. 关于在windows上首次编译运行pbrt-3
  10. 少儿计算机基础知识,儿童计算机基本操作