1

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?

Павел Иванов
  • 1,863
  • 5
  • 28
  • 51
  • Have you read e.g. http://stackoverflow.com/q/32044/3001761? http://stackoverflow.com/q/19193289/3001761? – jonrsharpe Apr 13 '15 at 09:39
  • Yes, and I don't have a full understanding how to iterate through this structure, because it's a mixture of list and dictionaries inside. I have an intuitive feeling that in my case recursion is only choice but... – Павел Иванов Apr 13 '15 at 12:30

2 Answers2

3

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.

Ben
  • 6,687
  • 2
  • 33
  • 46
0

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

WHS
  • 139
  • 1
  • 4