Yii 1.1: Rendering a view file in a console application

8 followers

When someone want to send formatted HTML mails from a console application she/he will probably come across the problem of rendering view files.

The simlpest solution i could find was to use a behavior to bypass some web application specific problem (i used a widget to render the HTML templates with generated content).

Here is the behavior:

class CAViewRendererBehavior extends CBehavior
{
    public function getViewRenderer()
    {
        return NULL;
    }
 
    public function getTheme()
    {
        return NULL;
    }   
}

And the config:

return array(
    .
    .
    .
    'behaviors'=>array(
        'viewRenderer'=>'CAViewRendererBehavior',
    ),
    'components'=>array(
        .
        .
        .
    );

The behavior implements the functions the widget checks before rendering by defaults. By returning NULL the widget skips rendering with a ViewRenderer and uses default directory (skipping the theme).

You can implement the two functions above, if you need to use a custom renderer, or a theme based template. Please note, that these functionalities are already implemented in Yii, so you can use them as a starting point.

Total 1 comment

#5994 report it
phreak at 2011/12/04 03:31am
my way

I just use the CConsoleCommand->renderFile() function which renders a view file ? I think its easier and more straightforward.

Leave a comment

Please to leave your comment.