0

I'm running several cronjobs as CakePHP shells and I need to generate certain documents there. Some of them are HTMLs that are going to be converted to PDFs, some of them are email with HTML content. I have templates/views for these and I'm able to render them via Controller. How can I render them in Shell?

Currently I'm using str_replace() to replace certain tokens with the strings I need, but that is dirty and I need more template-like functionality like loops and conditions.

In Smarty there is Smarty->fetch(). In CodeIgniter there is $this->load->view(). How is it done in CakePHP?

tereško
  • 58,060
  • 25
  • 98
  • 150
Ivan Petrushev
  • 182
  • 2
  • 13

2 Answers2

2

In cake 1.3:

    App::import('Controller');
    $this->view = new View(new Controller(), false);
    $this->view->viewPath = 'elements';
    echo $this->view->element($name,$params);

In cake 3.x

    $view = new View();
    echo $view->element($name,$params);
Amorphous
  • 779
  • 7
  • 27
-1

I do not see str_replace as dirty.

cake views can be rendered into a string variable vary easily… using the “requestAction()”

function {
requestAction("/controller/action/input1/input2",array("return"));
}

Source: Post #3 and on wards: http://cakebaker.wordpress.com/2005/12/31/cakephp-and-smartyies/

$posts = $this->requestAction(‘posts/index’);
foreach($posts as $post):
echo $post['Post']['title'];
endforeach;
TheBlackBenzKid
  • 26,324
  • 41
  • 139
  • 209
  • str_replace becomes dirty when you need to replace some more complex structures than simple strings. This requestAction() requires additional controller to be used and its action renders the view. Also I'm not sure how it works, it seems it makes a real http request. For me it works only if I put a full "http:// www.domain.com/controller/action/" for the first parameter. I thought there might be an easier approach like "put this data into that view and give me the result". – Ivan Petrushev Jul 13 '12 at 06:48
  • Maybe you could do `=getFunction($abc)?>` place this in the template files. Then you could in the PHP controllers use `function getFunction($result)` and call it that way? – TheBlackBenzKid Jul 13 '12 at 08:12