How to use a single @layout for all modules
- simonjcarr
- Member | 28
Not sure if am missing something, but it seems that I have put a copy of @layout.latte in the templates directory of every module. But if I decide to change my site layout, I might have 50 files to update.
Is there a way I can put put a @layout.latte file in the Modules folder so that all modules use one layout file.
Last edited by simonjcarr (2015-11-01 20:22)
- simonjcarr
- Member | 28
The only way I have found to do it so far is to include this line in all my views, but it is not very nice.
{layout '../../../../@layout.latte'}
This uses the file app\modules\@layout.latte
Is there a better way?
- David Matějka
- Moderator | 6445
Just override formatLayoutTemplateFiles method in your base presenter like this:
public function formatLayoutTemplateFiles()
{
return ['path/to/@layout.latte'];
}
- simonjcarr
- Member | 28
I have now have the following code in /app/presenters/BasePresenter.php
<?php
namespace App\Presenters;
use Nette;
use App\Model;
/**
* Base presenter for all application presenters.
*/
abstract class BasePresenter extends Nette\Application\UI\Presenter
{
public function formatLayoutTemplateFiles()
{
return ["/app/modules/@layout.latte"];
}
}
But the layout is not being used.
Also when I tried to use protected instead of public, I got the following error
Access level to App\Presenters\BasePresenter::formatLayoutTemplateFiles() must be public
Last edited by simonjcarr (2015-11-01 21:16)
- David Matějka
- Moderator | 6445
oh, sorry I didn't noticed it must be public. So just change the
“protected” to the “public”. And using “/app” will probably not
work, use path relative to the location of the base presenter,
e.g __DIR__ . '/../modules/@layout'
- simonjcarr
- Member | 28
Very strange,
Overriding the formatLayoutTemplateFiles method does not seem to work, however if I change the code in core to include the my folder in the returned array it does work.
/**
* Formats layout template file names.
* @return array
*/
public function formatLayoutTemplateFiles()
{
$name = $this->getName();
$presenter = substr($name, strrpos(':' . $name, ':'));
$layout = $this->layout ? $this->layout : 'layout';
$dir = dirname($this->getReflection()->getFileName());
$dir = is_dir("$dir/templates") ? $dir : dirname($dir);
$list = array(
"$dir/templates/$presenter/@$layout.latte",
"$dir/templates/$presenter.@$layout.latte",
);
do {
$list[] = "$dir/templates/@$layout.latte";
$dir = dirname($dir);
} while ($dir && ($name = substr($name, 0, strrpos($name, ':'))));
$list[] = "/var/www/html/nette_test/app/modules/@layout.latte";
return $list;
}