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 View Problem

About the View of Phalcon. I wanted to make a menu generator. And i want to Compile Two Volt files in to String and combine this string. After that, i just wanted to simply echo or return this string to View(This string will be in RAW HTML code).

And i didn't found any function to support it. If I make new View Object and Pick the Volt by $view->pick. it's not converting to String but stays in object.

So...Any idea how to do it? guys?

Two options come to my mind:

1) Render the views into a variable (string)

$viewParams = [
    'param1' => '....',
    'param2' => '....',
];

// Render view into a variable
$this->view->setRenderLevel(\Phalcon\Mvc\View::LEVEL_NO_RENDER);
$html = $this->view->getRender('yourTemplateDir', 'yourTemplateName', $viewParams, function($view) {
    $view->setRenderLevel(\Phalcon\Mvc\View::LEVEL_ACTION_VIEW);
});

More info on view render levels: https://docs.phalcon.io/en/latest/reference/views.html#control-rendering-levels

2) Use volt view fragments

{% cache "sidebar" %}
    <!-- generate this content is slow so we are going to cache it -->
{% endcache %}

More info here: https://docs.phalcon.io/en/latest/reference/volt.html#caching-view-fragments



961

Thanks.

But Still not working quite well here.

The Return of the First Code is empty ' '.

I used this view in my own class which extends \Phalcon....\Component class .

use \Phalcon\Mvc\User\Component;

use \Phalcon\Mvc\View;

use Phalcon\Session\Adapter\Redis;

          class MainMenu extends Component {

public $MenuCfg; protected $cfgHash; protected $redis; public .... }

Any Ideas or Can you teach me some thing about this View thing? I used Laravel and some other framework stuff before. But not quite understand this volt thing. It's quite similar to Twig or Blade but not exactlly same the way the View Generated.