mervick / yii2-mthaml

The MtHaml integration for the Yii2 framework.
MIT License
8 stars 1 forks source link

Cannot use $this: Using $this when not in object context #2

Closed starrychloe closed 9 years ago

starrychloe commented 9 years ago

Yoohoo, it's me again... thanks for the fix for the other problem. Here's another. There's no way to use HAML on the layout or to render a sub page, as $this is unavailable. I tried the following, copying from the default views/layouts/main.php.

-use app\assets\AppAsset;
-AppAsset::register($this);
!!! 5
%html
  %head
    -$this->head()
PHP Fatal Error – yii\base\ErrorException: Using $this when not in object context
<?php
use app\assets\AppAsset;
function __MtHamlTemplate_eca6c0b9dd1af0a192edc0c82eb56816dd0802d08929e8ec83ab90a21ffd3692($__variables)
{
    extract($__variables);
?><?php AppAsset::register($this); ?>
<!DOCTYPE html>
<html>
  <head>
    <?php $this->head(); ?>
  </head>
</html>
<?php
}

Line 10 is highlighted in red.

mervick commented 9 years ago

This is because the compiled code is executed inside the function so you need to use $view instead of a $this.

starrychloe commented 9 years ago

That worked! Awesome!

starrychloe commented 9 years ago

How I got a raw HAML page/layout to work:

-use app\assets\AppAsset;
-AppAsset::register($view);
-$view->off(\yii\web\View::EVENT_END_BODY, [\yii\debug\Module::getInstance(), 'renderToolbar']);
-$view->beginPage()
!!! 5
%html
  %head
    -$view->head()
  %body
    %style
      *{margin:0px;}
    .star-rating{:title => $rating, 'data-url' => $model->url}
      *****
  -$view->endBody()
-$view->endPage()