correct use of widgets??!!??!!

good morning, I have a problem with the widgets and smarty …

I am not able to display the widget where I want to show it.

smarty is my version:Smarty 3.1.7 Released

my yii framework is:v1.1.9

and smarty-renderer :v1.0.2

I have a tpl file where I have the call to the widget:

principal.tpl


<div class="container" id="page">


	<div id="header">

		<div id="logo">{$PageName}</div>

	</div><!-- header -->

        

	<div id="mainmenu">

            <ul id="yw0">

                {foreach $Menu as $pack}

                    <li><a href="{$pack.url}">{$pack.label}</a></li>

                {/foreach}

            </ul>

	</div><!-- mainmenu -->

        

        {widget('zii.widgets.CBreadcrumbs',['links'=>$this->breadcrumbs],true)}

        

        {$Contenedor}


	<div class="clear"></div>



and php file:

main.php




$smarty = new Smarty();

$smarty->assign("URL",Yii::app()->request->baseUrl);

$smarty->assign("PageTitle",CHtml::encode($this->pageTitle));

$smarty->assign("PageName",CHtml::encode(Yii::app()->name));

$smarty->display("templates/principal.tpl");

the error I get is:


PHP notice


Undefined index: this


/var/www/Codesk2/templates_c/9498380aff9ec2291ebe746945e4167a401df325.file.principal.tpl.php(85)


73                 <?php  $_smarty_tpl->tpl_vars['pack'] = new Smarty_Variable; $_smarty_tpl->tpl_vars['pack']->_loop = false;

74  $_from = $_smarty_tpl->tpl_vars['Menu']->value; if (!is_array($_from) && !is_object($_from)) { settype($_from, 'array');}

75 foreach ($_from as $_smarty_tpl->tpl_vars['pack']->key => $_smarty_tpl->tpl_vars['pack']->value){

76 $_smarty_tpl->tpl_vars['pack']->_loop = true;

77 ?>

78                     <li><a href="<?php echo $_smarty_tpl->tpl_vars['pack']->value['url'];?>

79 "><?php echo $_smarty_tpl->tpl_vars['pack']->value['label'];?>

80 </a></li>

81                 <?php } ?>

82             </ul>

83     </div><!-- mainmenu -->

84         

85         <?php echo $_smarty_tpl->tpl_vars['this']->value->widget('zii.widgets.CBreadcrumbs',array('links'=>$_smarty_tpl->tpl_vars['this']->value->breadcrumbs),true);?>

86 

87         

88         

89         

90     <?php  $_smarty_tpl->tpl_vars['pack'] = new Smarty_Variable; $_smarty_tpl->tpl_vars['pack']->_loop = false;

91  $_from = $_smarty_tpl->tpl_vars['migas']->value; if (!is_array($_from) && !is_object($_from)) { settype($_from, 'array');}

92 foreach ($_from as $_smarty_tpl->tpl_vars['pack']->key => $_smarty_tpl->tpl_vars['pack']->value){

93 $_smarty_tpl->tpl_vars['pack']->_loop = true;

94 ?>

95             <?php  $_smarty_tpl->tpl_vars['rest'] = new Smarty_Variable; $_smarty_tpl->tpl_vars['rest']->_loop = false;

96  $_from = $_smarty_tpl->tpl_vars['pack']->value; if (!is_array($_from) && !is_object($_from)) { settype($_from, 'array');}

97 foreach ($_from as $_smarty_tpl->tpl_vars['rest']->key => $_smarty_tpl->tpl_vars['rest']->value){




this is what I have and I can not use this widget,I am lost with the use of widgets and I do not get advance.

can help me ?

thank you very much!

Wow!!! I’m stupid…

I only need one line in the php file


$smarty->assign("this",$this);

and tpl file:


{$this->widget('zii.widgets.CBreadcrumbs',['links'=>$this->breadcrumbs],true)}

but this way seems very dirty

Do I always am obliged to send all the "$ this"?

Could I make this choice?

in php file




ob_start();

$this->widget('zii.widgets.CBreadcrumbs',array('links'=>$this->breadcrumbs));

$variable = ob_get_clean();  

$smarty->assign("caca",$variable);



and tpl file:


{$caca}

so I am not obliged to send all "$ this"

Is cleaner than this?

You should not use Smarty yourself. This is done by smarty viewRenderer.

You first have to add SmartyViewRenderer to your application config as described in the readme.

Then you can simply render your template in a controller action just as you do it with normal views:

http://www.yiiframework.com/doc/guide/1.1/en/basics.view

in the template $this refers to the current controller rendering the template so it has to be:


{$this->widget('zii.widgets.CBreadcrumbs',['links'=>$this->breadcrumbs],true)}

to render a widget.

You do not need to create a Smarty object and you do not need to assign anything to it. Template variables are assigned by giving them as second parameter array to the render()-method.