Get Smarty

Donate

Smarty Icon

You may use the Smarty logo according to the trademark notice.

Smarty Template Engine Smarty Template Engine

For sponsorship, advertising, news or other inquiries, contact us at:

Sites Using Smarty

Buy cheap glasses from Cheapglasses123.com and save up to 80%.

Where to buy prescription glasses? EyewearCanada.com is your best choice!

Where to buy discount wedding dresses and cheap smart dresses free shipping - Weddingdresstrend.com

Shop for 2018 World Cup Football Boots Online sale at guthjcc.com

Browse best 2018 world cup soccer cleats and shoes from SoccerAzteca.com

Advertisement

Name

assign() — 赋值

说明

void assign(mixed var);
void assign(string varname,
            mixed var,
            bool nocache);

你可以传递键值对,或联合数组进行赋值到模板。

如设置第三个参数nocache为true,则该变量不进行缓存。 详细参考 变量缓存

Note

在你传递对象到模板时,请记住对象在模板中使用的成员变量和方法均只为了显示。通过对象在模板中进行一些更复杂的应用逻辑是很容易的事情,但这样会成为较差的设计,并且难以管理。请参见Smarty网站上关于最佳实践的主题。

Example 14.6. assign()


<?php
// 传递键值对
$smarty->assign('Name', 'Fred');
$smarty->assign('Address', $address);

// 传递联合数组
$smarty->assign(array('city' => 'Lincoln', 'state' => 'Nebraska'));

// 传递数组
$myArray = array('no' => 10, 'label' => 'Peanuts');
$smarty->assign('foo',$myArray);

// 传递一行数据库的返回记录 (如 adodb)
$sql = 'select id, name, email from contacts where contact ='.$id;
$smarty->assign('contact', $db->getRow($sql));
?>

模板中可以直接使用:


{* 和php一样,变量名不区分大小写 *}
{$Name}
{$Address}
{$city}
{$state}

{$foo.no}, {$foo.label}
{$contact.id}, {$contact.name},{$contact.email}


更多数组的使用方法请参见 {foreach}{section}

参见 assignByRef(), getTemplateVars(), clearAssign(), append(){assign}

Comments
No comments for this page.
Post a Comment
All comments are moderated. Support questions are ignored, use the forums instead.
Author:
Email: (not shown)
What is 18 plus 5? (Are you human?)

Advertisement