Here is a debuging macro to view easy to read `__tera_context`

I created a short script that displays the same stuff as {{ __tera_context }}, but each variable is in it’s own accordion <details> element. It also put page content and sub-pages in their own <details>. It’s easier to quickly view if you have messed something up or forget the structure of something. Extremely useful for neophytes just starting to learn Tera.

Any suggestions to make it better are welcome. It shows the contents of the __tera_context_ string by individually shows the contest of config, section or page, current_path, current_url, lang. I’d rather the last 3 be combined in one drop-down, but I believe we cannot create arrays or objects with Tera.


{% macro show_tera_context() %}
    {{ self::show(name="Config", content=config) }}
    {% if section %}{{ self::show(name="Section", content=section) }}{% endif %}
    {% if page %}{{ self::show(name="Page", content=page) }}{% endif %}
    {{ self::show(name="current_path", content=current_path) }}
    {{ self::show(name="current_url", content=current_url) }}
    {{ self::show(name="lang", content=lang) }}
{% endmacro show_tera_context %}


{% macro show(name, content) %}
    <details style="text-align: left;">
        <summary><strong>{{ name }}</strong></summary>
        {{ self::show_value(type="",data=content) }}
    </details>
{% endmacro show %}


{% macro show_array(data) %}
    <ul>
    {% for value in data %}
        <li><strong>{{ loop.index }}</strong>: {{ self::show_value(type="",data=value) }}</li>
    {% endfor %}
    </ul>
{% endmacro show_array %}


{% macro show_object(data) %}
    <ul>
    {% for key,value in data %}
        <li><strong>{{ key }}</strong>: {{ self::show_value(type=key,data=value) }}</li>
    {% endfor %}
    </ul>
{% endmacro show_object %}


{% macro show_value(type, data) %}
    {% if type == "pages" %}
        {% for page in data %}
            {{ self::show(name=page['title'], content=data) }}
        {% endfor %}
    {% elif type == "content" %}
        {{ self::show(name=type, content=data) }}
    {% elif data is object %}
        {{ self::show_object(data=data) }}
    {% elif data is iterable %}
        {{ self::show_array(data=data) }}
    {% else %}
        {{ data }}
    {% endif %}
{% endmacro show_value %}

I drop that in to templates/macros/debug.html and in my base.html theme or template file add to the top:

{% import "macros/debug.html" as debug %}

and add in the footer for the site:

{{ debug::show_tera_context() }} 

And you can leave it there during main development if you keep making some basic mistakes.

4 Likes

Thanks the little I was able to see this looks like it would have been cool but it took like 16 seconds to build my site and then my browser stopped responding once I tried to load the page. I think maybe I shouldn’t try it on my current site as I guess it’s too big now.

that will not be an issue anymore in the next version, you can fetch all your site if you want and it shouldn’t increase load time

That’s great. TBH the 16 seconds to build was surprising but not really a problem. The browser stopping responding was a bigger problem.

All I really wanted was to see what is available in the __tera_context but didn’t really want content as that was really big. There is another thread that has a link to a repo where the OP said they got __tera_context_stripped implemented but I didn’t end up trying it. I just took my time and went through the whole context as I only needed to get the path to the file being rendered to make a “Edit on github link”. But I do want to say thank you for all you’ve done for the community. I really appreciate what you’ve done. It’s the only SSG I’ve ever used in production but I’m happy to continue using it because it works well and the code feels approachable.