Lobster, a simple template engine for MATLAB
Published in Matlab
Lobster is a template engine for MATLAB with a syntax quite similar to Jinja, a template engine implemented in python. A template engine is useful if you need to create documents with a repetitive syntax. I use them for example to
- generate C++ to convert enumeration fields to and from strings,
- generate HTML content
- generate latex contents from MATLAB variables.
Let's start with a simple example of the syntax. Create a file called template.tpl with the following contents
Welcome {{ FirstName }},
Your grocery list contains:
{% for item in groceries %}
- {{ item.name }} {% if item.quantity > 1 %}(x{{ item.quantity}}){% endif %}
{% endfor %}
If you've downloaded Lobster and added it to your path then using the template is as simple as:
context = struct( ...
'FirstName', 'Thibaud', ...
'Groceries', {'Milk', 'Eggs', 'Bacon', 'Nutella'} ...
);
template = Lobster('template.tpl').render(context);
The rendered template is stored in the template variable. What you do with it is up to you. The full documentation can be found in the github repo.