Created
July 19, 2013 10:10
-
-
Save rakeshtembhurne/6038128 to your computer and use it in GitHub Desktop.
CakePHP: Render view in a variable inside controlller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$view = new View($this, false); | |
$view->set(compact('some', 'vars')); | |
$html = $view->render('view_name'); |
Thanks! Works with "cakephp/cakephp": "2.6.*"
. I'd add, that to render without the layout, just set $view->layout = null
, or at least that's what worked for me.
This still works in 3.0.*, with slightly tweaked code:
$view = new View($this->request);
$view->set(compact('some', 'vars'));
$html = $view->render('view_name');
Very helpful, thank you!
public function index()
{
$this->set(compact('users'));
$this->set('_serialize', ['users']);
}
i am not able to call simple view from my controller ..pls help.
Thanks!!
what if the View is for a different Controller?
Thank u very much! Work like a charm! (also i created empty.ctp Layout, but how suggest @TamasBarta it can be disabled)
For different controller view just pass the path of the .ctp file in the render() method call...
$view = new View($your_controller || $this, false);
$view->layout = false; //if you donot want to render it in template
$view->set('some_data', $some_data);
$html = $view->render('../back/path/to/the/other/controller/ctp/file.ctp');
public function template(){
$entity = TableRegistry::get('MyTable')->find()->last();
$view = new View($this->request);
$view->layout = false;
$view->set(compact('entity'));
$html = $view->render('Etiqueta'.DS.'por_aviso');
debug($html);
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you.