News for hackers. Stuff that matters.

Ask HN: General Purpose Templating Engine

The title attempts to distill the essence of the question, however, I suspect my lack of terminology in this area may make things more difficult.

What I'm looking for is a standalone general purpose templating engine. I envision this to meet the following criteria

- Available as a single binary with a CLI interface

- Ingests common data formats (i.e. JSON, etc.)

- Exports textual data of any format

The primary use-case is not generating HTML, but rather various textual data with differing structural requirements. It's very hard to search in this space because you're inevitably met with Javascript solutions aiming to template HTML. I'd prefer not to be tied to a JS runtime and the idiosyncrasies of trying to template HTML.

( 8 comments | 3 points | on HN )
Re:Ask HN: General Purpose Templating Engine by necovek on Tuesday May 24, 2022 @12:51PM (#31494513)
Most templating engines can produce any format even if their directives are XML-like.

Taking input from JSON is probably rarer because all of them are programming language focused.

For CLI, you could combine it with things like jq (there was also something else that allowed using css selectors on JSON) and other tools like awk.

Or you could simply use PHP for a built in templating language (i.e. put <? ?> statements into whatever your file format is).

Re:Ask HN: General Purpose Templating Engine by linkdd on Tuesday May 24, 2022 @04:42PM (#31497339)
I'm not sure if it fits your description, but a while ago me and my associate made https://github.com/link-society/gotempl
Re:Ask HN: General Purpose Templating Engine by ThePhysicist on Tuesday May 24, 2022 @02:11PM (#31495547)
Jinja2 started out as a general template engine, although most people use it to generate HTML these days. Not a single binary but really powerful and very easy to customize and extend.
Re:Ask HN: General Purpose Templating Engine by hidden_sheepman on Wednesday May 25, 2022 @03:51AM (#31501699)
You could looking to using: http://template-toolkit.org/
Re:Ask HN: General Purpose Templating Engine by EddieDante on Tuesday May 24, 2022 @02:11PM (#31495539)
jq was made specifically to process JSON, and is a CLI tool.

If you're familiar with UNIX tools you could probably do a lot with the shell, sed, and awk.

Knowing Perl might be useful in this situation, too, and it's an old and stable language with a huge library of modules (CPAN).

Re:Ask HN: General Purpose Templating Engine by verdverm on Tuesday May 24, 2022 @01:50PM (#31495275)
https://docs.hofstadter.io

Built to generate anything, works on directories of files for input / output. CUE is the primary input, which means JSON & Yaml are also supported. It also supports writing custom code in the output, changing your designs and regenerating, while not losing the custom parts.

Re:Ask HN: General Purpose Templating Engine by aliasxneo on Tuesday May 24, 2022 @02:26PM (#31495763)
This seems promising, but a bit bloated for my use-case. For example, it's not apparent that it would be simple to go from a JSON input to say a NGINX configuration output.
Re:Ask HN: General Purpose Templating Engine by verdverm on Tuesday May 24, 2022 @02:44PM (#31495973)
Indeed, I need a one liner example. Happy to help you through that in Slack