We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Phalcon doesn't let me render a supporting view within an action

Hi there,

I posted a question yesterday that was somewhat lengthy. I've distilled it down to the code below. Quite simply, the index action is called, body copy is set to $this->view->body. Before we leave the action, I create a new local instance of a view and render a template from a different location. As a result, nothing is rendered at all by the index action.

If I comment out:

$view->render('forms', 'minimal_business_contact');

I get the output I expect!

Any ideas why?


    /**
     * Main index action
     */
    public function indexAction()
    {
    // Get the 'body' markup
        $body = $this->getPage()->get('body');

    // Now, try and capture the output of a different template using a different instance of 'view'
        $view = $this->getDI()->get('view');
        $view->setRenderLevel(\Phalcon\Mvc\View::LEVEL_ACTION_VIEW);
        $view->start();
        $view->render('forms', 'minimal_business_contact'); <--- This Guy hurts!
        $view->finish();
        $form_markup = $view->getContent();

        // Assign the $body markup with process HTML form
        $this->view->body = $body;

    }
edited Aug '14

Just a thought:

It might be worth trying to reset render level before leaving the indexAction:

$view->setRenderLevel(\Phalcon\Mvc\View::LEVEL_MAIN_LAYOUT);


38.8k

Thanks for the suggestion Piotr. Unfortunately, it made no difference :(

Just the act of rendering the new view instance completely stops the output from the action. It would be great if someone could just confirm whether what I am doing should work. It seems reasonable to want to process some form of sub-template inside an action with its own template.

In my case, the $body value includes "inserts" that are searched and replaced with a named HTML form. If I was doing this in a Volt template, it would be easy. However, on this occasion the desired markup is created in another system and on analysis of the body copy (by code not shown in the above example), if there are any forms to be injected, I need to render the form, take the view output and insert into $body.

At that point, I just want the 'index' template to render the view with $body assigned to the 'body' view variable. It seems straightforward enough but I've been trying for days to get this working and it won't output anything.

Thanks for your thoughts around this. It's appreciated :)