php smart模板,SmartTemplate(适用于企业级PHP开发的模板引擎)-PHP教程,PHP应用

[color=darkblue:b920254608][size=24:b920254608]基本方法

smarttemplate::assign()[/size:b920254608][/color:b920254608]

void [b:b920254608]assign [/b:b920254608]( string placeholder, mixed content )

or

void [b:b920254608]assign [/b:b920254608]( array content )

给模板占位符(placeholder)或者列表(content)赋值. 可以使用散列数组或者标量

[b:b920254608]例子1:标量赋值[/b:b920254608]

[code:1:b920254608]<?php

$template  =  new smarttemplate(template.html);

$text  =  sample text;

$template->assign( title, $text );

$template->output();

?>[/code:1:b920254608]

模板(template.html):

[code:1:b920254608] {title} [/code:1:b920254608]

输出:

[code:1:b920254608] sample text [/code:1:b920254608]

[b:b920254608]例子2: 多个标量赋值[/b:b920254608]

[code:1:b920254608]<?php

$template  =  new smarttemplate(user.html);

$template->assign( name,  john doe );

$template->assign( group, admin    );

$template->assign( age,   42       );

$template->output();

?>[/code:1:b920254608]

模板(user.html):

[code:1:b920254608]name: {name}

group: {group}

age:   {age}

[/code:1:b920254608]

输出:

[code:1:b920254608]name:  john doe

group: admin

age:   42[/code:1:b920254608]

[b:b920254608]例子3: 使用数组给多个标量赋值[/b:b920254608]

[code:1:b920254608]<?php

$user  =  array(

name  => john doe,

group => admin,

age   => 42,

);

$template  =  new smarttemplate(user.html);

$template->assign( $user );

$template->output();

?>[/code:1:b920254608]

模板(user.html):

[code:1:b920254608]name:  {name}

group: {group}

age:   {age}[/code:1:b920254608]

输出:

[code:1:b920254608]name:  john doe

group: admin

age:   42[/code:1:b920254608]

[b:b920254608]例子4: 命名空间[/b:b920254608]

[code:1:b920254608]<?php

$admin  =  array(

name  => john doe,

age   => 42,

);

$guest  =  array(

name  => roger rabbit,

age   => 16,

);

$template  =  new smarttemplate(users.html);

$template->assign( admin, $admin );

$template->assign( guest, $guest );

$template->output();

?>[/code:1:b920254608]

模板(user.html): 占位符(placeholder)对应数组,“.”对应数组“[]”

[code:1:b920254608]admin name: {admin.name}

admin age:  {admin.age}

guest name: {guest.name}

guest age:  {guest.age}[/code:1:b920254608]

输出:

[code:1:b920254608]admin name: john doe

admin age:  42

guest name: roger rabbit

guest age:  16[/code:1:b920254608]

[b:b920254608]例子5: 使用数组命名空间[/b:b920254608]

[code:1:b920254608]<?php

$users  =  array(

admin => array(

name  => john doe,

age   => 42,

),

guest => array(

name  => roger rabbit,

age   => 16,

),

);

$template  =  new smarttemplate(users.html);

$template->assign( $users );

$template->output();

?>[/code:1:b920254608]

模板(user.html): 占位符(placeholder)对应数组,“.”对应数组“[]”

[code:1:b920254608]admin name: {admin.name}

admin age:  {admin.age}

guest name: {guest.name}

guest age:  {guest.age}[/code:1:b920254608]

输出:

[code:1:b920254608]admin name: john doe

admin age:  42

guest name: roger rabbit

guest age:  16[/code:1:b920254608]

[b:b920254608]例子6: 命名空间, 3个部分[/b:b920254608]

[code:1:b920254608]<?php

$template  =  new smarttemplate(template.html);

$content[world][europe][germany]  =  de;

$template->assign( top_level_domain, $content );

$template->output();

?>[/code:1:b920254608]

模板(template.html): 占位符(placeholder)对应数组,“.”对应数组“[]”

[code:1:b920254608] german tld: {top_level_domain.world.europe.germany} [/code:1:b920254608]

输出:

[code:1:b920254608] german tld: de [/code:1:b920254608]

[b:b920254608]例子7: 列表赋值[/b:b920254608]

[code:1:b920254608]<?php

$links  =  array(

array(

title => php,

url   => http://www.php.net/,

),

array(

title => apache,

url   => http://www.php.net/,

),

array(

title => mysql,

url   => http://www.mysql.com/,

),

);

$template  =  new smarttemplate(links.html);

$template->assign( links, $links );

$template->output();

?>[/code:1:b920254608]

模板(links.html): 结构名称lnks对应数组

[code:1:b920254608]

 sample links 

 {title} 

[/code:1:b920254608]

输出:

[code:1:b920254608]

 sample links 

 php 

 apache 

 mysql 

[/code:1:b920254608]

[b:b920254608]example 8: 使用数组于多个命名空间[/b:b920254608]

[code:1:b920254608]<?php

$title  =  sample links;  //  page title

$target =  _blank;        //  the same target for all links

$links  =  array(

array(

title => php,

url   => http://www.php.net/,

),

array(

title => apache,

url   => http://www.php.net/,

),

array(

title => mysql,

url   => http://www.mysql.com/,

),

);

$template  =  new smarttemplate(links.html);

$template->assign( title, $title );

$template->assign( target, $target );

$template->assign( links,  $links  );

$template->output();

?>[/code:1:b920254608]

注意:

title 与 links..title 使用不同的命名空间!

target 不是 links 数组的成员. 如果使用在 begin..end 块之内, 他必须被引用为 {parent.target} 或者 {top.target}.

其他可能的用法:

{top.title}, {parent.parent.page_id}, {top.users.admin}, 等等..

模板(links.html):

[code:1:b920254608]

 {title} 

 {title} 

[/code:1:b920254608]

输出:

[code:1:b920254608]

 sample links 

 php 

 apache 

 mysql 

[/code:1:b920254608]

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值