如何将对象分配给 smarty 模板?

发布于 2024-08-24 17:17:24 字数 686 浏览 3 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

贪恋 2024-08-31 17:17:24

您应该能够从 Smarty 模板访问对象的任何公共属性。例如:

$o2= new stdclass;
$o2->myvar= 'abc';
$smarty->assign('o2', $o2);

### later on, in a Smarty template file ###

{$o2->myvar}  ### This will output the string 'abc'

如果您打算在将对象分配给 Smarty 模板后更新对象,则还可以使用 assign_by_ref

class User2 {
  public $title;
  public function changeTitle($newTitle){
    $this->title = $newTitle; 
  }
}
$user2= new User2();
$smarty->assign_by_ref('user2', $user2);
$user2->changeTitle('title #2');

并且在模板文件中

{$user2->title}  ## Outputs the string 'title #2'

You should be able to access any public properties of an object from a Smarty template. For instance:

$o2= new stdclass;
$o2->myvar= 'abc';
$smarty->assign('o2', $o2);

### later on, in a Smarty template file ###

{$o2->myvar}  ### This will output the string 'abc'

You can also use assign_by_ref if you plan on updating the object after assigning it to the Smarty template:

class User2 {
  public $title;
  public function changeTitle($newTitle){
    $this->title = $newTitle; 
  }
}
$user2= new User2();
$smarty->assign_by_ref('user2', $user2);
$user2->changeTitle('title #2');

And in the template file

{$user2->title}  ## Outputs the string 'title #2'
凉风有信 2024-08-31 17:17:24
$smarty->assign('user', $user);

在模板中

{$user->title}
$smarty->assign('user', $user);

in template

{$user->title}
只有一腔孤勇 2024-08-31 17:17:24

这对我有用。

$smarty->register_object('user', $user);

// Inside the template. note the lack of a $ sign
{user->title}

无论我是否有 $ 符号,这个都不起作用

$smarty->assign('user', $user);

我希望有人能告诉我原因。

This one works for me.

$smarty->register_object('user', $user);

// Inside the template. note the lack of a $ sign
{user->title}

This one doesn't work regardless whether i have the $ sign

$smarty->assign('user', $user);

I hope someone can tell me why.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文