1

I need to provide a JSON formatted information from a CMS into a web page.

There may be deep nested structures in the page input like:

{
  "input": [
    {
      "header": "sample header text",
      "description": "sample description text",
      "content": {
        "json": {
          "children": [
            {
              "type": "paragraph",
              "children": [
                {
                  "text": "sample child text "
                },
                {
                  "href": "https://myorg/tilasto/kony",
                  "type": "link",
                  "title": "Linkki ",
                  "children": [
                    {
                      "text": "sample child text"
                    }
                  ]
                },
                {
                  "text": "sample child text"
                }
              ]
            },
            {
              "type": "table",
              "children": [
                {
                  "type": "table_body",
                  "children": [
                    {
                      "type": "table_row",
                      "children": [
                        {
                          "type": "table_cell",
                          "children": [
                            {
                              "type": "paragraph",
                              "children": [
                                {
                                  "text": "sample text"
                                }
                              ]
                            },
                            {
                              "type": "table",
                              "children": [
                                {
                                  "text": "sample text"
                                }
                              ]
                            },
                            {
                              "type": "paragraph",
                              "children": [
                                {
                                  "text": "sample text"
                                }
                              ]
                            }
                          ]
                        },
                        {
                          "type": "table_cell",
                          "children": [
                            {
                              "type": "paragraph",
                              "children": [
                                {
                                  "text": "sample text"
                                }
                              ]
                            }
                          ]
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        },
        "references": [

        ]
      }
    }
  ]
}

How can I recursively do the same thing as in the below HTML?

{% for tiedote in input %}
    <br>

<h2> {{tiedote.header}}</h2>

<p> {{tiedote.description}} </p>
{% for children_list in tiedote.content.json.children %}
    {% for child in children_list.children %}
        <p> {{child.text}} </p>
        <a href="{{child.href}}">
        {% for chil in child.children %}
            <p> {{chil.text}} </p>
            <a href="{{chil.href}}">
            {% for chi in chil.children %}
                <p> {{chi.text}} </p>
                <a href="{{chi.href}}">
                {% for ch in chi.children %}
                    <p> {{ch.text}} </p>
                    <a href="{{ch.href}}">
                {% endfor %}
            {% endfor %}
        {% endfor %}
    {% endfor %}
{% endfor %}
{% endfor %}

Also href may be present in some level of the input, how can I check if it is present or not?

Updated: In order to use macro (as in answer) in jinja template do I need to install django-macros or would there be any other means for that?

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
Jaana
  • 266
  • 1
  • 3
  • 14
  • Django is not using Jinja, your question have been misleading from the start. If you want to use Jinja and not the templating engine of Django, you have to make a configuration change, see: https://stackoverflow.com/a/30715508/2123530 – β.εηοιτ.βε Jun 02 '22 at 08:25

0 Answers0