From 4f2dfd207fa2631829ca797f333bab3a38c8b3ad Mon Sep 17 00:00:00 2001 From: Robert Lillack Date: Mon, 7 Jun 2021 23:56:17 +0200 Subject: [PATCH] sections: Fix building context stack for inner elements of sections. cbroglie/mustache#53 --- mustache.go | 2 +- mustache_test.go | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/mustache.go b/mustache.go index d304fdb..2815ca1 100644 --- a/mustache.go +++ b/mustache.go @@ -570,7 +570,7 @@ func renderSection(section *sectionElement, contextChain []interface{}, buf io.W if err != nil { return err } - var context = contextChain[len(contextChain)-1].(reflect.Value) + var context = contextChain[0].(reflect.Value) var contexts = []interface{}{} // if the value is nil, check if it's an inverted section isEmpty := isEmpty(value) diff --git a/mustache_test.go b/mustache_test.go index 836dbe4..09f9f97 100644 --- a/mustache_test.go +++ b/mustache_test.go @@ -179,6 +179,24 @@ var tests = []Test{ "categories": {&Category{"a", "b"}}, }, "a - b", nil}, + {`{{#section}}{{#bool}}{{x}}{{/bool}}{{/section}}`, + map[string]interface{}{ + "x": "broken", + "section": []map[string]interface{}{ + {"x": "working", "bool": true}, + {"x": "nope", "bool": false}, + }, + }, "working", nil}, + + {`{{#section}}{{^bool}}{{x}}{{/bool}}{{/section}}`, + map[string]interface{}{ + "x": "broken", + "section": []map[string]interface{}{ + {"x": "working", "bool": false}, + {"x": "nope", "bool": true}, + }, + }, "working", nil}, + //dotted names(dot notation) {`"{{person.name}}" == "{{#person}}{{name}}{{/person}}"`, map[string]interface{}{"person": map[string]string{"name": "Joe"}}, `"Joe" == "Joe"`, nil}, {`"{{{person.name}}}" == "{{#person}}{{{name}}}{{/person}}"`, map[string]interface{}{"person": map[string]string{"name": "Joe"}}, `"Joe" == "Joe"`, nil},