Two separate views in once request

I created simple CMS and need to render right side of web page content which is not related to the central (middle) part of page - separate controller, separate action, separate view.

To do that, I need to call another controller action, render it to variable and inside index.phtml <?= $rightSide; ?>

I was thinking to use ControllerBase afterExecuteRoute() and call controllers action and render it to variable but I don't know how to do that.

I know I can do that with Ajax, but I don't want to do in that way.

How to do that?

Izo, please, before comment read entire post.



32.2k

Hi,

What you're asking is not directly possible, but .... this is a very common use case ! And they are many ways to do that !

but without more details regarding what you want to achieve, I think that I will surely give you bad advices ...



32.2k

you can have a look at the demo apps on github.com/phalcon (like Invo, Album-O-rama, ...) and see how menues are built and rendered

Thank you le51!



32.2k

phalcon mvc framework is very flexible, templates, volt blocks could be very usefull too

If phalcon is very flexible, at ControllerBase afterExecuteRoute() how to call action unrelated to router controller/action, and render it to variable?



32.2k

I told you that this is not possible ;-)

a view is rendered in a controller context. You can't have one view with two different associated controllers (without some ajax magic).

once again, without some real clear use case and some code fragments (what's the code in your initial controller action and the one in the controller's action you want to call with afterExecuteRoute() ...) I can't help you further.



69.0k
//service
$this->di->set('simpleView', function ($dir) use ($config) {

            $eventsManager = new EventsManager();
            $view = new \Phalcon\Mvc\View\Simple();
            $view->setViewsDir($dir);
            $view->registerEngines([...])
}, false);

//in a contoller 

$viewEngine = $this->getDI()->get("simpleView", "app/parcials");
$myBlock = $viewEngine->render("user/profile.volt", [ .. params ..])

now all this can be in a seperated function so it can be more commonly used



32.2k
edited 20m ago

saying you want a common layout with a general and a more contextual menus on the right side of your pages, using templates like this:


// in ControllerAdmin

public function initialize()
{
    $this->view->mainMenu = [
        // an array of menu items
    ];
    $this->view->setTemplateAfter('admin');
}

// in MyController
class MyController extends ControllerAdmin

public function initialize()
{
    $this->view->contextMenu = [
        // an array of menu items
    ];
}

// views/layouts/admin.volt
<div class="row">
    <div class="col-sm-3 col-md-2 sidebar">
          <ul class="nav nav-sidebar">
            {% for menuItem in mainMenu %}
                <li>
                    {{ link_to(menuItem.name,menuItem.link) }}
                </li>
            {% endfor %}
          </ul>
           <ul class="nav nav-sidebar">
            {% for menuItem in contextMenu %}
                <li>
                    {{ link_to(menuItem.name,menuItem.link) }}
                </li>
            {% endfor %}
          </ul>
    </div>
    <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
          {{content()}}
    </div>
</div>