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

Render template in the Volt Extension

Hi All,

I am trying to write my own functions for Volt to render some form parts. I have written static method like below which is working for me alright, but I have got a few questions.

public static function form_element($form, $element, $label = true, $inline = true)
{
    $di = \Phalcon\DI::getDefault();

    $di->set('viewFormElement', function() {
            $view = new \Phalcon\Mvc\View();
            $view->setViewsDir('../app/engine/Volt/View/');
            $view->registerEngines(array(
                ".volt" => 'voltService'
            ));
            return $view;
        });

    $view = $di->get('viewFormElement');
    $view->setVars(array(
        'label' => true,
        'form' => $form,
        'element' => $element
    ));

    $view->start();
    $view->render('form', 'formElementInline');

    return $view->getContent();

}

When I am trying to add $view->start() (doesn't matter if after render or after getContent()), the content is empty. If I am not adding $view->start() then it is printing the content just before generating of the main content.

Could someone please let me know why this is happening and what is the best way to do it?



6.2k

I have changed it to the code below, but I am getting now NULL from method render and method getContent

public static function form_element($element, $label = true, $inline = true)
{
    $di = \Phalcon\DI::getDefault();

    $di->set('viewFormElement', function() {
            $view = new \Phalcon\Mvc\View\Simple();
            $view->setViewsDir('../app/engine/Volt/View/');
            $view->registerEngines(array(
                ".volt" => 'voltService'
            ));
            return $view;
        });

    $view = $di->get('viewFormElement');
    $view->setVars(array(
        'label' => true,
        'element' => $element
    ));

    return $view->render('form/formElementInline');   
}