I'm using docpad with the eco templating engine and the partials plugin. The point of this is to insert a list of snippets into a landing page.
The logical approach (to me) is to iterate through a collection of the snippets and insert each snippet.
I have the following structure (following the beginner guide on adding blog posts and the partials doco):
src
├── documents
│ ├── index.html.eco
│ ├── scripts ...
│ └── styles ...
├── files...
├── layouts
│ └── default.html.eco
└── partials
├── hello.html.eco
└── world.html.eco
Contents of hello.html.eco:
---
title: "Hello"
isArticle: true
---
Hello
Contents of world.html.eco:
---
title: "World"
isArticle: true
---
World
Contents of default.html.eco (the layout)
<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>
And the contents of 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 %>
As is above, docpad will crash with a Cannot read property '0' of null
. However if I remove the line <%- @partial("<%= page.url %>") %>
then it works.
The following line retrieves all the files in the partials directory where isArticle is set to true.
<% for page in @getCollection("partials").findAllLive({isArticle:true}).toJSON(): %>
If I change <%- @partial("<%= page.url %>") %>
to <p>page.url</p>
then the output lists /hello.html and /world.html, which is what I expect.
It appears that using <%-
twice in one line, effectively attempting to embed some eco, is causing the problem.
Does anyone know how I can get around this? I'm trying to avoid manually coding a @partial("<filename>")
for each file that I wish to be inserted. Is there a way to somehow pass to @partial
the value of page.url
?
The problem is not to do with the partial plugin, it's because of the second <%=
statement. As we're already in eco, we don't need to escape back into eco again. So the second <%=
is redundant as variables can be referenced directly.
I should have originally titled this question "How do you reference variables in eco that have been previously set".
The solution is to use the following for 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 %>
As can be seen above, page.url
is now referenced directly.