Slim icon indicating copy to clipboard operation
Slim copied to clipboard

Custom MustacheView breaks in 2.3.0

Open anoxic opened this issue 13 years ago • 6 comments

(Possibly related to https://github.com/codeguy/Slim/issues/567)

I just updated to 2.3.0. I have a simple MustacheView class, like so:

class MustacheView extends \Slim\View
{
    public function render($template)
    {
        $mustache = new Mustache_Engine(array(
            'loader' => new Mustache_Loader_FilesystemLoader('views')
        ));
         return $mustache->render($template, $this->data);
     }
 }

With calls like:

$app->render($template, $data);

After switching to version 2.3.0 none of the data is getting to Mustache. Downgrading to 2.2.0 causes everything to work as expected. What am I doing wrong?

anoxic avatar Jul 18 '13 15:07 anoxic

UPDATE: After examining the Twig Slim View at https://github.com/codeguy/Slim-Views/blob/master/Twig.php, I have found just changing $this->$data to $this->all() fixes the problem.

return $mustache->render($template, $this->all());

anoxic avatar Jul 18 '13 15:07 anoxic

Does using (array)$this->data resolve it? If so, then this has been logged and I think dealt with for the next point release.

designermonkey avatar Jul 18 '13 16:07 designermonkey

I tried casting it as an array, and it still breaks. What is the difference between $this->data and $this->all()?

anoxic avatar Jul 18 '13 18:07 anoxic

Future versions of Slim will only support the custom Twig and Smarty classes. However, @anoxic's comment above is correct. You can fix the issue by changing $this->data to $this->data->all().

The View contains an instance of \Slim\Helper\Set to store and retrieve template data.

codeguy avatar Jul 18 '13 18:07 codeguy

var_export($this->all()) yields:

array (
  'site_view' => true,
  'main_view' => true,
)

but var_export((array)$this->data) (in 2.3.0) yields:

array (
  '' . "\0" . '*' . "\0" . 'data' => 
  array (
    'site_view' => true,
    'main_view' => true,
  ),
)

Where in 2.2.0 var_export((array)$this->data) was the same as what var_export($this->all()) yields now.

It is also possible to get the correct data by using $this->data["\0*\0data"] right now, but it seems quirky. At any rate, $this->all() does the job.

anoxic avatar Jul 18 '13 18:07 anoxic

#537 was about fixing this issue. It didn't make it to 2.3.0 however.

zomble avatar Jul 18 '13 23:07 zomble