Sometimes I need to use same functionality across multiple tasks. It would be great if I could define this functionality somehow and then reference it in my tasks.
For example, if Mechanic supports snippets, I could define a snippet like so:
{% comment %}
# snippets/calc-a-plus-b.liquid
Example calculation snippet.
@param {number} a
@param {number} b
@returns {number}
{% endcomment %}
{% if a == nil %}
{% error %}
{"message": "param a was not defined"}
{% enderror %}
{% endif %}
{% if b == nil %}
{% error %}
{"message": "param b was not defined"}
{% enderror %}
{% endif %}
{{- a | plus: b -}}
And then in my task, if I did this:
{% capture result -%}
{%- render 'calc-a-plus-b' a: 1, b: 2 -%}
{%- endcapture %}
{% log result %}
# -> 3
Ideally snippets could have a built-in optional param of
output_type
where by the output could be automatically converted to the type, e.g.
output_type: "string"
-> default behaviour, just returns string (with automatic
strip
applied)
output_type: "number"
-> would be same as
{% assign result = result | plus: 0 %}
output_type: "parse_json"
-> would be same as
{% assign result = result | parse_json %}