I am struggling with recursive for loops.

186 views
Skip to first unread message

Tory Berra

unread,
Feb 29, 2016, 12:00:37 PM2/29/16
to Nunjucks
Documenation:


If you scroll down to "recursive" you will see an example. I copy/pasted that code, and I have data that looks identical to the data it wants. But I get this:

[Line 24, Column 37] expected block end in for statement

Tory Berra

unread,
Feb 29, 2016, 12:08:20 PM2/29/16
to Nunjucks
DATA:

[
  {
    "id": 1,
    "User_id": 1,
    "ForumThread_id": 1,
    "ForumPost_id": null,
    "post": "Heh.",
    "createdAt": 1456765537963 
  },
  {
    "id": 2,
    "User_id": 2,
    "ForumThread_id": 1,
    "ForumPost_id": null,
    "post": "I love this site!",
    "createdAt": 1456765537963
  },
  {
    "id": 3,
    "User_id": 3,
    "ForumThread_id": 1,
    "ForumPost_id": null,
    "post": "Hello?",
    "createdAt": 1456765537963,
    "children": [
      {
        "id": 4,
        "User_id": 2,
        "ForumThread_id": 1,
        "ForumPost_id": 3,
        "post": "Yes where here.",
        "createdAt": 1456765537963,
        "children": [
          {
            "id": 5,
            "User_id": 1,
            "ForumThread_id": 1,
            "ForumPost_id": 4,
            "post": "Banned for discussing secrets.",
            "createdAt": 1456765537963
          }
        ]
      }
    ]
  }
]

Tory Berra

unread,
Feb 29, 2016, 12:10:25 PM2/29/16
to Nunjucks
Nunjucks code

<ul class="sitemap">
{%- for item in thread.posts recursive %}
   
<li><a>{{ item.post }}</a>
    {%- if item.children -%}
       
<ul class="submenu">{{ loop(item.children) }}</ul>
    {%- endif %}
</li>
{%- endfor %}
</ul>

Tory Berra

unread,
Mar 1, 2016, 10:00:50 AM3/1/16
to Nunjucks
So turns out this isnt even a supported feature of Nunjucks. I thought Nunjucks was a direct port of Jinja 2?  This seems like a really powerful feature to not have in the library and im going through some real trouble trying to build a custom tag with the terrible parser API documentation. It would be much appreciated for someone who already understands and knows the Nunjucks codebase to allow a for loop to be called again and passing it new data when a global function "call(newData)" is seen inside the for loop.
Reply all
Reply to author
Forward
0 new messages