Павел Иванов
Павел Иванов

Reputation: 1913

How can I recursively extract a hierarchical dictionary using a django template?

I have hierarchical structure:

{
    "content": "Header 1", 
    "name": "folder/name.txt", 
    "decendent": [
         {
            "content": "Header 2", 
            "name": "folder/subfolder/name.txt", 
            "decendent": null
        }, 
        {
            "content": "Header 3", 
            "name": "folder/subfolder2/name.txt", 
            "decendent": [
                {
                    "content": "Header 4", 
                    "name": "folder/subfolder2/subsubfolder1/name.txt", 
                    "decendent": null
                }
                ... etc.
            ]
        }
    ]
}

I have to unroll it using (as an example) this template:

                {% for key, value in list.items %}
                <ul class="Container">
                    <li class="Node ExpandClosed">
                        <div class="Expand">

                        </div>


                        <div class="Content">
                            <a href="/help/pur/">
                                {{ key }}
                            </a>
                        </div>

                        {% for k, v in value.items %}
                        <ul class="Container">
                            <li class="Node ExpandClosed">
                                <div class="Expand">

                                </div>

                                <div class="Content">
                                    <a href="/help/test/">
                                        {{ k }}
                                    </a>
                                </div>
                                {% for k1, v1 in v.items %}
                                <ul class="Container">
                                        <li class="Node ExpandClosed">
                                            <div class="Expand">

                                            </div>

                                            <div class="Content">
                                                <a href="/help/test/">
                                                    {{ k1 }}
                                                </a>
                                            </div>

                                            {% for k2, v2 in v1.items %}
                                                    <ul class="Container">
                                                        <li class="Node ExpandClosed">
                                                            <div class="Expand">

                                                            </div>

                                                            <div class="Content">
                                                                <a href="#" onclick="k2 = '{{ k2 }}'; changeText(k2)">
                                                                    {{ k2 }}
                                                                </a>
                                                            </div>
                                                        </li>
                                                    </ul>
                                            {% endfor %}
                                        </li>
                                </ul>
                                {% endfor %}
                            </li>
                        </ul>
                        {% endfor %}
                    </li>
                </ul>
                {% endfor %}

I have to put the "name" attribute to "a href" tag, and between opening and closing tag extract the "content", for descendents of node I'd like to go through them recursively. Unfortunately, I have no idea how to perform it using django template language considering it's limitations.

Can you help me, please?

Upvotes: 1

Views: 1194

Answers (2)

WHS
WHS

Reputation: 144

I'd rather create a new tag for Jinja2-templates. Django-doc reference

Upvotes: 0

Ben
Ben

Reputation: 6767

Django can use recursive templates. If you create a template called (for example) recurse.html, and pass it a data variable with your top-level dictionary:

<ul class="Container">
    <li class="Node ExpandClosed">

        <div class="Expand"></div>

        <div class="Content">
            <a href="{{data.name}}">
                {{data.content}}
            </a>
        </div>

        {% for item in data.decendent %}
            {% include 'recurse.html' with data=item %}
        {% endfor %}

    </li>
</ul>

That should display for the first level, then re-call itself each time for it's decendents with data re-bound to the new sub-tree.

Upvotes: 3

Related Questions