php中%3ctr标签,PHP 相关文章 —— ECMALL 标签使用整理

[《PHP设计模式大全》系列技术文章整理收藏]

在ECMall模板中,用"{"开头,以"}"结尾就构成一个标签单元,"{"紧接着的单词就是标签名。在标签单元中单词前含"$"(美元符)的为变量名。

资源引用

res标签

功能:返回当前模板当前风格目录的url路径

实例:{res file=css/ecmall.css}这个标签在模板编译后将变成http://商城域名/themes/default/styles/default,注意末尾没有"/",返回结果会随后台设置的主题变化

lib标签

功能:返回javascript库的url路径

实例:{lib file=ecmall.js}这个标签在模板编译后将变成http://商城域名/includes/libraries/javascript,注意末尾没有"/",返回结果不会随后台设置的主题变化

url标签

功能:url解析器,可根据后台伪静态状态返回相应url等。

说明:如果一个链接的目标页面需要伪静态功能,请使用该url标签,只有当后台开启伪静态并在.htaccess文件为目标页面设置了伪静态规则时url标签才能解析为静态url地址。

实例:{url app=goods&id=$goods_id}解析后如果伪静态成功则返回"goods/19"

include标签:include 标签用于在当前模板中包含其它模板。当前模板中的变量在被包含的模板中可用。必须指定 file 属性,该属性指明模板资源的位置。实例:

1

{

include

file=

"header.html"

}

2

{* body of template goes here *}

3

{

include

file=

"footer.html"

}

模板变量

1. 模板保留变量

模板预置的一些系统变量,包括

$smarty.now 当前时刻对应的格林尼治时间戳,可以用{$smarty.now|date}显示当前日期时间,关于date变量调节器请看下文讲解。

$smarty.get $smarty.post $smarty.cookie $smarty.env $smarty.server $smarty.request $smarty.session同php的$_GET、$_POST、$_COOKIE、$_ENV、$_SEVER、$_REQUEST、$_SESSION变量。非程序人员如果需要了解请参考php相关手册了解。

2. 自定义变量

从php赋值变量。例如在调用该模板的app程序文件中进行赋值

1

//在app/default.app.php文件的index方法中$this->display前添加赋值语句

2

$this

->assign(

'name'

,

'Tom'

);

//普通变量

3

$this

->assign(

'user'

,

array

(

4

'name'

=>

'Tom'

,

5

'age'

=>

'28'

6

));

//数组变量

7

$this

->display(

'index.html'

);

在themes/mall/default/index.html中显示变量

1

Hello,{

$name

},your age are {

$user

.age}!

在模板中赋值变量。assign标签,例在themes/mall/default/index.html中赋值变量

1

{assign

var

=

"name"

value=

"Tom"

}

2

Hello,{

$firstname

}!

3.模板上使用语言项

说明:为了满足多语言需求,ECMall采用了语言包机制,除挂件外,在模板、js文件中均使用语言项代替直接显示语言文字。

语言文件:语言文件位于商城根目录下的languages目录下,为当前语言建了一个目录,如果您用的是sc-gbk版本,则会有sc-gbk目录,进去之后就能看到属于前台控制器的全部语言文件了。除common.lang.php属于所有app外,每一个语言文件都只属于一个app(ECMall中称为控制器)。语言文件属于某个控制器而不属于某个模板,同一个模板如果被不同控制器调用将使用不同语言文件进行语言解析。

在默认控制器对应的语言文件default.lang.php添加语言项"test":

1

2

return

array

(

3

'hot_search'

=>

'热门搜索'

,

4

... ...

5

'best_recommended'

=>

'精品推荐'

,

6

'test'

=>

'测试语言项'

7

);

8

?>

在index.html模板中显示语言项"test"

1

{

$lang

.test}

模板中还支持数组形式语言项,在默认控制器对应的语言文件default.lang.php添加语言项"test_array"

01

02

return

array

(

03

'hot_search'

=>

'热门搜索'

,

04

... ...

05

'best_recommended'

=>

'精品推荐'

,

06

'test_array'

=>

array

(

07

'key1'

=>

'测试数组语言项'

,

08

'key2'

=>

'太好了'

09

),

10

);

11

?>

在index.html模板中显示语言项"test"

1

{

$lang

.test_array.key1}

4. 变量调节器

* escape

功能:提供各种编码功能。

参数:可选参数html、url、quotes、input、editor,缺省为html html:分别替换变量中的如下字符& < > "为其html实体代码,用于按原样输出html源代码。

url:如果该变量用于储存url地址,需要进行url编码

quotes:在单双引号字符前添加反斜杠

input:给输入框赋值时使用

editor:当显示通过文本编辑器录入的内容,需要用此参数

例如php赋值:

1

$this

->assign(

'goods_name'

,

"L'oreal/欧莱雅"

.  '

"'   . "

清润全日保湿乳霜

"  .  '"

'   .

"50ml

);

