News for hackers. Stuff that matters.
Re:M4 as a templating language (2020) by teddyh on Thursday July 29, 2021 @06:58AM (#27995020)
M4 has some well-known uses like Sendmail and GNU Autoconf, but I don’t think this can be counted in M4’s favor: For operating in a standard Unix-like environment, there is really no good alternative to m4 for generic text macro expansion. You can try to force cpp – the C preprocessor – into this, but it is really only suited to being part of a C compiler pipeline. For better or for worse, m4 is it in the world of Unix and shell tools, even though its syntax is quite annoying to use.

Of course, if you operate within a language ecosystem, you can probably find a nice templating system there. Python, for instance, has Jinja2. Or, if your needs are simple, you can use something more basic, like sed(1).

(Repost: https://news.ycombinator.com/item?id=22770406)

Re:M4 as a templating language (2020) by ShrigmaMale on Thursday July 29, 2021 @11:10AM (#27997457)
The Mustache template language has a bash version that is good for simple stuff: https://github.com/tests-always-included/mo
Re:M4 as a templating language (2020) by packetlost on Thursday July 29, 2021 @11:12AM (#27997490)
Seconding `mo`. It's pretty nice
Re:M4 as a templating language (2020) by M3t0r on Thursday July 29, 2021 @08:09AM (#27995485)
There are a couple of tools that try to package Jinja2 (and probably also other templating engines like go-templates) in a CLI friendly way:

- https://github.com/M3t0r/tpl (mine)

- https://github.com/kblomqvist/yasha

- https://github.com/mattrobenolt/jinja2-cli

Re:M4 as a templating language (2020) by ricardobeat on Thursday July 29, 2021 @07:23AM (#27995190)
It seems to me that a combination of sed and simple shell scripts would be much better than this. The syntax is really not friendly.
Re:M4 as a templating language (2020) by WmyEE0UsWAwC2i on Thursday July 29, 2021 @10:03AM (#27996671)
M4 syntax looks that way because it has to work for arbitrary text files.

Quotes can be redefined to an arbitrary string

   changequote(`[[[', `]]]')
And some editor give you syntax highlighting.
Re:M4 as a templating language (2020) by acdha on Thursday July 29, 2021 @12:11PM (#27998121)
> M4 syntax looks that way because it has to work for arbitrary text files.

That seems like an odd assertion given the existence of other templating systems which have friendlier syntax while being capable of generating arbitrary output. Is there a reason why you think on the M4 approach is valid?

Re:M4 as a templating language (2020) by bonzini on Thursday July 29, 2021 @01:58PM (#27999412)
Most templating systems work in combination with an external program written in a high-level language, e.g. Ruby for Liquid or Python for jinja2. m4 does not.
Re:M4 as a templating language (2020) by zbentley on Thursday July 29, 2021 @11:24PM (#28004102)
And m4 works in combination with a bunch of C code. Virtually any system with m4 can come with an interpreter for a higher level language without running out of space (I can't imagine many uses for m4 at runtime on systems where, say, a perl interpreter wouldn't fit). Why is relying on such a runtime a problem?
Re:M4 as a templating language (2020) by bonzini on Friday July 30, 2021 @05:06AM (#28005983)
No, m4 is written in C but it is autonomous. Liquid needs something like Jekyll around it.
Re:M4 as a templating language (2020) by ricardobeat on Friday July 30, 2021 @07:07AM (#28006690)
What does “autonomous” mean? There are a ton of template engines that don’t depend on a parent framework, there is no reason you can’t implement all features in C.

Something like mustache for example, which has a C implementation that includes support for includes/partials: http://mustache.github.io/mustache.5.html

Re:M4 as a templating language (2020) by bonzini on Friday July 30, 2021 @12:09PM (#28009478)
Jinja2 is an API and it needs a Python program to determine the values for the substitution variable and invoke the template engine. In contrast the M4 executable is the engine.
Re:M4 as a templating language (2020) by totony on Friday July 30, 2021 @01:24PM (#28010231)
I use PHP when I need non-trivial templating. Always struck me as odd that people would invent/use their own templating engines (think HCL, jinja, smarty, m4). All of these are implementable as either a PHP file or a haskell interface
Re:M4 as a templating language (2020) by bloopernova on Thursday July 29, 2021 @08:29AM (#27995639)
Can anyone explain how m4 is different to tr?
Re:M4 as a templating language (2020) by timonoko on Thursday July 29, 2021 @08:34AM (#27995680)
I can.

    tr translates letters, m4 translates words and simple expressions.
Re:M4 as a templating language (2020) by fomine3 on Friday July 30, 2021 @01:22AM (#28004748)
envsubst is forgotten one.