Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metadata flagged partials #32

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ To increase performance it is recommended you only include the exact template da

If your partial only needs to be rendered once per (re)generation then you can specify `cacheable: true` in the partial's meta data, doing so greatly improves performance.

An alternate to using the ```src/partials``` folder is to instead include ```isPartial: true``` in a document's metadata. This will prevent that document from being written during generation, but will make it available as a partial. The above guidelines around referencing (e.g. ```<%- @partial('metadata-flagged-partial', false, { localState: true }) %>```) apply.

Partials actually render asynchronously, when you call `<%- @partial('hello') %>` you'll actually get back something a temporary placeholder like `[partial:0.1290219301293]` while your template is rendering, then once your template has rendered, and once all the partials have rendered, we will then go through and replace these placeholder values with the correct content. We must do this as template rendering is a synchronous process whereas document rendering is an asynchronous process. [More info here.](https://github.com/docpad/docpad-plugin-partials/issues/12)


Expand Down
7 changes: 6 additions & 1 deletion src/partials.plugin.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,12 @@ module.exports = (BasePlugin) ->

# Fetch our partial
partialFuzzyPath = pathUtil.join(config.partialsPath, partialName)
partial.document ?= docpad.getCollection('partials').fuzzyFindOne(partialFuzzyPath)
partialCollection = docpad.getCollection(config.collectionName)
partial.document ?= partialCollection.fuzzyFindOne(partialFuzzyPath)
partial.document ?= partialCollection.findOne({ filename: $startsWith: partialName })
partial.document ?= partialCollection.findOne({ basename: partialName })
partial.document ?= partialCollection.findOne({ relativeBase: partialName })
partial.document ?= partialCollection.findOne({ relativeOutPath: partialName })
unless partial.document
# Partial was not found
message = util.format(locale.partialNotFound, partialName)
Expand Down
12 changes: 11 additions & 1 deletion test/out-expected/partials.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,14 @@


<!-- With a partial containing the same partial -->
<header>Hello <header>Hello World. Site date is defined no</header>. Site date is defined no</header>
<header>Hello <header>Hello World. Site date is defined no</header>. Site date is defined no</header>

<!-- A partial by way of setting metadata (isPartial: true) -->
<div>Metadata-flagged partial</div>
<div>Metadata-flagged partial</div>

<!-- A partial by way of setting metadata (isPartial: true), located in a subfolder -->
<div>Metadata-flagged partial in subfolder</div>
<div>Metadata-flagged partial in subfolder</div>
<div>Metadata-flagged partial in subfolder</div>
<div>Metadata-flagged partial in subfolder</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
isPartial: true
cacheable: true
---
<div>Metadata-flagged partial in subfolder</div>
5 changes: 5 additions & 0 deletions test/src/documents/metadata-flagged-partial.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
isPartial: true
cacheable: true
---
<div>Metadata-flagged partial</div>
10 changes: 10 additions & 0 deletions test/src/documents/partials.html.eco
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,13 @@ title: 'Partials Test'

<!-- With a partial containing the same partial -->
<%- @partial('header.html.eco', false, {name: @partial('header.html.eco', false)}) %>

<!-- A partial by way of setting metadata (isPartial: true) -->
<%- @partial('metadata-flagged-partial.html') %>
<%- @partial('metadata-flagged-partial') %>

<!-- A partial by way of setting metadata (isPartial: true), located in a subfolder -->
<%- @partial('folder/metadata-flagged-partial-in-folder.html') %>
<%- @partial('folder/metadata-flagged-partial-in-folder') %>
<%- @partial('metadata-flagged-partial-in-folder.html') %>
<%- @partial('metadata-flagged-partial-in-folder') %>