Allow `load_data` to take a literal?

I’d like to see what people think about allowing Zola’s built-in load_data function to also take a string literal? To be clear, currently you can do:

{# from path: #}
{% set data = load_data(path="content/blog/story/data.toml") %} 

{# from url: #}
{% set response = load_data(url="https://api.github.com/repos/getzola/zola") %}

However you can not do the following

{# From a literal #}
{% set data = load_data(str='{ "name": "bob", "age": 42 }', format="json") %}

I would find this to be useful, and make Zola more flexible.

For example, if I have a list of “comment” objects, each with timestamp and id fields, I’d find it very useful and flexible to be able to process this list by updating the parent comments with their child’s latest timestamp, for ordering the comments in a certain order.

Of course there are other ways to do this, but they’re not so good in this specific case, compared to something simple as I suggested above.

In general, being able to load objects is useful, which is why we have the ability to do it from the file system. Therefore, being able to load an object from a string literal would be useful much for the same reasons.

If the reason against having this is to discourage creation of objects at runtime, then that is already made possible via the load_data(url) option, where you could submit a POST request, and pass in the data as an argument, to have the server echo it back. So the Pandora’s box of creating objects at runtime has been opened already, in a way, if you understand my meaning.

But what do others think? If the reception is positive, I will do the work to add this functionality.

Ok, well I didn’t hear from anyone, but it was a very simple change to make, so here’s the PR if anyone is interested

I just had to change one file, add some tests, and update documentation, so no sweat if this idea is hated or anything.

cc: @keats

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