Yii Framework Forum: Smarty renderer: how to pass Yii objects to main template? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Smarty renderer: how to pass Yii objects to main template? Rate Topic: -----

#1 User is offline   phantomlord 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 15-October 17

Posted 15 October 2017 - 07:11 AM

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:
1) $content and $app are passed to the template and used as expected
2) $this seems to be undefined and gets ignored

Also.
Attemps to use some other objects (such as widgets) produce the following error:

Quote

SmartyCompilerException
Syntax error in template "file:/home/nginx/yii1/www/views/layouts/main.tpl" on line 14 "{Breadcrumbs::widget(['links' => $this->params['breadcrumbs'] ]}" - Unexpected "}", expected one of: "","" , ")"


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.
0

#2 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,184
  • Joined: 17-January 09
  • Location:Russia

Posted 16 October 2017 - 02:47 AM

It's described here: https://github.com/y...plate-syntax.md
Yii 2.0 Development Cookbook

Enjoying Yii? Star us at github

Support me so I can work more on Yii: https://www.patreon.com/samdark
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users