cfml-liquid icon indicating copy to clipboard operation
cfml-liquid copied to clipboard

Stack overflow when template includes itself

Open adrianscott83 opened this issue 12 years ago • 4 comments

For recursively included templates, the LiquidTagInclude will consume all memory due to the parse method's call to tokenize. Possibly perform this action within the render function?

adrianscott83 avatar Nov 27 '12 16:11 adrianscott83

can you give me an example of the template that will cause the error

rip747 avatar Nov 27 '12 19:11 rip747

Sure thing. Here is a really basic example for a file named blog_comment.liquid. This would depict a series of blog comment entries that may contain child entries as well. The context being passed to this file would contain the name, body, and an array of child comments, if any.

<div class="comment">
  <div class="comment-name">{{ blog_comment.name }}</div>
  <div class="comment-body">{{ blog_comment.body }}</div>

  {% comment %} Recursively call the comments and output them here, if any {% endcomment %}
  {% if blog_comment.comments %}
    <div class="comment-children">
      {% include 'blog_comment' for blog_comment.comments %}
    </div>
  {% endif %}
</div>

adrianscott83 avatar Nov 27 '12 19:11 adrianscott83

can you post the code for your blog_comment drop

rip747 avatar Nov 27 '12 22:11 rip747

The blog.liquid file, which includes the blog_comment.liquid file initially, would look like this:

<div class="blog">
  <h1>{{ title }}</h1>
  <div class="blog-content">{{ content }}</div>
  {% if comments %}
    {% include 'blog_comment' for comments %}
  {% endif %}
</div>

And a context like the following could be provided to the renderer:

<cfset context = {
  'title' = 'My Blog',
  'content' = '<p>Some content goes here.</p>',
  'comments' = [
    {
      'name' = 'Bob',
      'body' = 'Great blog!'
    },
    {
      'name' = 'Frank',
      'body' = 'I really get a lot out of this blog, thanks.',
      'comments' = [
        {
          'name' = 'Adrian',
          'body' = 'No problem, thanks for reading.'
        },
        {
          'name' = 'Frank',
          'body' = 'Any idea when it will be updated again?',
          'comments' = [
            {
              'name' = 'Adrian',
              'body' = 'When I have time, I guess.'
            }
          ]
        }
      ]
    },
    {
      'name' = 'Sally',
      'body' = 'Looking forward to the next one!'
    }
  ]
} />

adrianscott83 avatar Nov 27 '12 22:11 adrianscott83