Looks like a Tera bug, what am I doing wrong?

Hi All

I am trying aggregate blogs from taxonomies so I can have a list of all related blogs. And I ran into something that looks like a Tera bug, so please, what am I doing wrong?

So, I loop over all categories and add them to W. I use set_global so it is accessible outside of the loop. Here is the apprent bug

I can access W inside the loop (length and iterate)

but outside of loop I can print it with {{W}} and get the right number of objects, but then when trying to do something with it:

Error: Reason: Tried to iterate on a container (`W`) that has a unsupported type

so it is like W changes type as loop finishes.

is it a bug, or more likely, am i just very stupid?

best j

{% set categories = get_taxonomy(kind="category") %}
{% set w=0 %}
{% set W=0 %}
{% for p in page.taxonomies.category %}
    {% for i in categories.items %}
        {% if i.name== p %}
            {% if w==0 %}
                {% set_global w=1 %}
                {% set_global W=i.pages %}
            {%else %}
                {% set_global W= W | concat(with=i.pages) %}
            {%endif%}
            {% set length = W | length %}
            <h1>{{ length}}</h1>
        {% endif %}
    {% endfor %}
{% endfor %}
{{w}}
{{W}}
{% for q in W %}

{% endfor %}

What does {{W}} show? And {{ __tera_context }}?

It looks like the right thing

{{W}} is

[[object], [object], [object], [object], [object], [object], [object], [object], [object], [object], [object], [object]]

and {{ __tera_context }} seems to indicate all is there, it is quite long, here is the start

{ "W": [ { "relative_path": "blog/2022-08-12-review.md", "content": "

but then

    {% for i in W %}
    {% endfor %}

gives

`Error: Reason: Tried to iterate on a container (`W`) that has a unsupported type

Weird, it works in the playground. Do you have a repo somewhere?

I sent you the repo by message

{% set W=0 %}

If you don’t go in the for loop just after, it will try to iterate on the value 0

yes, the
{% set W=0 %}
should not make any difference, as W is set in the loop with set_global, but it does make a difference and if you move the file 2022-07-06-who-is-to-blame.md out of content/blog the site renders without problems.

for me, the signs point to something weird happening with the class/type of W inside the loop.

If i replace it with {% set W=[] %} the site builds correctly so I think it’s just that

1 Like

Thanks @keats

Indeed, my site builds now. I can do a PR for the documentation page if desired, someone may want to do same with taxonomies (and some of the other tricks I came up with)?

Still, while I am not an export in template languages, I think there is something not right about this. Specifically, set_global should set the type/class for the variable, and not depend on it being pre-set like this. It feels like it ends up as something like an array, but not quite, or scope does something weird to the type. Does the Tera variable type correspond to a Rust type?

all the best, j

You must have a page without any page.taxonomies.category so that loop never even gets ran once

1 Like

Ah, so that is the bug, I can see that. Thanks @keats

I have been stupid. Coding in template is different than other languages, and comes with fewer error messages, so I should have been more careful and checked my code better,

best, j