[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]