This is a follow-up of a discussion started here and here
Problem
We currently handle configuration template with rules like these, i.e. have a file with __PATH__
, __PORT__
, etc… at the appropriate places, then the helpers do a search-and-replace of these keywords with their actual values.
This is fine for the majority of apps and historically was simpler to implement, but nevertheless :
- currently we are basically implementing our own template engine, in every helper that need that kind of feature (nginx, phpfpm, systemd, and maybe others already or in the future) ;
- there are historical non-trivial conversion of e.g.
$path_url
to__PATH__
, not to be mistaken for__FINAL_PATH__
which comes from$final_path
… One can only understand those by reading the doc of the helper or reading other people’s template ; - there’s a constrain on which variables can be used in which templates (we only replace a very specific list of keywords depending on the template). Some apps need more variables, and that led to the initial PR which introduces a hack, where instead it would make sense to just be able to use any variable ;
- for element of logics (e.g. adapt the template if
__PATH__
is just/
) we have to put this logic outside the template, and use other tricks to comment/decomment the proper lines instead of having this logic inside the template ;
Proposal
So the proposal is to implement or use a proper template engine, which the other helpers would them rely upon to “render” the template.
Some people already started to use Jinja2, which is kind of “the current standard” (?) for templates as far as I know … Applied to a nginx conf, this gives something like this.
While discussing this, it should be understood that it’s not just a matter of “people can just define their own helper if they want to” : not using the official helpers mean that you won’t benefit from their evolutions in the future (like the automatic transition from php5 to php7 for stretch) and therefore increase the maintenance effort required to keep the app alive.
So here I try to list the pro and cons of using Jinja2 … though of course I am very biased. But at least that should get the discussion (re)started
Pros
- Have a single template engine for all helpers
- Jinja2 is used in many other projects so people can use their knowledge of it (or learn it) when writing apps
- Direct correspondence between the bash variable and the template :
$path_url <-> {{ path_url }}
- Be able to use any variable (as long as you exported it previously)
- Be able to manage the logic of the template inside it (e.g.
if
blocks)
Cons
- Less readable than the
__VAR__
syntax, especially when used in e.g. nginx templates which might include complex regex stuff / rewriting rules… - ???