2

$this

->display(

'index.tpl'

);

模板

1

{

$goods_name

}

2

{

$goods_name

|escape}

3

{

$goods_name

|escape:

"html"

}

4

{

$goods_name

|escape:

"url"

}

5

{

$goods_name

|escape:

"quotes"

}

6

{

$goods_name

|escape:

"input"

}

7

{

$goods_name

|escape:

"editor"

}

输出结果为

1

L'oreal/欧莱雅

"清润全日保湿乳霜"

50ml

2

L'oreal/欧莱雅

"清润全日保湿乳霜"

50ml

3

L'oreal/欧莱雅

"清润全日保湿乳霜"

50ml

4

L%27oreal%2F%E6%AC%A7%E8%8E%B1%E9%9B%85%22%E6%B8%85%E6%B6%A6%E5%85%A8%E6%97%A5%E4%BF

5

%9D%E6%B9%BF%E4%B9%B3%E9%9C%9C%2250ml%3Cbr+%2F%3E%3Cscript%3E

6

L\'oreal/欧莱雅\"清润全日保湿乳霜\"50ml

7

L'oreal/欧莱雅\"清润全日保湿乳霜\"50ml

8

L'oreal/欧莱雅

"清润全日保湿乳霜"

50ml

* nl2br

功能:将换行符替换成

1

{

$var

|

nl2br

}

* default

功能:为变量设置一个默认值,当变量为空或者未分配的时候,将由默认值替代输出

1

{

$var

|

default

:

"no title"

}

* truncate

功能:字符串截取。从字符串开始处截取某长度的字符。默认会在末尾追加省略号。

1

{

$content

|truncate:20}

* strip_tags

功能:去除<和>标签,包括在<和>之间的任何内容。

1

{assign

var

=

"content"

value=

"文章内容"

}

2

{

$content

|

strip_tags

}

* price

功能:格式化价格。

1

{assign

var

=

"goods_price"

value=

"123456"

}

2

{

$goods_price

|price}

输出结果为:¥123,456.00[《PHP框架Yii学习》系列技术文章整理收藏]

* date

功能:格式化本地时间和日期。

格式:{$var|date:format}

说明:变量$var必须是格林尼治标准时间,php中gmtime()和模板中$smarty.now得到的都是格林尼治标准时间,参数format可为simple、complete或自定义日期格式,缺省为simple。

1

{

$smarty

.now|

date

}

2

{

$smarty

.now|

date

:complete}

3

{

$smarty

.now|

date

:Y-m-d H:i}

输出结果为:

1

2010-12-01

2

2010-12-01 22:49:46

3

2010-12-01 22:49

* modifier

功能:调用php自定义函数。

格式:{$var|modifier:user_func}

流程控制标签

1. 条件判断(if,elseif,else)

模板中的 if 语句和 php 中的 if 语句一样灵活易用,并增加了几个特性以适宜模板引擎. if 必须于 /if 成对出现. 可以使用 else 和 elseif 子句. 可以使用以下条件修饰词:eq、ne/neq、gt、lt、lte/le、gte/ge、mod、not、==、!=、>、<、<=、>=、%、!使用这些修饰词时必须和变量或常量用空格格开.

多个条件之间用 and、or、&&、|| 连接,实现简单的逻辑运算

01

{

if

$name

eq

"Fred"

}

02

Welcome Sir.

03

{

elseif

$name

eq

"Wilma"

}

04

Welcome Ma'am.

05

{

else

}

06

Welcome, whatever you are.

07

{/

if

}

08

{* 一个

"或"

逻辑的例子 *}

09

{

if

$name

eq

"Fred"

or

$name

eq

"Wilma"

}

10

...

11

{/

if

}

12

{* 与上例等效 *}

13

{

if

$name

==

"Fred"

||

$name

==

"Wilma"

}

14

...

15

{/

if

}

16

{* 下面的语法无效,条件修饰符必须由空格跟其他元素分开 *}

17

{

if

$name

==

"Fred"

||

$name

==

"Wilma"

}

18

...

19

{/

if

}

20

{* 允许使用括号 *}

21

