Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Bash Template Engine

A small, dependency-free template engine written entirely in Bash inspired by Jinja/Twig.

Installation

Copy template into your project or clone the repository.

Syntax

{{ expression }}  Output
{% statement %}   Control statement
{# comment #}     Template comment

Supported statements

{% if variable %}
{% elif variable %}
{% else %}
{% endif %}

{% if [[ "$user" == "Max" ]] %}
{% elif [ "$user" = "Jo" ] %}
{% elif (( 2 + 2 == 4 )) %}

{% for user in users %}
{% endfor %}

{% raw %}
{% endraw %}

Expressions

{{ user }}
{{ users[0] }}
{{ user | tr 'a-z' 'A-Z' }}

WARNING

you can do:

{{ use | rm -rf / }}

so templates must be trusted code.

Loop variables

{{ loop.index }}
{{ loop.index0 }}
{{ loop.revindex }}
{{ loop.revindex0 }}
{{ loop.first }}
{{ loop.last }}
{{ loop.length }}
{{ loop.parent.XYZ }}

Whitespace control

suppresses the newline and whitespaces after or before the tag (or both)

{{ user -}}
{% if enabled -%}
{# comment -#}
{{- user }}
{%- if enabled %}
{#- comment #}

suppresses the only whitespaces after or before the tag (or both)

{{ user ~}}
{% if enabled ~%}
{# comment ~#}
{{~ user }}
{%~ if enabled %}
{#~ comment #}

Usage

source ./template
template file.tmpl

Or:

./template file.tmpl

IMPORTANT

Templates are trusted code.

Conditions and pipes execute Bash commands.

Not (jet) implemented featurs

{% set x="OK" %}
{{ user.name }} for associative arrays
{% break %} and {% continue %} in for loop
{{ user | default("gast") }} or a similar feature for default value

Test

Run test with:

docker run -it -v "$PWD:/code" bats/bats:latest /code/test

Contributing

Contributions are welcome! Please open an issue or submit a pull request with any enhancements or bug fixes.

License

This project is licensed under the MIT License. See the LICENSE file for more information.

About

A small, dependency-free template engine written entirely in Bash inspired by Jinja/Twig.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages