Dear friends!
I am trying to use Smarty view rendered with yii2. I cannot find how to use smarty template as main layout. I found some sample code, but it doesn’t work.
This is ‘main.tpl’:
<html lang="en">
<head>
<meta charset="{$app->charset}"/>
<title>{$this->title|escape}</title>
{$this->head()}
</head>
<body>
{$this->beginBody()}
{$content}
{$this->endBody()}
</body>
{$this->endPage()}
This is how I use it (in siteController.php):
public function actionSmarty()
{
$this->layout = 'main.tpl';
$smarty_vars['lis'] = [1,2,3,4,5];
return $this->render('smarty.tpl', $smarty_vars);
}
This is ‘smarty.tpl’
<ul>
{foreach from=$lis item=li}
<li>{$li}</li>
{/foreach}
</ul>
This is the output:
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8"/>
<title></title>
</head>
<body>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
</body>
So:
-
$content and $app are passed to the template and used as expected
-
$this seems to be undefined and gets ignored
Also.
Attemps to use some other objects (such as widgets) produce the following error:
I get the above error when try to use the following template:
<!DOCTYPE html>
<html lang="{Yii::$app->language}">
<head>
<meta charset="{$app->charset}"/>
<title>{$this->title|escape}</title>
{$this->head()}
</head>
<body>
{$this->beginBody()}
{Breadcrumbs::widget(['links' => $this->params['breadcrumbs'] ]}
{$content}
{$this->endBody()}
</body>
{$this->endPage()}
It seems that I cannot simply ‘copy-paste’ such code from YII’s php templates. What it the right syntax in this case?
Any help will be much appreciated.