0

I want to access the variable in layout in zend framework, how can I do that. I searched a lot but can't find any thing useful or helpful for me. Below are the links that I already tried so admin please don't mark this question as duplicate one..

Thanks..

Zend Framework 2 - Layout and variable

access controller action variables to zf2 layout

Senario

I have a link in layout.phtml that I want to display on conditional bases. Like some user of my sites cannot see that link but other can do. That condition comes from the databases that I have but I don't know how to access that in layout.phtml

Example

<?php if($this->check == true) {?><a href="#">This Link</a><?php } ?>

if $this->check == true than show the link otherwise not.

Community
  • 1
  • 1
Najam-us-Saqib
  • 526
  • 3
  • 10
  • 23

2 Answers2

0

Pass the variable from the action to the view like

$this->view->check = true;

Then access it in the view like $this->check and do the checking

if($this->check){
  //Do something
}
else{
  //Do something
}
웃웃웃웃웃
  • 11,829
  • 15
  • 59
  • 91
  • I have more 20 action/views, how can pass the variable in all those actions/views. Plus the link is in the layout not in the view how can I make that link invisible from any other view..... – Najam-us-Saqib Oct 30 '13 at 11:11
  • You have to pass from each action and do the hiding in the corresponding layout of the view. – 웃웃웃웃웃 Oct 30 '13 at 11:14
0

I got the solution of my problem

write the below code in IndexController's init Function

$this->_helper->layout()->myvar = $someValue;

and access it in the layout by this code

$this->placeholder('Zend_Layout')->myvar;

One has to so this for each controller that he/she has in his/her application. Otherwise will get an error regarding undefined variable.

Reference:

http://framework.zend.com/manual/1.12/en/zend.layout.quickstart.html

Line 15 of first code listing....

Najam-us-Saqib
  • 526
  • 3
  • 10
  • 23