Allow `load_data` to take a literal?

Thanks for this PR!

is is possible to have some variable rendering in the “literal”?

{% macro print_tags(page) %}
{% set_global tags = [] %}

{% for t in page.taxonomies["tags"] %}
{% set s = t | slugify %}
{% set obj = load_data(literal='{ "slug": "{{s}}", "tag": "{{t}}" }', format="json") %}
{% set_global tags = tags | concat(with=obj) %}
{% endfor %}

{% for tag in tags | sort(attribute="slug") %}
<a href="/tags/{{ tag['slug'] }}" class="tag">{{ tag['tag'] }}</a>{% if not loop.last %}, {% endif %}
{% endfor %}

{% endmacro print_tags %}

gives me:

Error: Reason: Function call 'load_data' failed
Error: Reason: Error("expected `,` or `}`", line: 1, column: 18)

EDIT: I have a typo in my code, fixed but the variable doesn’t render :frowning: (I ended with “{{t}}”)

EDIT2: worked around it!

{% macro print_tags(page) %}
{% set_global tags = [] %}
{% for t in page.taxonomies["tags"] %}
{% set s = t | slugify %}
{% set expanded_obj = '{ "slug": "' ~ s ~ '", "tag": "' ~ t ~ '" }' %}
{% set obj = load_data(literal=expanded_obj, format="json") %}
{% set_global tags = tags | concat(with=obj) %}
{% endfor %}
{% for tag in tags | sort(attribute="slug") %}
<a href="/tags/{{ tag['slug'] }}" class="tag">{{ tag['tag'] }}</a>{% if not loop.last %}, {% endif %}
{% endfor %}
{% endmacro print_tags %}
1 Like