如何将对象分配给 smarty 模板?
我在 PHP 中创建了一个模型对象
class User {
public $title;
public function changeTitle($newTitle){
$this->title = $newTitle;
}
}
如何通过分配对象来公开 smarty 中的 User 对象的属性?
我知道我可以做到这一点
$smarty->assign('title', $user->title);
,但我的对象有 20 多个属性。
请指教。
编辑1
以下内容对我不起作用。
$smarty->assign('user', $user);
或者
$smarty->register_object('user', $user);
然后我尝试 {$user->title}
什么也没出来。
编辑2
我目前只是尝试在 smarty 模板中输出对象的公共属性。抱歉,如果我让任何人与该功能混淆了。
谢谢。
i created a model object in PHP
class User {
public $title;
public function changeTitle($newTitle){
$this->title = $newTitle;
}
}
How do i expose the property of a User object in smarty just by assigning the object?
I know i can do this
$smarty->assign('title', $user->title);
but my object has something like over 20 plus properties.
Please advise.
EDIT 1
the following didn't work for me.
$smarty->assign('user', $user);
OR
$smarty->register_object('user', $user);
then i try to {$user->title}
nothing came out.
EDIT 2
I am only currently trying to output the public property of the object in the smarty template. Sorry if i confused any one with the function.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该能够从 Smarty 模板访问对象的任何公共属性。例如:
如果您打算在将对象分配给 Smarty 模板后更新对象,则还可以使用
assign_by_ref
:并且在模板文件中
You should be able to access any public properties of an object from a Smarty template. For instance:
You can also use
assign_by_ref
if you plan on updating the object after assigning it to the Smarty template:And in the template file
在模板中
in template
这对我有用。
无论我是否有 $ 符号,这个都不起作用
我希望有人能告诉我原因。
This one works for me.
This one doesn't work regardless whether i have the $ sign
I hope someone can tell me why.