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

Dynamic names #1

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,29 @@ Mustache.render(template, view, {
});
```

Partials can have dynamic names, which begin with `*`. In this case, a value is read from the context and the partial with that value is used.

For example, this template and partial:

base.mustache:
<h2>Message of the Day</h2>
{{> *dayOfWeek}}

thursday.mustache:
<span>This must be Thursday. I never could get the hang of Thursdays.</span>

When loaded with the context:
```json
{
"dayOfWeek": "thursday"
}
```
will be expanded to:
```html
<h2>Message of the Day</h2>
<span>This must be Thursday. I never could get the hang of Thursdays.</span>
```

### Custom Delimiters

Custom delimiters can be used in place of `{{` and `}}` by setting the new values in JavaScript or in templates.
Expand Down
4 changes: 4 additions & 0 deletions mustache.js
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,10 @@ Writer.prototype.renderPartial = function renderPartial (token, context, partial
if (!partials) return;
var tags = this.getConfigTags(config);

// Partial names beginning with an asterix are treated as a dynamic name
if (token[1].trim().substring(0, 1) === '*') {
token[1] = context.lookup(token[1].trim().substring(1).trim());
}
var value = isFunction(partials) ? partials(token[1]) : partials[token[1]];
if (value != null) {
var lineHasNonSpace = token[6];
Expand Down
31 changes: 31 additions & 0 deletions test/mustache-spec-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,44 @@ var skipTests = {
inverted: [
'Standalone Without Newline'
],
interpolation: [
'Dotted Names - Context Precedence'
],
partials: [
'Standalone Without Previous Line',
'Standalone Without Newline'
],
sections: [
'Standalone Without Newline'
],
'~inheritance': [
'Default',
'Variable',
'Triple Mustache',
'Sections',
'Negative Sections',
'Mustache Injection',
'Inherit',
'Overridden content',
'Data does not override block',
'Data does not override block default',
'Overridden parent',
'Two overridden parents',
'Override parent with newlines',
'Inherit indentation',
'Only one override',
'Parent template',
'Recursion',
'Multi-level inheritance',
'Multi-level inheritance, no sub child',
'Text inside parent',
'Block scope',
'Standalone parent',
'Standalone block',
'Block reindentation',
'Intrinsic indentation',
'Nested block reindentation'
],
'~lambdas': [
'Interpolation',
'Interpolation - Expansion',
Expand Down
Loading