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

How would you render a sub view/widget inside a main window?

Hello,

I'm very new to Phalcon and excited. I've watched tutorials and get the basics of Phalcon's controllers and view hierarchy but I'm having a hard time wrapping my head around what I would like to achieve. I currently have a header, footer, side left panel and Main Content all loading using the regular hierarchical rendering mixed with some partials in my DashboardController file. Currently the Main Content area is empty as I'd like my widgets to load in there. In my old php project I have a folder of 'apps' where each app is built from pure php (no frameworks) and I simply include each folder and load the app into a sub view in the Main Content area.

(app folder structure example)
app/todo/index.php 
app/calendar/index.php
app/metrics/index.php

So my questions are:

  • In Phalcon, how can I look into an apps folder, with no phalcon structure just regular php, and load that in my empty Main Content area in my DashboardController?
  • Eventually I'll convert those apps to all use phalcon controllers and views so how would I then render that app view inside the Main Content area?

All in all how do I render an external view within a view?

This may or may not be too complex to answer so any tips, advice or some form of direction would be greatly appreciated. Thanks a bunch.



17.5k

I use custom routing and don't use volt, but you could also solve w/ regular routing and volt in a similar fashion.

I have a main layout file... views/layouts/layout.phtml That contains the shell of the page.

Inside of layout.phtml, I have an echo of content:

<?php echo $this->getContent(); ?>

Inside of my routes file, I have the get:

$router->addGet("/",[
    'controller' => 'noauth',
    'action' => 'index'
]);

Then inside the controller, I have:

<?php

use Phalcon\Http\Response;

class NoauthController extends ControllerBase {

    public function initialize() {
        $this->view->setTemplateAfter('layout');
    }

    public function indexAction() {
        $this->view->pick("home");
    }
}

Then I have a file: views/home.phtml that has the content for that route.

Thanks Zach but it's not quite what I'm looking for. Ok, I feel like I've come close but hit another wall as I just can't seem to get what I'm trying to achieve here. Here's an update from what I've managed to do thus far.

I have a main controller called DashboardController and in the indexAction() I want to run a loop to look at each multi module application and return it's default action/view. This returned view will be stored in an array which will again be looped in the view component to output each module.

As of now I can do this but it only grabs the view, it doesn't go through the controllers at all which means I can't make DB calls that are specific to that module. I've structured my code to be similar to the multi shared views example from here: Multi Shared Views

Here's my loop in the DashboardController indexAction() to get a module to render it's view:


public function indexAction()
{
    // ....
    // ....

    $render_array = array();

    // go through the list of modules
    foreach ($this->config->apps as $key => $this_app) {

        // get the view content for each module
        $this_content = $this->view->getRender($this->config->apps[$key]->name, 'index', null,

            function ($view) use($key) {

                // Set any extra options here
                $view->setViewsDir(APP_PATH . '/apps/'. $this->config->apps[$key]->name. '/views/');
                $view->setRenderLevel(\Phalcon\Mvc\View::LEVEL_LAYOUT);
            }
        );

        array_push($render_array, $this_content);
    }

    $this->view->widgets = $render_array;
}
php

In my dashboard/index.phtml file I then loop through $widgets and display.

How in the world can I loop through the multi modules calling each modules default view and have it use it's own controllers to make DB calls and whatever other processing??