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?
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.
Try this one: https://gomplate.hairyhenderson.ca Rich set of functions and just a stanalone binary (written in Go)
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.
You can use ESH – a simple templating engine based on shell. It’s written ~290 LoC of POSIX shell and awk.
Example from the project’s readme:
http {
access_log <%= $logs_dir/access.log %> main;
resolver <%= $(sed -En 's/^nameserver ([^#]+)/\1/p' /etc/resolv.conf) %>;
<% if nginx -V 2>&1 | grep -q lua-nginx-module; then -%>
lua_package_path '<%= $(pkg-config --variable=INSTALL_LMOD lua) %>/?.lua';
<% fi -%>
<%+ ./http-common.esh %>
<%# The rest of the config is omitted %>
}
Actually the Ruby on Rails generators are based on a library called Thor. It's very useful for creating custom CLI tools for generating code based on templates.
© 2022 - 2024 — McMap. All rights reserved.