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

Best method for including site templates

Just starting with Phalcon and looking into it with a view to porting a big project from CodeIgniter to Phalcon before development continues.

I'm wondering what is the best method for including site templates - skeleton, nav, header, footer etc, all of which contain dynamic features. In other words, the main style, look and feel of the site which should be displayed for every controller that does not return AJAX, JSON or a background process.

I prefer to use a complete skeleton view which contains doc type, html open, head open, head close, body open, body close, html close etc in a single view file, then render other template views into that such as nav, header, footer etc. and the output view from the controller output.

From scrolling endlesly through the docs I can't see what the best method may be. So far I have created my own render function in a base controller that I have to call from my controllers in order to get the complete output;

Base controller

    <?php

    class ControllerBase extends \Phalcon\Mvc\Controller
    {

        public $siteName = FALSE;
        public $metaTitle = FALSE;

        public function initialize(){
            $siteName = $this->config->site->name;
            $this->view->setVar('siteName', $siteName);
            $this->tag->setTitleSeparator('-');
            $this->tag->setTitle($this->config->site->metaTitle . " Phalcon");
        }

        public function __render($viewToLoad, $data=FALSE){
            $this->view->setVar('render_content', $viewToLoad);
            $this->view->setVar('data', $data);
            $this->view->pick('layouts/skeleton');
        }

    }

Page controller

    <?php

    class IndexController extends ControllerBase
    {

        public function indexAction()
        {
            $this->tag->prependTitle('Welcome to');
            $data = 'Hello and welcome to ';
            $this->__render('index/index', $data);
        }

    }

Skeleton view

    <!DOCTYPE html>
    <html>
        <head>
            <?php echo $this->tag->getTitle(); ?>
            <!-- CSS -->

            <!-- JS -->

        </head>
        <body>
            <!-- Nav bar -->
            <?php $this->partial("layouts/nav") ?>

            <!-- main content -->
            <?php $this->partial($render_content) ?>

            <!-- footer -->
            <?php $this->partial("layouts/footer") ?>

        </body>
    </html>

I'm sure this can't be the best way! Phalcon seems to have some very powerful view rendering features so I don't think I should have to create my own render function and re-invent the wheel as it were! I need to be able to dynamically add CSS & JS to the skeleton from within some controllers as well as making some vars available to some or all site template components etc.

So what is the best or recommended method for doing this?

I'm not 100% clear on what you're trying to accomplish. All of my template files have this basic format (in Volt syntax)

{{ partial('partials/head') }}
...
markup for the page in question
...
{{ partial('partials/foot') }}

My controllers can then set variables necessary for the particular template. My apologies if I'm being dense - it's just not clear to me what your issue is.

If it's relevant, Phalcon does have template inheritance: https://docs.phalcon.io/en/latest/reference/volt.html#template-inheritance



20.4k

Thanks for your reply quasipickle, I prefer a single skeleton than seperate head and foot files. For a start it's easier to follow the design flow in a single file. It's also easier to dynamically switch template if you need to, amongst other things. The head/foot method is hard coded into every view file!