# EOTK Templates The EOTK template engine is an enormous (but reasonably clean) kludge for automating repetitive tasks in template-building; it started off as a small Perl hack (because existing templaters really sucked, or required YAML or other such nonsense) and then grew to add functionality in a mostly-considered, mostly-orthogonal way. For further examples, see also the unit tests in `lib.d/test-expand-template.sh`. ## Variables The templater reads/uses variables from three places: * The Unix Process Environment (lowest priority) * Space-separated columnar input from STDIN * Local "Range" or builtin variables (highest priority) Attempts to interpolate a nonexistent (rather than empty) variable *anywhere* are a fatal error. ### Environment A simple example of the first case: ``` $ echo $USER $SHELL TERM alecm /bin/bash TERM $ cat > foo.template Hello %USER% using %SHELL% in %TERM% $ ./lib.d/expand-template.pl foo.template `Foo%Bar` ## Control Statements There are simple conditionals: ``` %%IF %BOOLEAN% %%ENDIF %%IF %BOOLEAN% %%ELSE %%ENDIF ``` ...and simple conditional operations: ``` $ cat foo.template %%IF %HOME% contains /Users/ you are probably on a Mac (%HOME%) %%ELSE you are probably NOT on a Mac (%HOME%) %%ENDIF $ ./lib.d/expand-template.pl foo.template =` * `<=` * `>` * `<` ### String Operators eg: `%%IF %ONION% eq facebookcorewwwi` * `eq` * `ne` * `ge` * `le` * `gt` * `lt` * `contains` * `!contains` ### Logic Operators eg: `%%IF %BOOL1% and %BOOL2%` - no subexpressions, sorry * `and` * `or` * `xor` Also, simple boolean conditionals may use `!` or `not` to invert the sense of an if-statement, so this is valid: ``` %%IF not %VALID% Where VALID is boolean-evaluatable like 0 or 1 ... %%ENDIF ``` ...but the following is *invalid* because there is no expression parser: ``` %%IF ! %FOO% eq BAR *THIS WILL NOT WORK AS INTENDED* *YOU WANT: %FOO% ne BAR* ... %%ENDIF ``` ### Operator Notes If you are at risk of empty strings for string comparisons, disambiguate the string interpolation with quotes, thusly: ``` %%IF "%FOO%" eq "%BAR%" ``` ...and please remember that the whole thing will explode messily (and the logic will possibly change) if %FOO% contains a whitespace character. ## Relevance to EOTK Config-File Format Variables are set in a config file as: ``` set foo bar baz ``` This will result in the (uppercase) Environment Variable `FOO` being set to the string `bar baz`, and then being available to the templater as `%FOO%`. Therefore it is entirely possible (though dangerous) to do this: ``` set path /usr/bin:/bin:... ``` ...and set the `$PATH` environment variable for the rest of template generation. Doing so will probably break everything, so on your head be it. *IMPORTANT* - all variables, excepting `project`, are retroactively global in scope; even if you set them at the **bottom** of a config file, they impact the projects at the **top**. For clarity, keep all globals at the top, and if you have projects which need different settings then use different config files and different runs of `eotk configure`. # Variables ## Template Variables A list of template variables - and their default values - is provided in [the example template configuration file](../demo.d/example.tconf) ## Fake Variables These are used in Template Configuration (`.tconf`) files, and do not represent real environment variables. * NEW_ONION Used only in template configs (`*.tconf` files) to show the point where a newly created onion address should be inserted.