Listing Variables Within a Jinja2 Template

04 Apr 2025

While converting this site to Pelican, I needed to do quite a bit of plugin and template writing to get it to look and feel the way I wanted to. One frustration was not knowing which variables I had available to my templates. After quite a few failed attempts and bad advice from some LLMs, ChatGPT offered this solution which worked for me:

<ul>
  {% for key, value in self.__dict__.items() %}
    <li>{{ key }}: {{ value }}</li>
  {% endfor %}
</ul>

To be clear, though, ChatGPT offered several solutions, some of which were incorrect or bad... this one happened to work though.

Tagged with: