Archive for September 2nd, 2010

How to get the returned value from a given module/action without rendering a view template. symfony 1.3

I had been tring to figure this out for a while now.
Typically if you wanted an action to render text, you would use the following line of code.

    public function executeTest() {
         return $this->renderText('abcdefg');
    }

This is fine if you access that action directly.
I wanted a way to grab the output of another module/action.
The best way to this is using the getPresentationFor($module, $action) method

    public function executeTest2(){
        $output = sfContext::getInstance()->getController()->getPresentationFor('user', 'test');
        echo $output;
        exit;
    }

The problem is…
This method grabs the presentation (ie, rendered template) for the module/action (‘user’, ‘test’) but in action executeTest, there is no template to render, causing an error.
Why dont I just create a template and be happy with the returned presentation? Id rather bypass this step to optimise code execution.

Normally I would return sfView::NONE in my action to fluff the template, but getPresentationFor() still looks for a template and I cant return any rendered text if Im returning sfView.

I found a way to set the view to NONE without returning sfView. This leaves me a way to return $this->renderText().

this is how I did it…

    public function executeTest() {
         sfContext::getInstance()->getController()->getRenderMode(sfView::NONE);
         return $this->renderText('abcdefg');
    }
    public function executeTest2(){
        $output = sfContext::getInstance()->getController()->getPresentationFor('user', 'test');
        echo $output;
        exit;
    }

No Comments