I know the landscape is crowded, but I'd like to offer my submission for an Easy To Use / Easy To Maintain template engine:
Introducing Bash-TPL : A Smart, lightweight shell script templating engine, written in Bash
FEATURES
Lightweight
- Single bash script
- Easy to ship with your project or build process
Smart Indentation Tracking
- Removes unwanted whitespace from rendered text files
- Encourages well-formatted mix of template tags and textual content
Generates Reusable Shell Scripts
- Can manage & invoke shell scripts without bash-tpl
- Only need bash-tpl when your source templates change
Shell Agnostic
- Uses standard printf, variable, and sub-shell constructs
- Templates can target any modern shell
Supports Includes
- Can organize your templates as smaller, reusable components
- Indentation tracking even works across includes !
Flexible Delimiter Support
- Can customize all template delimiters
- Can modify delimiters mid-template
- Can include templates with differing delimiters
Quick Example
As is the way: A quintessential hello world example is of course in order:
hello.tpl
Hello, <% ${NAME:-World} %>
compile + run
$ source <( bash-tpl hello.tpl )
Hello, World
compile + run with NAME
$ NAME=TekWizely source <( bash-tpl hello.tpl )
Hello, TekWizely
view compiled template
$ bash-tpl hello.tpl
printf "%s\n" Hello\,\ "${NAME:-World}"
More Information
This hello example is really just the start of what bash-tpl can do.
There's a full README on the Project Home Page.
If you're looking for an easy solution for creating maintainable templates to generate well-formatted text files, I hope you'll give my project a try.
I'm happy to answer any questions or work through any use-cases you might have for templates and see how bash-tpl might help.
Feel free to comment and thanks for looking!
-TW
Top comments (3)
What problem does this solve? Can you give a real-world example of where it would be used?
It seems to me that if you know how to type this:
then you know enough POSIX to be able to write this:
Anything large enough to need templates for output would probably be better written in a more comprehensive scripting language.
Greetings!
Bash-TPL allows you to use the FULL power of the shell scripting language of your choice, so I think we're saying the same thing.
As with any templating language, the problem being solved is enabling you to mix some kind of logic in with a well-formatted textual file (say an HTML file, XML file, apache config, ad-hoc blog template, etc)
Say you have ~100 line config template (maybe an apache vhost template or somesuch), do you really want to be writing 100+
printf
lines ?bash-tpl manages converting your well-formatted and easy to edit text file into printf lines, while allowing you easily inject scripting logic around the file ...
The smart indentation tracking allows you clearly integrate your shell scripting without affecting the final resulting whitespace.
Trivial example:
Say you want:
Given a template:
This will generate the expected output, even though you added whitespace in order to make the for-loop content clearly visible and easier to edit.
I hope that helps a bit - thanks for looking and I'm happy to answer any other questions you might have.
-TW
edit: grammar,typos
Gotcha, the example of generating a configuration file makes perfect sense.