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 to render a templates with layouts using micro

Hello everyone!

I have a REST API app that I'm adding a number of basic html pages to.

I have the following services.php:

<?PHP
use Phalcon\Mvc\View\Simple as View;
use Phalcon\Mvc\Url as UrlResolver;
use Phalcon\DI\FactoryDefault;
use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;

$di = new FactoryDefault();

/**
 * Sets the view component
 */
$di['view'] = function () use ($config) {
    $view = new View();
    $view->setViewsDir($config->application->viewsDir);
    $view->registerEngines([
        '.volt' => 'volt'
    ]);
    return $view;
};

// Register Volt as a service
$di['volt'] = function($view, $di) use ($config)
{
    $volt = new Phalcon\Mvc\View\Engine\Volt($view, $di);
    $volt->setOptions(
        array(
            'compiledPath'      => $config->volt->path,
            'compiledExtension' => $config->volt->extension,
            'compiledSeparator' => $config->volt->separator,
            //'stat'              => (bool) $config->volt->stat,
        )
    );
    return $volt;
};

I am trying to render the following:

$app->get('/', function () use ($app)
    {
        // I'd like to render <viewDir>/index/dashboard.volt as
        // the content inside <viewDir>/layouts/index.volt here
        echo $app['view']->render('index');
    });

How do you do that in micro?

Thanks Thien,

Is there a way of specifying the main layout file in config and rendering a body template with ->render and {{ content() }} using micro?

Hi

You can refer sameple app https://github.com/phalcon/store