-
-
Notifications
You must be signed in to change notification settings - Fork 496
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
Globally set default layout? #380
Comments
You can use directory data files but they do not work yet in the project root, only in subfolders. See #245 for that request. So to answer your question: kind-of. I would consider this request to be a duplicate of #245 though, which would complete this request too. Can you upvote and follow along there? |
Directory data files: https://www.11ty.io/docs/data-template-dir/ |
|
The challenge is, when using a global data object in the So if you have the following file:
And it contains the following JSON object: { "layout": "default.njk" } The resulting global data object will look like this: But there's nothing special about We can make this work in our favor by naming the file with the property name we'd like to set, and then using JS (not json) to return a plain string value. So add the following file:
And add whatever value you want for you default layout page: module.exports = "default.njk" Which will now set the value at the appropriate depth This is really nice if you'd like to process markdown files with your own custom styles that typically have to live at the root directory of your project (and don't want to add frontmatter to them)
And voila! Docs published to your site, instead of only visible through github |
@KyleMit just to say thanks, this is a really useful workaround! |
@KyleMit Thank you so much for providing a workaround, as well as some clarity around why and how the workaround works. It's greatly appreciated. |
…thub.com/11ty/eleventy/issues/380\#issuecomment-568033456 Former-commit-id: 66cf028626ce7ff754ffe5c7e9197be8886ebb14
I'm new to 11ty so apologies if this is a stupid question, but why can't this just be a config option in
|
I tried the workaround but it didn't work for me. |
Just starting to learn 11ty and was trying to figure out how to set a global default layout - adding this in case it helps other beginners. Found two ways: Method 1: Add a data file at the top level of your source folder. It needs to have the same name as the folder. For example, if your source folder is named src, the file would be src.11tydata.js or src.11tydata.json src/src.11tydata.js module.exports = {
// Set a default layout for everything in the src folder and below.
layout: "layouts/default.njk"
} Method 2: In the _data folder, add a file named layout.js (This one is mentioned above. In this case, the format is a little different, it needs to be a string. Don't know enough yet to know why.) _data/layout.js module.exports = "default.njk" |
It's almost end of 2024 and you can do this by using eleventyConfig.addGlobalData("layout", "layout/default.njk"); |
Is there some way to globally set the
layout
property? Using a global data file doesn't work, since the values in them don't override the top-level ones.The text was updated successfully, but these errors were encountered: