You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In this mustache spec discussion - mustache/spec#41 (comment) - an example is given for using an object as a helper with Mustache.php. However I've attempted to recreate that example and it's not working as described.
However, this does NOT (but should, based on the example):
class Helper {
public function uppercase($val)
{
return strtoupper($val);
}
}
$m = new Mustache_Engine();
$m->addHelper('helper', new Helper);
echo $m->render("{{# helper.uppercase }}test{{/ helper.uppercase }}",array());
Has this syntax changed, or is there an issue here? I'm testing this with 2.0.2.
The text was updated successfully, but these errors were encountered:
Ahh, yes. Sorry about that, I misspoke (mistyped?) in that thread.
There are two types of functions called in mustache. There's the normal type, and there is the "lambda" or "higher order section" type. If {{# foo }} resolves to a value, it will be used the normal way. If {{# foo }} resolves to a lambda (in php, a "callable") it will be treated as a higher order section. You're looking for higher order sections here.
... will not do what you're looking for. When the {{# helper.uppercase }} tag is reached, the function uppercase will be called, and its return value is expected to be the context for the section. Since it's called with nothing, its return value is falsey, and the section will be omitted.
This implementation, however, will be treated as a lambda:
A good way to think about it is as a callback. If the function returns something callable, it will be called with the section contents. If it returns a value, the value will be treated as the section context.
Note that you don't have to return anonymous functions, this also works perfectly well:
In this mustache spec discussion - mustache/spec#41 (comment) - an example is given for using an object as a helper with Mustache.php. However I've attempted to recreate that example and it's not working as described.
Here's an example. This DOES work:
However, this does NOT (but should, based on the example):
Has this syntax changed, or is there an issue here? I'm testing this with 2.0.2.
The text was updated successfully, but these errors were encountered: