trying to show a variable in my layout
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 go开发者_C百科t 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
Have you tried echo $this->view->username
?
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 use echo $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.
精彩评论