Skip to content

Commit

Permalink
Merge pull request #55 from roblillack/fix-rendering-non-false-value-…
Browse files Browse the repository at this point in the history
…sections

Fix rendering non-false value sections
  • Loading branch information
cbroglie authored Jun 10, 2021
2 parents c5eea54 + 10eb828 commit 1fb86e2
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
5 changes: 4 additions & 1 deletion mustache.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,10 @@ func renderSection(section *sectionElement, contextChain []interface{}, buf io.W
case reflect.Map, reflect.Struct:
contexts = append(contexts, value)
default:
contexts = append(contexts, context)
// Spec: Non-false sections have their value at the top of context,
// accessible as {{.}} or through the parent context. This gives
// a simple way to display content conditionally if a variable exists.
contexts = append(contexts, value)
}
} else if section.inverted {
contexts = append(contexts, context)
Expand Down
3 changes: 3 additions & 0 deletions mustache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ var tests = []Test{
{"{{#a}}Hi {{.}}{{/a}}", map[string]interface{}{"a": []interface{}{0}}, "Hi 0", nil},
{"{{#a}}Hi {{.}}{{/a}}", map[string]interface{}{"a": [1]interface{}{0}}, "Hi 0", nil},

// non-false section have their value at the top of the context
{"{{#a}}Hi {{.}}{{/a}}", map[string]interface{}{"a": "Rob"}, "Hi Rob", nil},

//section does not exist
{`{{#has}}{{/has}}`, &User{"Mike", 1}, "", nil},

Expand Down
5 changes: 0 additions & 5 deletions spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ var disabledTests = map[string]map[string]struct{}{
"Ampersand Null Interpolation": struct{}{},
"Implicit Iterators - HTML Escaping": struct{}{},
},
// To be fixed by https://github.com/cbroglie/mustache/pull/55
"sections.json": {
"Variable test": struct{}{},
"Deeply Nested Contexts": struct{}{},
},
"~lambdas.json": {}, // not implemented
"~inheritance.json": {}, // not implemented
}
Expand Down

0 comments on commit 1fb86e2

Please sign in to comment.