bobthecow/mustache.php — pull request #101 by bobthecow (2012-07-23 19:46:02+00:00)
Title: Pass a rendering helper to section lambdas

In addition to the section contents, section lambdas (higher order sections) would receive a lambda helper as a second parameter. This helper consists of a single `render` method, allowing a template to be rendered using the current context stack.

The returned result from a section lambda is automatically rendered as a mustache template, which is sufficient most of the time. But if you want to do something with the rendered value of that template, it doesn't quite cut it.

For example, this data:

```php
'win',
'embiggen' => function($text) {
return strtoupper($text);
},
);
```

... and this template:

```html+jinja
{{#embiggen}}{{ foo }}{{/embiggen}}
```

... would uppercase the `{{ FOO }}` template string, rather than the _value_ of foo. This would then be automatically rendered, resulting in an empty string, since there is no `FOO` value available.

With this change, a mustache lambda helper is passed as the second parameter to the anonymous function, giving you a way to render the section value:

```php
'win',
'embiggen' => function($text, $mustache) {
return strtoupper($mustache->render($text));
},
);
```

... which would render as `WIN`, exactly like you'd expect.

In other languages (JavaScript, Ruby), this method would be bound to the closure context and just *be available* inside the anonymous function, but this isn't an option in PHP :)

This change is backwards compatible, as the second value will simply be ignored by any section lambda not referencing it.