How to parse a nested dictionary with Jinja2

Manish

I am passing a dictionary to template in python

   users={
        "output": {
                "title": "Sample Konfabulator Widget"
        }
    }

    return render_template('user.html', **locals())

but i can not parse the output at response page(user.html).

{% extends "base.html" %}
{% block title %}Index{% endblock %}
{% block head %}
    {{ super() }}
    <style type="text/css">
        .important { color: #336699; }
    </style>
{% endblock %}
{% block content %}
    <h1>Users</h1>
    <p class="important">
      <ul>
      {% for user in users %}
          <li>{{user}}.title</a></li>
      {% endfor %}
      </ul>

    </p>
{% endblock %}

But i don't get expected output Sample Konfabulator Widget while i get output.title. How can i get value of output.title?

OneCricketeer

users is a one-element dictionary, not a list, so a loop doesn't really make sense.

Since you have just one element, get that without a loop

<p>{{users['output']['title']}}</p>

If you did want multiple "users", and had an element like this

users={
    "output": {
            "title": "Sample Konfabulator Widget"
    }, 
    "output2": {
            "title": "foo"
    }
}

Then you could have done a loop

  {% for key in users %}
      <li>{{key}} : {{users[key]['title']}}</li>
  {% endfor %}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Ansible: How to create nested dictionary using Jinja2

How can I save a nested dictionary as variable in jinja2 ansible?

How to indent nested if/for statements in jinja2

How to parse this nested dictionary datastructure in Python 3?

How to parse nested dictionary inside a list in yaml?

How to access dictionary parameters with key containing "." (dots) in Jinja2?

How to print a dictionary within a list in jinja2?

How to reference dictionary value without iterating in Jinja2 template?

How do I print a dictionary on a single line in jinja2?

How to iterate over a dictionary in Jinja2 using FastAPI?

how to get nested undeclared variables in python jinja2

How to add nested groupby using Jinja2 templates

Ansible Jinja2: How to get nth value of nested dict?

How to use filter on a nested jinja2 template

Parse nested dictionary conveniently

Swift 5 How do we parse a Dictionary nested inside a Dictionary, nested inside another Dictionary?

Best way to format a nested Python dictionary to be rendered in a jinja2 templates

Loop over Ansible variable array in Jinja2 template to create nested dictionary

Nested For Loop in Jinja2

How to further parse a nested dictionary in a json file to a dataframe in Python

How to parse a nested dictionary in pandas/python - baseball API

How to access each dictionary individually in a list of dictionaries using Jinja2?

How to compared a nested pillar key value in an if statement in jinja2 for saltstack

How to work with nested list of dicts for docxtpl jinja2 tags in python

Creating and populating a dictionary in jinja2

default value for dictionary in jinja2 (ansible)

ansible jinja2 loop dictionary

Iterating through Python dictionary with Jinja2

jinja2 - loop through a dictionary