eleventy icon indicating copy to clipboard operation
eleventy copied to clipboard

UsingCircularTemplateContentReferenceError when using Eleventy snippets in Markdown

Open fragosti opened this issue 4 years ago • 7 comments

Describe the bug When using

<ul>
  {%- for post in collections.post | reverse | limit(postsListLimit) -%}
  <li class="py-3">
    <h3>
      <a href="{{ post.url | url }}">{{ post.data.title }}</a>
    </h3>
    <label>
      {{ post.date | dateReadable }}
    </label>
    <p class="line-clamp py-1">
      {% excerpt post %}
    </p>
  </li>
  {%- endfor -%}
</ul>

In my markdown, I get

> ./src/site/index.njk contains a circular reference (using collections) to its own templateContent.

`UsingCircularTemplateContentReferenceError` was thrown:
    UsingCircularTemplateContentReferenceError: ./src/site/index.njk contains a circular reference (using collections) to its own templateContent.
        at TemplateMap.populateContentDataInMap (/Users/francescoagosti/Projects/blog/node_modules/@11ty/eleventy/src/TemplateMap.js:382:17)
        at processTicksAndRejections (internal/process/task_queues.js:86:5)
Copied 1 item / Wrote 0 files in 0.09 seconds (v0.10.0)

I get this for other syntax as well, like when using:

{% include "components/postslist.njk" %}

My markdown-it config is

  eleventyConfig.setLibrary(
    'md',
    markdownIt({ html: true })
      .use(markdownItAnchor, {
        permalink: true,
        permalinkSymbol: '<i data-feather="link" class="link"></i>',
      })
      .use(markdownItLinkAttr, {
        // Make external links open in a new tab.
        pattern: /^https?:\/\//,
        attrs: {
          target: '_blank',
          rel: 'noopener noreferrer',
        },
      })
  )

Expected behavior For eleventy not to process the template code in markdown code snippets.

Environment:

  • OS and Version: Mac OS Sierra
  • Eleventy Version: 0.11.0

fragosti avatar Jun 06 '20 02:06 fragosti

This fixed it:

-    markdownTemplateEngine: 'njk',
+    markdownTemplateEngine: false,
   }

fragosti avatar Jun 06 '20 02:06 fragosti

Re-opening as this is still an issue if you want to use nunchucks in your markdown.

fragosti avatar Jun 09 '20 17:06 fragosti

Re-opening as this is still an issue if you want to use nunchucks in your markdown.

Having the same issue here.

mpalpha avatar Jul 20 '20 16:07 mpalpha

Same issue too here.... But very different environment! Windows 10, eleventy last commit from github, nunjucks templates

~= 2000 articles, but 3 with a link to a perfectly working page give that message. No idea where to go from there!

TigersWay avatar Jan 17 '21 09:01 TigersWay

Same issue too here.... But very different environment! Windows 10, eleventy last commit from github, nunjucks templates

Not really... Just some trouble with eleventyComputed which needed to go too deep!

TigersWay avatar Jan 20 '21 01:01 TigersWay

I was having the same issue with a liquid template (for rendering tags with post excerpts), so the problem seems to be universal. I also use the excerpt short code (with a custom function), so I guess the access to templateContent inside the excerpt function might be the culprit.

I have solved the problem (rather clumsily) with the following code snippet at the top of my excerpt function:

function excerpt(post){
  // list of template pages  that iterate over post
  const iteratingTemplates = ['./src/index.liquid', './src/tags.liquid'];
  if (iteratingTemplates.indexOf(post.inputPath)>-1) {
    return null;
  }
 // now extract the excerpt from post.templateContent
}

Of course this means that templates which contain lists can never have excerpts and that you have to add your specific template names to your custom excerpt function. A better approach would probably be to check somewhere in post.template if the template matches post.inputPath, preventing a post from trying to excerpt itself. But I don't know enough how 11ty works to know where to look for that information or if such an approach is even possible.

gbirke avatar May 03 '21 20:05 gbirke

I'm trying to render one collection from an item in another collection, and I get this error. I see nothing circular about it. Turning off markdownTemplateEngine does not resolve it. 😕

jboolean avatar Jun 06 '22 18:06 jboolean

Came across this issue too. Turns out that the problem was that 11ty didn't knew about the collections used. Using eleventyImport front matter config fixed the problem. See https://www.11ty.dev/docs/collections/#declare-your-collections-for-incremental-builds and https://github.com/11ty/eleventy/issues/975.

megheaiulian avatar Jan 19 '23 07:01 megheaiulian

Came across this issue too. Turns out that the problem was that 11ty didn't knew about the collections used. Using eleventyImport front matter config fixed the problem. See https://www.11ty.dev/docs/collections/#declare-your-collections-for-incremental-builds and #975.

Thanks, it seems that this might work, but I do not understand how to use it. I have the same issue, getting a warning for UsingCircularTemplateContentReferenceError when using templateContent within the excerpt shortcode in a tags template

guilleliss avatar Feb 09 '23 20:02 guilleliss

I managed to solve this but filtering all from the collections in the tags template, this is the tags template:

---
layout: base
pagination:
  data: collections
  size: 1
  alias: tag
  filter:
      - post
      - all
permalink: /tags/{{ tag }}/

guilleliss avatar Feb 10 '23 20:02 guilleliss