metalsmith / layouts

A metalsmith plugin for layouts
MIT License
116 stars 49 forks source link

Partial file not found #126

Closed rehierl closed 7 years ago

rehierl commented 7 years ago

In the hopes that this will help anyone ...

Lets assume the following:

.use(layouts({
engine: "handlebars",
directory: "layouts",
default: "layout.txt",
partials: "partials"
}))
directory = metalsmith's base folder
directory / test.txt = a file to render, but with no frontmatter
directory / layouts / layout.txt = so the default layout template
directory / layouts / specific / layout.txt = some specific template
directory / partials / partial.text = a partial with whatever content

Test 1

At first, (index.js / render / line 149) will be executed with str representing an absolute path to the current template file. And str will be passed on as path to (consolidate.js / readPartials / line 121), which it will use in var file = join(dirname(path), partials[key] + extname(path));

In short: consolidate.js recreates an absolute path to the partial file by using the template file's extension, which will fail (.txt vs .text) if it tires to read the partial's contents.

Now I realize, that consolidate is responsible for this quirk, but it might help, if the metalsmith-layouts' documentation would mention that template files and partial files must use the same file extension.

Test 2

Alright, lets rename "partial.text" to partial.txt.
This will work just fine.

Test 3

Now, lets change the content of directory / test.txt to:

---
layout: specific/layout.txt
---
whatever

This will fail because (read-partials.js / readPartials / line 37) calculates a path to the partial file that is relative to the base layout folder (see options.directory) and because consolidate.js uses the layout file's actual folder (see above). Both folders don't have to match!

Now that you already seem to work on version 2, please document that if you are using partials, then all template files must be located inside the options.directory folder and not in any subfolder.

ismay commented 7 years ago

Thanks for the issue 👍 . Would you mind addressing this in a PR?

rehierl commented 7 years ago

I think that should fix the issues with "Test 3".

(I'm a github newbie) Why does it display "Fix for issue 126 and 127"?