0

我正在使用带有 eco 模板引擎和 partials 插件的 docpad。这样做的目的是在登录页面中插入一个片段列表。

合乎逻辑的方法(对我来说)是遍历片段的集合并插入每个片段。

我有以下结构(按照初学者指南添加博客文章部分 doco):

src
├── documents
│   ├── index.html.eco
│   ├── scripts ...
│   └── styles ...
├── files...
├── layouts
│   └── default.html.eco
└── partials
    ├── hello.html.eco
    └── world.html.eco

hello.html.eco 的内容:

---
title: "Hello"
isArticle: true
---
Hello

world.html.eco 的内容:

---
title: "World"
isArticle: true
---
World

default.html.eco 的内容(布局)

<html>
<head>
    <title>Test Partials</title>
    <%- @getBlock("styles").add(["/styles/style.css"]).toHTML() %>
</head>
<body>
    <h1><%= @document.title %></h1>
    <%- @content %>
    <%- @getBlock("scripts").add(["/vendor/jquery.js","/scripts/script.js"]).toHTML() %>
</body>
</html>

以及 index.html.eco 的内容:

---
title: "Welcome"
layout: "default"
isPage: true
---
<p>Welcome to my website</p>

<% for page in @getCollection("partials").findAllLive({isArticle:true}).toJSON(): %>
<article>
    <%- @partial("<%= page.url %>") %>
</article>
<% end %>

如上所述,docpad 将崩溃并带有Cannot read property '0' of null. 但是,如果我删除该行<%- @partial("<%= page.url %>") %>,则它可以工作。

以下行检索部分目录中 isArticle 设置为 true 的所有文件。

<% for page in @getCollection("partials").findAllLive({isArticle:true}).toJSON(): %>

如果我更改<%- @partial("<%= page.url %>") %><p>page.url</p>然后输出列表 /hello.html 和 /world.html,这是我所期望的。

似乎<%-在一行中使用两次,有效地尝试嵌入一些生态,导致了问题。

有谁知道我该如何解决这个问题?我试图避免@partial("<filename>")为我希望插入的每个文件手动编码。有没有办法以某种方式传递给@partial的值page.url

4

1 回答 1

0

问题与部分插件无关,这是因为第二条<%=语句。由于我们已经进入生态,我们不需要再次逃回生态。所以第二个<%=是多余的,因为变量可以直接引用。

我最初应该将这个问题命名为“您如何引用先前设置的 eco 变量”。

解决方案是对 index.html.eco 使用以下内容:

---
title: "Welcome"
layout: "default"
isPage: true
---
<p>Welcome to my website</p>

<% for page in @getCollection("partials").findAllLive({isArticle:true}).toJSON(): %>
<article>
    <%- @partial(page.url) %>
</article>
<% end %>

从上面可以看出,page.url现在是直接引用的。

于 2014-03-12T12:33:24.223 回答