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 Level in Volt

Is Volt template provide Render Levels?

I have some code:

{# layout.volt #}
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <title>{% block title %}Welcome!{% endblock %}</title>

    <link href="/css/style.css" type="text/css" rel="stylesheet" />
    {% block CSS %}{% endblock %}

    <script src="/js/jquery-1.11.0.min.js"></script>
    <script src="/js/jquery-ui.min.js"></script>
    {% block JS %}{% endblock %}
</head>
<body>
<div id="jsContent">
    {% block content %}
    {% endblock %}
</div>
</body>
</html>
{# NewEvent.volt #}
{% extends "layout.volt" %}

{% block title %}title{% endblock %}

{% block JS %}
    <script src="/js/backend/some.js"></script>
{% endblock %}

{% block content %}
    <form action="{{ form.getAction() }}" method="post" class="create-entity">
        ...
    </form>
{% endblock %}
<?php

namespace Backend\Controllers;

class SomeController extends ControllerBase
{
    public function newEventAction()
    {
        $form = new EventForm();

        //this not works
        $this->view->setRenderLevel(View::LEVEL_ACTION_VIEW);

        $this->view->setVar('form', $form);
    }
}

And finally, the output is a full page, with layout content.

How to make it work, without layout, only "block content"?



16.0k
Accepted
answer
edited Apr '15

By default phalcon knows to render each level so you wouldn't need to extend the main layout. Also when you extend the main layout it renders it even you set to render only the action, because is part of action. Try removing

{% extends "layout.volt" %}

https://docs.phalcon.io/en/latest/reference/views.html#control-rendering-levels



2.2k

Thank you. I read docs again and saw that my layout named as "layout.volt". While debugging I saw "_mainView = "index""...

By the word :

        $di->set('view', function () {

            $view = new View();

            /**
             * Added this
             */
            $view->setMainView('layout');

            $view
                ->setViewsDir(__DIR__ . '/views/')
                ->registerEngines(array(
                    '.volt'  => function (View $view, $di) {
                            $volt = new Volt($view, $di );

                            $volt->setOptions(array(
                                'compiledPath'      => __DIR__ . '/cache/',
                                'compiledSeparator' => '_',
                                'compileAlways'     => true,
                            ));
                            return $volt;
                        },
                    '.phtml' => 'Phalcon\Mvc\View\Engine\Php'
                ));

            return $view;
        }, true);