Custom MustacheView breaks in 2.3.0
(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?
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());
Does using (array)$this->data resolve it? If so, then this has been logged and I think dealt with for the next point release.
I tried casting it as an array, and it still breaks. What is the difference between $this->data and $this->all()?
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.
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.
#537 was about fixing this issue. It didn't make it to 2.3.0 however.