I want to have a Mustache template reference a partial where the partial adds data to the context as well. Instead of having to define the data in the data to the initial Mustache rendering.
I have a mockup in https://gist.github.com/lode/ecc27fe1ededc9b4a219
It boils down to:
<?php
// controller
$options = array(
'partials' => array(
'members_collection' => new members_collection
)
);
$mustache = new Mustache_Engine($options);
$template = '
<h1>The team</h1>
{{> members_collection}}
';
echo $mustache->render($template);
// viewmodel
class members_collection {
public $data;
public function __toString() {
$template = '
<ul>
{{# data}}
{{.}}
{{/ data}}
</ul>
';
$mustache = new Mustache_Engine();
return $mustache->render($template, $this);
}
public function __construct() {
$this->data = array(
'Foo Bar',
'Bar Baz',
'Baz Foo',
);
}
}
This gives an error like Cannot use object of type members_collection as array.
Is there a way to make this work? Or is using __toString not the right way? And would using a partials_loader or __invoke help? I got it working with neither but might miss something.
I'm assuming you're using the bobthecow PHP implementation Mustache templating in PHP.
As of last time I checked, Mustache PHP it didn't support data driven partials. You want sort of a 'controller' backed a partial... however, currently partials just simple include-this-file style partials.
You're gonna have to build this yourself. Good luck!