Crates.io | terender |
lib.rs | terender |
version | 0.1.3 |
source | src |
created_at | 2020-11-12 07:04:10.203188 |
updated_at | 2020-11-17 14:19:19.52657 |
description | Render tera templates from data files |
homepage | |
repository | https://gitlab.com/silwol/terender/ |
max_upload_size | |
id | 311486 |
size | 47,330 |
terender
- Render tera templates from data filesterender
is a simple command-line tool for rendering template files in
tera template format simply using structured
input data from a file.
Currently supported file formats for the structured data are JSON, TOML and YAML. It does detect these only based on the file name extension, no inspection of the contents is done. For now, the file extension must be lowercase. Once rust-lang #70516 is in the stable compiler, I will use that to make the detection case-insensitive. If a file does contain data that can't be parsed, the tool will abort with an error message. If you're interested in adding a new format that is supported by serde, please either submit an issue or create a merge request.
For information on how to write tera
templates, take a look at their
documentation.
A short usage example for transforming from a simple markdown template file with some json data into the rendered markdown.
Contents of products.template.md
:
# Our products
{% for category in categories %}
## {{ category.name }} ({{ category.articles | length }} articles)
{% for article in category.articles -%}
* {{ article }}
{% endfor -%}
{% endfor -%}
Contents of products.json
:
{
"categories": [
{
"name": "Sports",
"articles": [
"Training shoes",
"Bicycle"
]
},
{
"name": "Electronics",
"articles": [
"Mobile phone",
"USB memory stick",
"E-Book reader"
]
},
{
"name": "Food",
"articles": [
"Cheese",
"Apple",
"Milk"
]
}
]
}
Running terender
:
$ terender products.template.md products.json
# Our products
## Sports (2 articles)
* Training shoes
* Bicycle
## Electronics (3 articles)
* Mobile phone
* USB memory stick
* E-Book reader
## Food (3 articles)
* Cheese
* Apple
* Milk
$
You need a working installation of the rust development environment, namely
rustc
and cargo
. These can either be obtained from your operating
system if it provides it (e.g. apt install rustc
on Debian-based
operating systems), or through https://rustup.rs.
The typical command for installation is:
cargo install terender
If you already have a version of the tool installed and want to overwrite
it, you need to add the -f
parameter:
cargo install -f terender
The tera
template rendering is only capable of rendering entities that
represent objects with named fields (e.g. JSON object, or YAML mmappings).
If a data input file contains something else at top-level, such as an
array / list / sequence, or a primitive type, that entity gets wrapped into
an object containing a single field named data
in order to allow
processing of that input.
[
"hello",
"world"
]
would be changed to
{
"data": [
"hello",
"world"
]
}
before processing.
In case you would like to use multiple data files in a rendering, you can
do so. If you pass multiple data file parameters, these will be wrapped
into a top-level object containing a data
field which is an array
enclosing all the data files.
If passing these two files to the process:
# This is file1.toml
[details]
version = "1.0"
{
"information": "this is file2.json"
}
the data passed to the template would look like this (in JSON):
{
"data": [
{
"details": {
"version": "1.0"
}
},
{
"information": "this is file2.json"
}
]
}
This tool is just a very small implementation plumbing what already exists. The heavy lifting is done in library crates used by this tool:
It is all because of these excellent implementations that this crate can be short and easily implemented.
If you'd like to package this tool for an operating system distribution, feel free to do so. I'd be happy if you inform me when doing so, but it's not mandatory.
If you would like to deploy shell completion scripts with your package,
these can be generated by setting the TERENDER_GENERATE_SHELL_COMPLETIONS
environment variable to a relative or absolute directory path where the
completion files for all shells supported by
clap are generated.
data
field.