Skip to content

Commit 2b9fa8c

Browse files
committed
Implement template inheritance (~ partials with parameters)
The implementation follows the spec proposal: mustache/spec#75 which is supported by at least the following Mustache implementations: - hogan.js - mustache.php - mustache.java - GRMustache (Obj-C) and GRMustache.swift (Swift) - Text::Caml (Perl) - hxmustache (Haxe) - MuttonChop (Swift)
1 parent 6c39f69 commit 2b9fa8c

File tree

13 files changed

+385
-112
lines changed

13 files changed

+385
-112
lines changed

CHANGES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
### 3.2.0
22

3+
* Support for "template inheritance" (partials with parameters)
4+
`{{<foo}} {{$param1}}...{{/param1}} {{$param2}}...{{/param2}} {{/foo}`
5+
following the widely-implemented semi-official specification
6+
https://github.com/mustache/spec/pull/75
7+
(@gasche, 58)
38
* Partials are now supported in the `mustache` command-line tool (@gasche, #57)
49
They are interpreted as template inclusion: "{{>foo/bar}}" will include
510
"foo/bar.mustache", relative to the current working directory.

bin/test/errors/parsing-errors.t

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Delimiter problems:
3333
$ echo "{{#foo}} {{.}} {{/" > $PROBLEM
3434
$ mustache foo.json $PROBLEM
3535
Template parse error:
36-
File "eof-before-section-end.mustache", line 2, character 0: ident expected.
36+
File "eof-before-section-end.mustache", line 2, character 0: '}}' expected.
3737
[3]
3838
3939
$ PROBLEM=eof-before-inverted-section.mustache
@@ -97,7 +97,7 @@ Mismatch between section-start and section-end:
9797
$ mustache foo.json $PROBLEM
9898
Template parse error:
9999
File "foo-bar.mustache", line 1, characters 0-23:
100-
Section mismatch: {{#foo}} is closed by {{/bar}}.
100+
Open/close tag mismatch: {{# foo }} is closed by {{/ bar }}.
101101
[3]
102102
103103
$ PROBLEM=foo-not-closed.mustache
@@ -112,9 +112,26 @@ Mismatch between section-start and section-end:
112112
$ mustache foo.json $PROBLEM
113113
Template parse error:
114114
File "wrong-nesting.mustache", line 1, characters 9-32:
115-
Section mismatch: {{#foo}} is closed by {{/bar}}.
115+
Open/close tag mismatch: {{# foo }} is closed by {{/ bar }}.
116116
[3]
117117
118+
$ PROBLEM=wrong-nesting-variable.mustache
119+
$ echo '{{#bar}} {{$foo}} {{.}} {{/bar}} {{/foo}}' > $PROBLEM
120+
$ mustache foo.json $PROBLEM
121+
Template parse error:
122+
File "wrong-nesting-variable.mustache", line 1, characters 9-32:
123+
Open/close tag mismatch: {{$ foo }} is closed by {{/ bar }}.
124+
[3]
125+
126+
$ PROBLEM=wrong-nesting-partial.mustache
127+
$ echo "{{#foo}} {{<foo-bar}} {{/foo}} {{/foo-bar}}" > $PROBLEM
128+
$ mustache foo.json $PROBLEM
129+
Template parse error:
130+
File "wrong-nesting-partial.mustache", line 1, characters 9-30:
131+
Open/close tag mismatch: {{< foo-bar }} is closed by {{/ foo }}.
132+
[3]
133+
134+
118135
119136
Weird cases that may confuse our lexer or parser:
120137
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<html>
2+
{{$header}}{{/header}}
3+
<body>
4+
{{$content}}{{/content}}
5+
</body>
6+
</html>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<head>
2+
<title>{{$title}}Default title{{/title}}</title>
3+
</head>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{{<base}}
2+
{{$header}}
3+
{{<header}}
4+
{{$title}}My page title{{/title}}
5+
{{/header}}
6+
{{/header}}
7+
{{$content}}
8+
<h1>Hello world</h1>
9+
{{/content}}
10+
{{/base}}

bin/test/inheritance.t/run.t

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
$ echo "{}" > data.json
2+
3+
This test is the reference example from the template-inheritance specification:
4+
https://github.com/mustache/spec/pull/75
5+
6+
$ mustache data.json mypage.mustache
7+
<html>
8+
<head>
9+
<title>My page title</title>
10+
</head>
11+
<body>
12+
<h1>Hello world</h1>
13+
</body>
14+
</html>

dune-project

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ Contains the `mustache` command line utility for driving logic-less templates.
2828
(ounit :with-test)
2929
(menhir (>= 20180703))
3030
(cmdliner (>= 1.0.4))
31-
(ocaml (>= 4.06))))
31+
(ocaml (>= 4.08))))

0 commit comments

Comments
 (0)