summaryrefslogtreecommitdiff
path: root/specs
diff options
context:
space:
mode:
authorGabriel Scherer <gabriel.scherer@gmail.com>2020-12-24 23:06:22 +0100
committerGabriel Scherer <gabriel.scherer@gmail.com>2021-03-09 09:44:11 +0100
commit995959b65f5184477f3dfab26eb75c24237ed435 (patch)
tree5790b663285c46db04c5560b3dc853474daba92d /specs
parent9944f29ab7ccc4a859d96326749872812e4204a8 (diff)
downloadmustache-spec-995959b65f5184477f3dfab26eb75c24237ed435.tar.gz
clarify the behavior of context stacks / parent contexts
The behavior of "context stacks" is not explained very clearly in the documentation outside the spec, but the following text from mustache(5) (http://mustache.github.io/mustache.5.html) is pretty clear: > A {{name}} tag in a basic template will try to find the name key in > the current context. If there is no name key, the parent contexts > will be checked recursively. If the top context is reached and the > name key is still not found, nothing will be rendered. In particular, when the documentation of section mentions that > When the value is a non-empty list [..]. The context of the block > will be set to the current item for each iteration. or > When the value is non-false but not a list, it will be used as the > context for a single rendering of the block. it should be understood that the new context becomes the "current context", and that the previous context becomes a "parent context", and its binding are still available. The new tests check that these properties hold, in particular for sections on non-object non-false values, where this was not previously tested in the spec.
Diffstat (limited to 'specs')
-rw-r--r--specs/sections.yml27
1 files changed, 22 insertions, 5 deletions
diff --git a/specs/sections.yml b/specs/sections.yml
index 30815c9..d204bea 100644
--- a/specs/sections.yml
+++ b/specs/sections.yml
@@ -53,14 +53,27 @@ tests:
template: '"{{#context}}Hi {{name}}.{{/context}}"'
expected: '"Hi Joe."'
+ - name: Parent contexts
+ desc: Names missing in the current context are looked up in the stack.
+ data: { a: "foo", b: "wrong", sec: { b: "bar" }, c : { d : "baz" } }
+ template: '"{{#sec}}{{a}}, {{b}}, {{c.d}}{{/sec}}"'
+ expected: '"foo, bar, baz"'
+
+ - name: Variable test
+ desc: |
+ 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.
+ data: { foo: "bar" }
+ template: '"{{#foo}} {{.}} is {{foo}} {{/foo}}"'
+ expected: '"bar is bar"'
+
- name: Deeply Nested Contexts
desc: All elements on the context stack should be accessible.
data:
a: { one: 1 }
b: { two: 2 }
- c: { three: 3 }
- d: { four: 4 }
- e: { five: 5 }
+ c: { three: 3, d : { four : 4, five : 5 } }
template: |
{{#a}}
{{one}}
@@ -70,9 +83,11 @@ tests:
{{one}}{{two}}{{three}}{{two}}{{one}}
{{#d}}
{{one}}{{two}}{{three}}{{four}}{{three}}{{two}}{{one}}
- {{#e}}
+ {{#five}}
+ {{one}}{{two}}{{three}}{{four}}{{five}}{{four}}{{three}}{{two}}{{one}}
+ {{one}}{{two}}{{three}}{{four}}{{.}}6{{.}}{{four}}{{three}}{{two}}{{one}}
{{one}}{{two}}{{three}}{{four}}{{five}}{{four}}{{three}}{{two}}{{one}}
- {{/e}}
+ {{/five}}
{{one}}{{two}}{{three}}{{four}}{{three}}{{two}}{{one}}
{{/d}}
{{one}}{{two}}{{three}}{{two}}{{one}}
@@ -87,6 +102,8 @@ tests:
12321
1234321
123454321
+ 12345654321
+ 123454321
1234321
12321
121