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

Is it possible to use views like this way

Hello,

First of all English is not my primary language and I'm not sure about to explain my problem properly, I thank you for your patience.

I'm trying to move from ZF to pHalcon. However I had some diffuclties. To be honest I already archieve this. However it looks like a bit hackish. (freeze the falcon and use old clasess). And I just want more offical way to archieve this.

For a long time I use GLOBAL output storages to display pages.

Let say I had standar 3col layout.

Header

Nav Content ADS

Footer

In my curent working way system has default layout (look like above) and may hav use different layout, either by programmer (for static controllers) or my have user configurable (dynamc page renderer) layouts.

(for example leftcol layout

Header

Nav Content ADS

Footer

)

Also I had dynamic page renderer, which user may choose any kind of plugin to put those predefined locations via viusal tools. Plugins also may send output in any location..

it was so simple (to me).

I just store template output in location holder.

t::assign('NAV',t::renderTemplate('this/that.php',array('foo'=>$bar)));

this will send my ouput in to NAV holder.

And at end of the page execution. I fetch the all output then print in proper locations.

php echo t::$NAV;

However, I can't find to use this model in pHalcon way. Is there any way to archieve this ?

My Best Regards



606

Hi,

I think the best way to solve this issue is with volt templates (https://docs.phalcon.io/en/latest/reference/volt.html). You can create a layout master template, and then define blocks of contents that will be filled with new contents generated by the controllers. You also can use the "include" tag to include the content from another volt template, like a separated "navigation.volt" template.

For other examples on how you can use the volt template structures to easily produce complex templates using volt structure, please visit: https://docs.phalcon.io/en/latest/reference/volt.html#view-integration



11.9k

Hello thanks for reply.

My system works on controller level. Layouts does not know anything about the content. Naming them navigation etc just make sense. You can add more location or change names.

I'm just creating content somewhere im my code and I set the destination of the content at rendering time. Then inject that content in page, at rendering time.

If I understood correctly, pHalcon uses pre determined template hiearchy, which perfectly fit something like wordpress page etc. However my system works like typo3 and it was chaotic.

I just do not want to use strict template policy.

You can disable template hierarchy loading: https://docs.phalcon.io/en/latest/reference/views.html#disabling-render-levels

Or define just what type of rendering levels you want for your application: https://docs.phalcon.io/en/latest/reference/views.html#control-rendering-levels

For me, the best mode is Action View rendering level and use volt template inheritance to extend the main layout template: https://docs.phalcon.io/en/latest/reference/volt.html#template-inheritance

Forgot to add this. If you want to setup by default witch Rendering Level you want, you can do this with setRenderLevel while setting up the view in index.php :

The code highlight bellow will have weird colors in variable names, just select all code to see it correctly:

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

                $view = new \Phalcon\Mvc\View();
                $view->setRenderLevel(View::LEVEL_ACTION_VIEW);
                $view->setViewsDir(__DIR__ . $config->application->viewsDir);
                $view->registerEngines(array(
                    '.volt' => 'volt',
                ));

                return $view;
        });