Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

return rendered view with variables in component

Tags:

cakephp

I'd like to have some layouts for messages in my MessageComponent. Its send method should get an array of variables which used in view (layout) and render message with them, then send it..

The question is:

How to render a view (layout) with an array of variables in a component? and get rendered content instead of print it (in a component, too)

Thanks.

like image 566
mrdaliri Avatar asked Apr 29 '26 15:04

mrdaliri


1 Answers

(Edit: Misread your question)

If you want to get the HTML of a rendered view, simply do something like:

$view = new View($this, false);
$view->set(compact('foo', 'bar')); // set variables
$view->viewPath = 'elements'; // render an element
$html = $view->render('message'); // get the rendered markup

This should work in a controller as well as a component.

like image 191
Lèse majesté Avatar answered May 02 '26 11:05

Lèse majesté