Search code examples
methodsyii2smarty3

Yii2-Smarty: Cannot Access Methods of $this-object


Yii2-Smarty: Cannot Access Object Methods

I set up Yii2 with smarty extension.

Now I am trying to simply convert the layouts/main.php file to a smarty template. For that I need access to the current yii\web\View object which is given from the extension as $this-smarty-variable.

I've validated that the smarty $this variable was the same as the view-object through var_dump()-ing and comparing both.

Problem

According to the smarty3 docs I should be able to call a method on the $this-object from smarty templates like this: {var_dump($this->head())}.

Applying the above script in a template just always returns NULL which let me guess that no method call had happen.

Question

How can I call a method from an object which is given to smarty from PHP ?

  1. Define variable for use in smarty: $template->assign('app', \Yii::$app);
  2. Call method on variable in smarty template: {$app->getSession();}

What am I doing wrong ?

(my current smarty layout: pastebin)


Solution

  • Applying the above script in a template just always returns NULL which let me guess that no method call had happen.

    the head() method does not return anything so if you see null that is correct.

    You can call the method like this:

    {$this->head()}