Hi all,
I'm building a system where each page a user sees is defined by a Page object. That Page object can then contain any number of Widget objects. Theoretically, the output the users sees is supposed to be generated by the Page object. Inside the Volt view file will be code that will loop through the Page object’s related Widget objects. Those Widget objects will then render their own template file and generate their own output. Something like:
{% extends 'partials/pages/page.phtml' %}
{% block body %}
{% for Widget in Widgets %}
{{ Widget.render() }}
{% endfor %}
{% endblock %}
The difficulty I'm having is in writing the Widget::render()
method. I tried using the View out of the DI and rendering the Widget’s view file that way, but that resulted in only the Widget’s view being displayed - not the Page’s.
Any ideas? I’m willing to completely re-organize how the Widget content gets displayed, if it turns out I’m doing it completely wrong.
UPDATE My render() method looks like this:
$View = \Phalcon\DI\FactoryDefault::getDefault()->get('view');
$View->pick($this->viewfile);
ob_start();
$View->render($this->viewfile,[]);
echo ob_get_clean();
Wrapping $View->render()
in an output buffer appears to have solved the problem, and the Widget output is being displayed in the proper context. So, my problem has been solved, but I am interested in better approaches if they exist.