I'm looking for a library or command line script that will allow me to create custom templates that I can generate from the command line. The ruby on rails scaffolding generator is almost identical to what I am trying to do. I would even prefer that it be written in Ruby (yet it cannot require Rails because I may not be using it on a Ruby application). What sorts of scripts like this are already available?
3 Answers
I've also been on the lookout for something like this -- haven't found what I hoped for. The two approaches I've used instead have been acceptable. But I'm still hoping to find the real thing.
obvious, but
sed
for simple use casesfor medium-complexity use cases, if you can assume some version of Python is present on the machine,
string.Template
in the standard library works well. You could write a small Python script that uses this function, and since it is Python, tests / looping etc. that might normally be provided by the template engine could pretty easily be handled in the Python code instead.I've just discovered Mustache (see http://mustache.github.io/). Seems like a solid, purpose-built solution. From its web site, Mustache has implementations in Ruby, JavaScript, Python, Erlang, PHP, Perl, Objective-C, Java, .NET, Android, C++, Go, Lua, ActionScript, ColdFusion, Scala, Clojure, Fantom, CoffeeScript, D, and node.js. If those choices suit your environment, you could script or compile Mustache support to be a command line utility pretty easily.
UPDATE 15-OCT-2013
Now that I've used Mustache for a while -- it's a great tool, simple but powerful.

- 20,650
- 6
- 81
- 80
-
1I was using the ruby mustache cli, but it seems too simple. At least the docs are. – nafg Jul 05 '18 at 22:16
Try this one: https://gomplate.hairyhenderson.ca Rich set of functions and just a stanalone binary (written in Go)

- 81
- 1
- 1
For simple use cases you can use envsubst
fom the gettext
GNU package.
Basically, it reads any (text) file from stdin
, replaces all occurrences of $VARIABLE
or ${VARIABLE}
from environment and writes the result to stdout
. Little more, nothing less.
Full documentation is here.
Pros:
- It is a small binary, no python (or other runtime) required
- It is very fast and has tiny memory footprint
Cons:
- It does just that. No special interpolations and functions (like loops, conditionals etc.)
- It doesn't support special
bash
-like "Parameter Expansion"
For more advanced use cases I recommend j2cli
, a "standalone" Jinja2 templating engine you can install from pip
as it is in Python. Code can be found here (though it looks like a little bit stale...) and documentation is there too.
Pros:
- It's Jinja2 with all bells and whistles!
Cons:
- You need full Python runtime to run it and PIP to install it.
- Computing resources could not be negligible in constrained environments.

- 3,107
- 2
- 22
- 25