当前位置: 首页 > 未分类>阅读正文

smarty在模板中定义变量

2022.7.23 朱丰华 995 次 留下评论 934字

在模板中可以定义变量和常量

调用自定义常量:

{$smarty.const.你定义的常量名}

定义变量:(只能定义普通键值对,无法定义复杂数据类型)

<?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));
?>

其他调用:

 {$smarty.get.page} {* PHP方式:$_GET[“page”] *}  

{$smarty.post.page} {* PHP方式:$_POST[“page”] *}  

{$smarty.cookies.username} {* PHP方式:$_COOKIE[“username”] *}  

{$smarty.session.id} {* PHP方式:$_SESSION[“id”] *}  

{$smarty.server.SERVER_NAME} {* PHP方式:$_SERVER[“SERVER_NAME”] *}  

{$smarty.env.PATH} {* PHP方式:$_ENV[“PATH”]*}  

{$smarty.request.username} {* PHP方式:$_REQUEST[“username”] *}  

 {$smarty.const._MY_CONST_VAL} {* 输出PHP脚本中自定义的常量 *}   

{$smarty.const._MY_CONST_VAL} {* 输出PHP脚本中自定义的常量 *}  

{$smarty.const.__FILE__} {* 通过保留变量数组直接输出系统常量 *}

……

本篇完,还有疑问?留下评论吧

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注