php smarty 语法,5. Smarty基本语法

注释

{* 注释内容 *}

输出

比如,现在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内置函数

922ace87033c

使用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}```

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值