{

if

(

$amount

< 0

or

$amount

> 1000 )

and

$volume

>= #minVolAmt#}

22

...

23

{/

if

}

2. 数组遍历(foreach,foreachelse)

foreach 用于处理简单数组(数组中的元素的类型一致)。foreach 必须和 /foreach 成对使用,且必须指定 from 和 item 属性。foreach 可以嵌套,但必须保证嵌套中的 foreach 名称唯一。foreachelse 语句在 from 变量没有值的时候被执行。

from 属性:指定被循环的数组,数组长度决定了循环的次数。item属性:单个循环项目的变量名,在循环内部使用。name 属性为可选属性,可以任意指定(字母、数字和下划线的组合)。

name 属性如果指定,foreach循环体内会自动生成如下变量

$smarty.foreach.foreach_name.index表示本次循环索引,从0开始递增的整数

$smarty.foreach.foreach_name.iteration表示本次的循环次数,从1开始递增的整数

$smarty.foreach.foreach_name.first表示是否是第一次循环

$smarty.foreach.foreach_name.last表示是否是最后一次循环

$smarty.foreach.foreach_name.show表示是否有数据

$smarty.foreach.foreach_name.total表示循环总次数,也可在循环体外使用

1

{* 该例将输出数组

$custid

中的所有元素的值 *}

2

{

foreach

from=

$custid

item=curr_id}

3

id: {

$curr_id

}

4

{/

foreach

}

输出结果为:

1

id: 1000

2

id: 1001

3

id: 1002

1

/* 在对应的控制器中赋值 */

2

$this

->assign(

"contacts"

,

array

(

3

array

(

"phone"

=>

"1"

,

"fax"

=>

"2"

,

"cell"

=>

"3"

),

4

array

(

"phone"

=>

"555-4444"

,

"fax"

=>

"555-3333"

,

"cell"

=>

"760-1234"

)

5

));

模板代码:

1

{* 键就是数组的下标,请参看关于数组的解释 *}

2

{

foreach

name=outer item=contact from=

$contacts

}

3

{

foreach

key=key item=item from=

$contact

}

4

{

$key

}: {

$item

}

5

{/

foreach

}

6

{/

foreach

}

输出结果为:

1

phone: 1

2

fax: 2

3

cell: 3

4

phone: 555-4444

5

fax: 555-3333

6

cell: 760-1234

1

{* 最后一行不显示
标签 *}

2

{

foreach

name=outer item=contact from=

$contacts

name=my_name}

3

{

foreach

key=key item=item from=

$contact

}

4

{

$key

}: {

$item

}{

if

!smarty.

foreach

.my_name.last}
{/

if

}

5

{/

foreach

}

6

{/

foreach

}

显示标签

* cycle

cycle 用于轮转使用一组值。该特性使得在表格中交替输出颜色或轮转使用数组中的值变得很容易。

格式:{cycle values="val1,val2,val3..."}

1

{

foreach

from=

$data_list

item=data}

2

"{cycle values="

#eeeeee,#d0d0d0

"}"

>

3

{

$data

}

4

5

{/

foreach

}

输出结果为:

1

"#eeeeee"

>

2

1

3

4

"#d0d0d0"

>

5

2

6

7

"#eeeeee"

>

8

3

9

* html_options

自定义函数 html_options 根据给定的数据创建选项组. 该函数可以指定哪些元素被选定. 要么必须指定 values 和 ouput 属性,要么指定 options 替代。

1

$this

->assign(

'cust_ids'

,

array

(1000,1001,1002,1003));

2

$this

->assign(

'cust_names'

,

array

(

'Joe Schmoe'

,

'Jack Smith'

,

'Jane Johnson'

,

'Carlie Brown'

));

3

$this

->assign(

'customer_id'

, 1001);

模板代码:

1

2

{html_options values=

$cust_ids

selected=

$customer_id

output=

$cust_names

}

3

1

$this

->assign(

'cust_options'

,

array

(

2

1001 =>

'Joe Schmoe'

,

3

1002 =>

'Jack Smith'

,

4

1003 =>

'Jane Johnson'

,

5

1004 =>

'Charlie Brown'

));

6

$this

->assign(

'customer_id'

, 1001);

模板代码:

1

2

{html_options options=

$cust_options

selected=

$customer_id

}

3

实例1和实例2输出结果均为:

1

2

"1000"

>Joe Schmoe

3

"1001"

selected>Jack Smith

4

"1002"

>Jane Johnson

5

"1003"

>Carlie Brown

6

* html_radios

自定义函数 html_radios 根据给定的数据创建单选按钮组。该函数可以指定哪个元素被选定。要么必须指定 values 和 ouput 属性,要么指定 options 替代。与html_options不同的是html_radios有一个checked属性。

* html_checkbox

自定义函数 html_checkboxes 根据给定的数据创建复选按钮组。该函数可以指定哪些元素被选定。 要么必须指定 values 和 ouput 属性,要么指定 options 替代.。与html_options不同的是html_checkbox有一个checked属性。

* sprintf

说明: 对变量进行格式化。

格式{sprintf lang=my_lang var1=my_var1 var2=my_var2 ...}

在语言文件添加语言项:

1

return array(

2

... ...

3

'query_info' => '页面执行 %0.3f 秒, 查询 %d 次,在线 %d 人'

4

);

模板中显示:

1

{sprintf lang=query_info var1=query_time var2=query_count var3=query_user_count}

输出结果为:

view sourceprint?

1

页面执行 0.160 秒, 查询 3 次,在线 10 人

[《PHP框架ThinkPHP学习》系列技术文章整理收藏]

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值