如何使用 .11ty.js 文件?

How to use a .11ty.js file?

我有一个 sample.11ty.js 文件,其中包含以下代码:

module.exports = "<p>sample</p>";

njk 布局文件中,我尝试像这样包含上面的 sample.11ty.js 文件:

{% include "components/sample.11ty.js" %}

sample段没有显示,但是控制台出现如下错误:

Reference Error: module is not defined

How can I include a .11ty.js file in a njk template?

official Nunjucks documentation所述:

an include is not a pre-processor that pulls the included template code into the including template before rendering; instead, it fires off a separate render of the included template, and the results of that render are included.

所以似乎在 Nunjucks 包含文件中尝试预处理 javascript 被认为是失败的。


但是,以下工作:

njk 布局文件中,使用以下 frontmatter:

--- layout: layouts/sample.11ty.js ---

并且在 layouts/sample.11ty.js 中,使用 javascript class 像这样:

class Sample {
data() {
return {
  name: "Eleventy"
  };
}
render(data){
    return data.content;
  }
}

module.exports = Sample;