Skip to content

Commit

Permalink
Implement template inheritance (~ partials with parameters)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
gasche committed Jan 5, 2021
1 parent 7d6b3fb commit b7e36c3
Show file tree
Hide file tree
Showing 13 changed files with 383 additions and 112 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
### 3.2.0

* Support for "template inheritance" (partials with parameters)
`{{<foo}} {{$param1}}...{{/param1}} {{$param2}}...{{/param2}} {{/foo}`
following the widely-implemented semi-official specification
https://github.com/mustache/spec/pull/75
(@gasche, 58)
* Partials are now supported in the `mustache` command-line tool (@gasche, #57)
They are interpreted as template inclusion: "{{>foo/bar}}" will include
"foo/bar.mustache", relative to the current working directory.
Expand Down
21 changes: 18 additions & 3 deletions bin/test/errors/parsing-errors.t
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Delimiter problems:
$ PROBLEM=eof-before-section-end.mustache
$ echo "{{#foo}} {{.}} {{/" > $PROBLEM
$ mustache foo.json $PROBLEM
File "eof-before-section-end.mustache", line 2, character 0: ident expected.
File "eof-before-section-end.mustache", line 2, character 0: '}}' expected.
[3]
$ PROBLEM=eof-before-inverted-section.mustache
Expand Down Expand Up @@ -84,7 +84,7 @@ Mismatch between section-start and section-end:
$ echo "{{#foo}} {{.}} {{/bar}}" > $PROBLEM
$ mustache foo.json $PROBLEM
File "foo-bar.mustache", line 1, characters 0-23:
Section mismatch: {{#foo}} is closed by {{/bar}}.
Open/close tag mismatch: {{# foo }} is closed by {{/ bar }}.
[3]
$ PROBLEM=foo-not-closed.mustache
Expand All @@ -97,9 +97,24 @@ Mismatch between section-start and section-end:
$ echo "{{#bar}} {{#foo}} {{.}} {{/bar}} {{/foo}}" > $PROBLEM
$ mustache foo.json $PROBLEM
File "wrong-nesting.mustache", line 1, characters 9-32:
Section mismatch: {{#foo}} is closed by {{/bar}}.
Open/close tag mismatch: {{# foo }} is closed by {{/ bar }}.
[3]
$ PROBLEM=wrong-nesting-variable.mustache
$ echo '{{#bar}} {{$foo}} {{.}} {{/bar}} {{/foo}}' > $PROBLEM
$ mustache foo.json $PROBLEM
File "wrong-nesting-variable.mustache", line 1, characters 9-32:
Open/close tag mismatch: {{$ foo }} is closed by {{/ bar }}.
[3]
$ PROBLEM=wrong-nesting-partial.mustache
$ echo "{{#foo}} {{<foo-bar}} {{/foo}} {{/foo-bar}}" > $PROBLEM
$ mustache foo.json $PROBLEM
File "wrong-nesting-partial.mustache", line 1, characters 9-30:
Open/close tag mismatch: {{< foo-bar }} is closed by {{/ foo }}.
[3]
Weird cases that may confuse our lexer or parser:
Expand Down
6 changes: 6 additions & 0 deletions bin/test/inheritance.t/base.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
{{$header}}{{/header}}
<body>
{{$content}}{{/content}}
</body>
</html>
3 changes: 3 additions & 0 deletions bin/test/inheritance.t/header.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<head>
<title>{{$title}}Default title{{/title}}</title>
</head>
10 changes: 10 additions & 0 deletions bin/test/inheritance.t/mypage.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{{<base}}
{{$header}}
{{<header}}
{{$title}}My page title{{/title}}
{{/header}}
{{/header}}
{{$content}}
<h1>Hello world</h1>
{{/content}}
{{/base}}
14 changes: 14 additions & 0 deletions bin/test/inheritance.t/run.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
$ echo "{}" > data.json

This test is the reference example from the template-inheritance specification:
https://github.com/mustache/spec/pull/75

$ mustache data.json mypage.mustache
<html>
<head>
<title>My page title</title>
</head>
<body>
<h1>Hello world</h1>
</body>
</html>
2 changes: 1 addition & 1 deletion dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ Contains the `mustache` command line utility for driving logic-less templates.
(ezjsonm :with-test)
(menhir (>= 20180703))
(cmdliner (>= 1.0.4))
(ocaml (>= 4.06))))
(ocaml (>= 4.08))))
Loading

0 comments on commit b7e36c3

Please sign in to comment.