我使用docpad與生態模板引擎和partials插件。這一點是要將片段列表插入登錄頁面。如何使用部分集合來根據url插入部分?
邏輯方法(對我而言)是迭代片段集合並插入每個片段。
我有以下結構(以下將beginner guide on adding blog posts和partials 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,這是我的期望。
看來,使用3210在一行中兩次,有效地試圖嵌入一些生態環境,正在造成這個問題。
有誰知道我該如何解決這個問題?我試圖避免爲每個希望插入的文件手動編碼@partial("<filename>")
。有沒有辦法以某種方式傳遞給@partial
值page.url
?