3 ms·
I think the main difference is that a templating language is explicit in what causes an interpolation, while a macro language is implicit. For example, compare
by jolmg 8y ago
I think the main difference is that a templating language is explicit in what causes an interpolation, while a macro language is implicit. For example, compare this use of the m4 macro language:
$ m4 << EOF
> define(name, Jane)dnl
> hello name
> EOF
hello Jane
with the equivalent using the erb templating language:
$ erb -T- << EOF
> <% name = "Jane" -%>
> hello <%= name %>
> EOF
hello Jane
In the m4 example, Jane, hello, or name might even be another macro, and you'd need to know that to know what the result will be:
$ m4 << EOF
> define(name, nombre)dnl
> define(hello, hola)dnl
> define(Jane, Joe)dnl
> define(name, Jane)dnl
> hello nombre
> EOF
hola Joe
Imagine you source those first 3 lines from elsewhere, a file serving as a library. Things can get pretty confusing if conventions aren't established and followed. You'd need to explicit in what you don't want to interpolate to get the same understandability as a templating language:
$ m4 << EOF
> define(\`name', \`nombre')dnl
> define(\`hello', \`hola')dnl
> define(\`Jane', \`Joe')dnl
> define(\`name', \`Jane')dnl
> \`hello 'name
> EOF
hello Jane
By quoting like this, you can look at any single line and know what's going on. name is not quoted in the last line so it "must" (mandated only by convention) be interpolated. "name" and "Jane" are quoted in the second-to-last line, so they're not interpolated, and they can only mean that "name" will be substituted by "Jane". This convention offers the same benefits of a templating language, only it's more burdensome and error prone.
Now, as to what is better? I think templating languages are better for modifications of documents based on variables, like making the rows of an HTML table correspond with a listing of data. The only good use-case I can think of for macro languages is extensions of languages. Like making mini-compilers (or do they call them transpilers nowadays?) by writing m4 scripts. That's the only time I think it'd be better to have implicit interpolation, when you have more interpolations than not, and the document source language is generally understood to be something far different than the target language.
This means that, since the majority of the time CPP (a macro language) is generally used for interpolation of data or code in what is generally understood to be C code as source and target language, I think it would have been better designed as a templating language. That way, you wouldn't have people joking about doing things like:
#define TRUE FALSE
which would have no ill effect in a templating language.
On second thought, however, C being what it is (a low-level language with inflexible syntax and semantics), I can see that the intention of CPP was indeed probably to write extensions to the language, which would make it suitable to be a macro language.