-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Is it possible to have 2 layouts? #1494
Comments
Finally I solved my problem by this code in layouts/index.jsx:
|
Yea, currently that is the only way to do it. I just PRed my example with that. I was working on it, but had to stop for a while. Hoping someone else can pick it up. |
For anyone else that ends up here - it's possible to do this by implementing the |
Again, for anybody searching here, I am using the following with success (slightly jury-rigged): front-matter.js // adapted from `front-matter` module
const yamlParser = require('js-yaml')
// extract /* @meta [YAML] */ comments
module.exports = function parse(string) {
const match = /\/\*\s*@meta([\s\S]*?)\*\//.exec(string)
if (!match) {
return {
attributes: {},
body: string,
}
}
const yaml = match[1].trim()
const attributes = yamlParser.load(yaml) || {}
const body = string.substr(match[0].length)
return {attributes, body, frontmatter: yaml}
} gatsby-node.js const fm = require('./front-matter')
const fs = require(`fs-extra`)
exports.onCreatePage = async function({page}) {
const {attributes: {layout}} = fm(await fs.readFile(page.component, 'utf8'))
page.layout = layout || 'index'
} With these in place, I can use frontmatter, like so: src/pages/sample.js /* @meta
layout: wide
*/
import React from 'react'
export default () => <div>...EXAMPLE...</div> |
Just came across this issue whilst searching for a way to add multiple layouts. Not sure if Gatsby has been updated since @jrop's helpful answer. But I found https://www.gatsbyjs.org/docs/creating-and-modifying-pages/#choosing-the-page-layout. gatsby-node.js
The key lines are here...
It assumes your main layout file is index.jsx src/pages/sample.md
Then just add NEW_LAYOUT_FILE_NAME.jsx to your layout folder, restart the server and you should be good to go. Update 11/04/2018 Update 11/04/2018 |
Can you add
to a JS file? isn't this for MarkDown? |
Good spot @PolGuixe 👍 I've updated my example src/pages/sample.js should have been src/pages/sample.md |
@gilesbutler How would you use it in .js files? |
You can't as far as I know. It was a typo and was supposed to be for MarkDown. |
If you just need to make small changes, I just use conditional rendering.
|
I cannot anything find at this link which corresponds with "Choosing ghe page layout". Did Gatsby changed the content? is there a "2020" approach or plugin? |
hi, is it possible to have more than 1 layout? I searched issues, but can't find anything useful?
I tried #1119 method, use the code below:
but it doesn't work. Is there any examples or is it possible? thanks~
The text was updated successfully, but these errors were encountered: