Chris
Chris

Reputation: 8968

Override layout in Phalcon

I have found out how to do a view override in Phalcon with the help of this: https://stackoverflow.com/a/19941928/74651

The problem is that it uses this method for the layout to, but if the directory does not exists on the original viewpath it does enter in this method.

Where does phalcon checks the directory for the layout and how to override it?

Upvotes: 0

Views: 4502

Answers (4)

Vivek Sharma
Vivek Sharma

Reputation: 71

Use this in your controller... worked for me

public function initialize() {
  parent::initialize();
  $this->view->setTemplateAfter('yourtemplate');
} 

Upvotes: 2

Alan
Alan

Reputation: 213

Checkout this framework: https://github.com/alanbarber111/cloud-phalcon-skeleton

Allows you to have design packages on a per website basis with the ability to setup a "Fall back" directory and an "override" directory. If nothing else, take a look at app/code/Core/Model/App/Design.php and app/code/core/Model/View* to see how we've completed this.

Upvotes: 2

Chris
Chris

Reputation: 8968

Ok i just figured it out after some debugging.. cannot dump vars in view class which is a bit annoying ;)

My problem was indeed, that the layout needs to be relative to the view dir. When you override the view dir as on: https://stackoverflow.com/a/19941928/74651 it will look for the layout on the original view dir not the override.

It's possible to override this in _engineRender a bit annoying that they force the relative directory and only trigger and event if a file has been found not very flexible.

<?php
namespace Phapp\Mvc;

class View extends \Phalcon\Mvc\View {

    /**
    * @param array $engines
    * @param string $viewPath
    * @param boolean $silence
    * @param boolean $mustClean
    * @param \Phalcon\Cache\BackendInterface $cache
     */
    protected function _engineRender($engines, $viewPath, $silence, $mustClean, $cache) {        
    // Layout override
    if ($this->getCurrentRenderLevel() !== \Phalcon\Mvc\View::LEVEL_LAYOUT) {
        return parent::_engineRender($engines, $viewPath, $silence, $mustClean, $cache);                        
    }
    foreach ($engines as $extension => $engine) {
        if (!$engine instanceof View\Engine\ThemeInterface) {
            continue;
        }
        $layout = $engine->getThemePath().$viewPath.$extension;
        if (is_readable($layout)) {
            $originalViewDir = $this->getViewsDir();
            $this->setViewsDir($engine->getThemePath());
            $content = parent::_engineRender($engines, $viewPath, $silence, $mustClean, $cache);
            $this->setViewsDir($originalViewDir);
            return $content;
        }
    }
    }        
}

Upvotes: 1

Ian Bytchek
Ian Bytchek

Reputation: 9075

Here and kind of here.

So, you could do three things. First, change the layout dir, second, just set the layout, or third, change both :)

$view->setLayoutsDir('custom/layouts/');
$view->setLayout('outrageouslyCustomLayout');

See all methods in the documentation. If I remember correctly, paths must be relative to your views directory.

Standard view offers quite a few ways of controlling your rendering, it might be that you don't really need to override things – http://docs.phalconphp.com/en/latest/reference/views.html#control-rendering-levels

Upvotes: 3

Related Questions