17

for this dictionary with this Flask controller

projects = {
        'life-calc':{'url':'life-calc',
                    'title': 'Life Calculator'},
        'text-game':{'url':'text-game',
                    'title':'Text Adventure'},
        'fill-it-up':{'url':'fill-it-up',
                    'title':'Fill It Up'},
        'rock-paper-scissors':{'url':'rock-paper-scissors',
                    'title':'Rock, Paper, Scissors'},
        'bubble-popper':{'url':'bubble-popper',
                    'title':'Bubble Popper'}
            }


@app.route('/')
def index():
    return render_template("index.html",
                            projects = projects)

and the template as such

    <h1>
        List of My Projects
    </h1>

    <ol>
        <li>
            <a href = "life-calc">Life Calculator</a>
        </li>
        <li>
            <a href = "text-game">Adventure Game</a>
        </li>
        <li>
            <a href = "fill-it-up">Fill It Up</a>
        </li>
        <li>
            <a href = "rock-paper-scissors">Rock Paper Scissors</a>
        </li>
        <li>
            <a href = "bubble-popper">Bubble Popper</a>
        </li>
    </ol>
    <p>test section below</p>
    <ol>
        {% for project in projects %}
        <li><a href = "{{ project['url'] }}">{{ project['title'] }}</a> </li>
        {% endfor %}
    </ol>

{% endblock %}

How can I access the items in the dict to print a list of my projects as in the HTML above the test?

I solved my own problem with help from Rendering a python dict in Jinja2 / Werkzeug The template block should be

{% for key, value in projects.iteritems() %}
<li><a href={{value['url']}}>{{value['title']}}</a></li>
{% endfor %}

But I'm still curious as to how to access further nested dictionaries, and if this is the smartest way to create a simple menu.

Community
  • 1
  • 1
MattO
  • 1,753
  • 3
  • 12
  • 16

2 Answers2

25

I think you want to know how access the nested dict in template

If you think I got your question

Generally, This is the way to access the nested dictionary items in dictionary.

If the iterables are getting nested further just you have to increase the forloop depth level whether it is list or dict.

Here I am giving just a generic example in my own way for your understanding

Data:

parent_dict = {1: {'A':'val1','B':'val2'}, 2:{'C':'val3','D':'val4'}}

iteration in jinja2:

{% for key,parent_dict_item in parent_dict.items() %}
   {% for key2, nested_value in parent_dict_item.items() %}
      <li><a href = "{{ nested_value }}">{{ nested_value }}</a> </li>
   {% endfor %}
{% endfor %}

Answer:

<li><a href="val1">val1</a> </li>
<li><a href="val2">val2</a> </li>
<li><a href="val3">val3</a> </li>
<li><a href="val4">val4</a> </li>
Nava
  • 6,276
  • 6
  • 44
  • 68
  • 1
    I know this was posted almost two years ago, but is there a way to sort the nested dicts using this method? I had been using something like `{% for k, v in parent_dict|dictsort %}` but with this method I cannot use `dictsort`. Any ideas? – DJGrandpaJ Apr 14 '16 at 16:05
  • 2
    You can write the `template filters` to do it. It is available in django templates, jinja2 etc..[refer](http://jinja.pocoo.org/docs/dev/api/#custom-filters) – Nava Apr 15 '16 at 10:16
  • Thanks, I'll take a look! :) – DJGrandpaJ Apr 15 '16 at 12:38
  • 1
    iteration using .iteritems() is better than .items() – Yogesh Kamat Jun 24 '16 at 15:04
20

Instead of expanding the key and value in the loop, you can also use the key to reference the item in the dict itself:

{% for project in projects %}
  <li><a href = "{{ projects[project].url }}">{{ projects[project].title }}</a> </li>
{% endfor %}
tkteun
  • 301
  • 2
  • 4