metalsmith / layouts

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

Question: Is it possible to use partials in content pages? #128

Closed mischah closed 7 years ago

mischah commented 7 years ago

Hej there,

using a partial in a template/layout file using {{> partialname }} works just fine.

But using a partial that way in in a content page doesnโ€™t render the partials content. Is this a bug or on purpose?

This tutorial says it should work content pages too: https://www.sitepoint.com/create-static-site-metalsmith/#partials

Iโ€™m confused ๐Ÿ˜•

Thanks in advance, Michael

ismay commented 7 years ago

Is this a bug or on purpose?

That's on purpose, the tutorial is incorrect. You can use metalsmith-in-place for rendering content in content pages (version 1 uses consolidate (like metalsmith-layouts), version 2 uses jstransformers).

mischah commented 7 years ago

Thanks for the quick reply.

One additional question. Am I wright that it is a decision between metalsmith-in-place and metalsmith-layouts?

Because I would love to be able to use partials in content pages as well as in layout files ๐Ÿ˜˜

ismay commented 7 years ago

Am I wright that it is a decision between metalsmith-in-place and metalsmith-layouts?

You can use both at the same time. You can also use them both multiple times with different settings. So it's all possible ๐Ÿ‘

mischah commented 7 years ago

Okay. I donโ€™t get it to work over here. Trying for about 2 hours for now ๐Ÿ™ˆ

Do I have to run metalsmith two times with different sources destinations to accomplish that?

Like first using metalsmith-in-place to render the content pages than using metalsmith-layouts to combine the rendered content pages with the layout?

Or can I chain them within one build?

ismay commented 7 years ago

Just chain them in one build. Also try stack overflow and the slack channel for help.

mischah commented 7 years ago

Thanks. Appreciate your time ๐Ÿ’–

mischah commented 7 years ago

I got it up and running ๐Ÿ†

Just leaving a note for people who might stumble about that :octocat:


Itโ€™s actually easier that I thought to accomplish that. Here is my complete build file:

const metalsmith = require('metalsmith');
const layouts = require('metalsmith-layouts');
const inPlace = require('metalsmith-in-place');
const registerHelpers = require('metalsmith-register-helpers');

metalsmith(__dirname)
.source('../src')
.destination('../build')
.clean(true) // Clean destination before build
.use(registerHelpers({
    directory: '../src/handlebars/helpers'
}))
.use(layouts({ // Wrap layouts around content pages
    engine: 'handlebars',
    rename: false,
    directory: '../src/handlebars/layouts',
    default: 'default.hbs',
    pattern: '*.hbs',
    partials: '../src/handlebars/partials',
    partialExtension: '.hbs'
}))
.use(inPlace({ // Render handlebars content pages
    engineOptions: {
        pattern: '*.hbs',
        partials: '../src/handlebars/partials'
    }
}))
.build((error) => { // Build process
    if (error) {
        console.error(error); // Error handling is required
    }
});

This way I can use handlebars partials, variables from frontmatters and helpers in layout files as well as in content pages ๐ŸŽ‰