xp-forge / handlebars-templates

Handlebars templates for XP web frontends
1 stars 0 forks source link

Allow passing maps to extensions #10

Closed thekid closed 2 years ago

thekid commented 2 years ago

Current code

The extensions argument had to be an array of web.frontend-helpers.Extension instances.

new Handlebars($this->environment->path('src/main/handlebars'), [
  new Dates(TimeZone::getByName('Europe/Berlin')),
  new class() extends Extension {
    public function helpers() {
      yield 'service' => function($in, $context, $options) {
        return $context->lookup('request')->uri()->base();
      };
    }
  }
]);

New code

We can now substantially shorten the above example:

new Handlebars($this->environment->path('src/main/handlebars'), [
  new Dates(TimeZone::getByName('Europe/Berlin')),
  ['service' => fn($in, $context, $options) => $context->lookup('request')->uri()->base()]
]);
thekid commented 2 years ago

Released in https://github.com/xp-forge/handlebars-templates/releases/tag/v1.2.0