Reputation: 1
I want to set a layout like main.htm for all of my twig files in yii 2, but it loads the folder that controller call first, so I can not call my layout with extends.
in SiteController
public function actionIndex()
{
return $this->render('index.htm');
}
This code calls views/site/index.htm
In my index.htm
{% extends "main.html" %}
So if I put the main.html in site folder, there is no problem, but I want to put it in the layout folder and can be called every controller views .. How can I do that ??
views
->site
->index.html
->layouts
->main.html
For example in symfony we can call like this
{% extends '::base.html.twig' %}
But in Yii2 I can not found a way to do this ....
I have done this with hardcoded the file viewRenderer.php. This is not the way I like it because a composer update the changes are gone away...
Now the function init is like this the loader is null here. I have added loader for my layouts.
$this->twig = new \Twig_Environment(null, array_merge([
'cache' => Yii::getAlias($this->cachePath),
'charset' => Yii::$app->charset,
], $this->options));
After adding a loader ..
$this->loader = new \Twig_Loader_Filesystem($_SERVER["DOCUMENT_ROOT"].'\views\layouts');
And last thing is render function in ViewRenderer.php
public function render($view, $file, $params)
{
$this->twig->addGlobal('this', $view);
//$this->twig->setLoader(new TwigSimpleFileLoader(dirname($file)));
$this->loader->addPath(dirname($file));
return $this->twig->render(pathinfo($file, PATHINFO_BASENAME), $params);
}
I want it without hardcoded changes in the core file of yii2-twig ....
Upvotes: 0
Views: 2826
Reputation: 9357
Have you tried using a full path here {% extends "main.html" %}
. Something like {% extends "\layouts\main.html" %}
Or even {% extends "@app/views/layouts/main.html" %}
I have not used twig with yii, but it is worth a shot.
Upvotes: 0