2

I want to render a different view in my action, pass data from action to that view and get its output in a variable.

I know that

  $output = $this->view->render('path/to/script');

render different view script and returns output. But I also want to pass data to this script from my action but not succeeded so far. I have used

 $this->view->data = $data

But it doesn't send data to this script.

Can any one guide how can I do it?

1
  • (OOT) Just a note, to render not default view script $this->renderScript('report/app-list-xlsx.tpl'); Commented Jan 27, 2016 at 11:27

1 Answer 1

5

You can pass an array of data to the view script:

$this->view->render('path/to/script/script', array('someValue' => true)); 

Access it in the view script with:

<?= $this->someValue ?>

EDIT: In a controller I always use...

$this->view->assign('someValue', 'data');

EDIT: With layout pattern... You need to set a variable in the controller for the path of the view script you want to use in the layout template and any variables/data you want passing to the view partial.

In controller:

$this->view->assign('partialPath', 'path/to/partial');
$this->view->assign('partialdata', 'value');

In Layout:

$this->view->render($this->partialPath, $this->partialData) //renders partial and passes data

In partial:

<?= $this->partialData ?> // echos data
Sign up to request clarification or add additional context in comments.

3 Comments

$htmlcontent = $this->view->render('scorecard/pdfpage5.phtml',array('data'=>$data)); at controller doesn't pass data. Tried to get as $this->data;
Are you using MVC pattern with a layout?
Yes with layout pattern

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.