Skip to content

dpranke/templating

Repository files navigation

Templating Engines

Sample data

There is a sample data file in t.json:

$ cat t.json
{
  "t": "rules",
  "rules": [
    {
      "t": "rule",
      "name": "grammar",
      "child": {
        "t": "seq",
        "ch": [
          {
            "t": "label",
            "name": "f"
          },
          {
            "t": "apply",
            "rule_name": "end"
          },
          {
            "t": "action",
            "child": {
              "t": "e_var",
              "name": "f"
            }
          }
        ]
      }
    }
  ]
}

Mustache and Handlebars

Mustache

Mustache is the simplest templating engine, and claims to have been ported to a number of languages. However, I could not get the default javascript implementation to work.

There are several implementations for Python; it doesn't appear that any of them are very maintained and/or production-ready. I had the best luck with combustache:

$ cat t.mustache
Hello, world.
{{#rules}}
Rule {{name}}
{{#child}}
{{#ch}}
  Node {{t}}
{{/ch}}
{{/child}}
{{/rules}}
$ combustache -d t.json t.mustache 
Hello, world.
Rule grammar
 Node label
 Node apply
 Node action
$

Handlebars

Handlebars is an extended form of Mustache w/ more features. The default implementation is JavaScript (I think?), but I could not figure out how to get a JS-based CLI tool to work.

There are multiple incomplete Python ports; looks like pybars3 might be the best. pybars3 does not have a CLI, so I wrote one in //pybars-cli.

$ ./pybars-cli t.json t.mustache
Hello, world.
Rule grammar
  Node label
  Node apply
  Node action
$

Go and text/template

Go provides a full-featured template engine in the text/template package in the Go standard library. It does not provide a command-line tool as part of it. I've tried two different other packages as tools: Gomplate and go-cli-template. Both have clunky interfaces.

$ cat t.go_template
Hello, world.
{{ range .rules -}}
Rule {{ .name }}
{{- range .child.ch }}
  Node {{ .t }}
{{- end -}}
{{- end }}
$ $HOME/go/bin/gomplate -c .=t.json -f t.go_template
Hello, world.
Rule grammar
  Node label
  Node apply
  Node action
$ cat t.json | ./tpl -f t.go_template
Hello, world.
Rule grammar
  Node label
  Node apply
  Node action
$

Java and StringTemplate

About

Notes and code for various templating engines

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors