Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decorators #1082

Merged
merged 5 commits into from
Sep 1, 2015
Merged

Decorators #1082

merged 5 commits into from
Sep 1, 2015

Conversation

kpdecker
Copy link
Collaborator

This is intended for merging into master but also dependent on #1076 so PRing against that branch for discussion right now.

This implements inline partials in the form of:

{{#*inline "foo"}}bar{{/inline}}
{{> foo }}

Which exposes the foo template to the current block and all children. This also adds a special case for partial blocks to support template inheritance:

{{#> layout}}
  {{#*inline "content"}}bar{{/inline}}
{{/layout}}

Which exposes the content partial to the layout partial, allowing for overrides and other conditional behaviors.

This is implemented through the more generic decorators feature, which are loosely inspired by @wycats's javascript decorators proposal. These allow a declarative means to both annotate particular blocks with metadata as well as to wrap in behaviors when desired, prior to execution. In addition to the inline partial helper case, this allows for blocks to be flagged with metadata, using this example:

{{#grid data}}
  {{#*column "Column 1"}}{{foo}}{{/column}}
  {{#*column "Column 2"}}{{bar}}{{/column}}
{{/grid}}
Handlebars.registerDecorator('column', function(fn, props, container, options) {
  props.columns = props.columns || [];
  props.columns.push({key: options.args[0], body: options.fn});
});
Handlebars.registerHelper('grid', function(data, options) {
  var headers = options.fn.columns.map(function(column) { return column.key; }).join(',');
  return headers + '\n' + data.map(function(data) {
    return options.fn.columns.map(function(column) { return column.body(data); }).join(',');
  }).join('\n');
});

/cc @wycats, @mmun, @ErisDS

These allow for a given block to be wrapped in helper methods or metadata and allow for more control over the current container and method before the code is run.
Allows for partials to be defined within the current template to allow for localized code reuse as well as for conditional behavior within nested partials.

Fixes #1018
@kpdecker kpdecker added this to the 4.0 milestone Aug 22, 2015
@kpdecker kpdecker mentioned this pull request Aug 22, 2015
kpdecker added a commit that referenced this pull request Sep 1, 2015
@kpdecker kpdecker merged commit 1aae321 into partial-block Sep 1, 2015
@kpdecker kpdecker deleted the decorators branch September 1, 2015 03:22
@kpdecker
Copy link
Collaborator Author

kpdecker commented Sep 1, 2015

Released in 4.0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant