试图在我的布局中显示变量
我有一个简单的问题
我的代码哪里错了?
索引控制器和索引操作中
在我放置的
$this->view->username="user1";
,当我在我的布局中尝试使用时,
echo $this->username;
出现以下错误或空值
注意:尝试获取 D:\Zend\Apache2\htdocs\test\application\layouts\ 中非对象的属性layout.phtml 第 115 行
谢谢
i have a simple question
where is my code wrong ?
in index controller and index action
i put
$this->view->username="user1";
and when i try in my layout i use
echo $this->username;
i got fllowing error or null value
Notice: Trying to get property of non-object in D:\Zend\Apache2\htdocs\test\application\layouts\layout.phtml on line 115
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过
echo $this->view->username
?Have you tried
echo $this->view->username
?您尝试回显变量的布局不是您的 视图。
本质上来说,不可能。让我解释一下:您的布局是一个全局布局。 IndexController 的 indexAction() 特有的东西在逻辑上在全局布局中是(/应该是)不可用的,因为如果你最终调用 FooController 的 barAction( )。
您的视图位于views/scripts/index/index.phtml - 您可以在其中使用
username; ?>
我还没有在布局中使用过变量,但这就是我从 文档:如果您希望布局显示变量,请尝试使用
$this->_helper->layout->username = "user1 "
在你的操作中,这应该让你在布局中使用echo $this->layout()->username
。但要小心,如果您确实使用布局变量,则还必须在每个操作中设置它们。如果您只想让 indexAction() 显示用户名,您应该尝试将 echo 放入视图中。
Your layout, where you are trying to echo your variable, is not your view.
Essentially, it can't be. Let me explain: Your layout is a global, well, layout. Something that's specific for IndexController's indexAction() is (/ should be) logically unavailable in the global layout, since your layout will not have this variable if you end up calling, say, FooController's barAction().
Your view would be at views/scripts/index/index.phtml - that's where you can use
<?php echo $this->username; ?>
I haven't worked with variables in layouts yet, but this is what I'm gleaning from the documentation: If you want your layout to show a variable, try using
$this->_helper->layout->username = "user1"
in your action, that should let you useecho $this->layout()->username
in the layout.Be careful, though, if you do use layout variables, you also have to set them in each action. If you just want your indexAction() to show the username, you should try putting your echo into your view.