11ty / eleventy

A simpler site generator. Transforms a directory of templates (of varying types) into HTML.
https://www.11ty.dev/
MIT License
17.14k stars 493 forks source link

content partials #2982

Open mrtnmgs opened 1 year ago

mrtnmgs commented 1 year ago

Is your feature request related to a problem? Please describe.

I often need to break the content of a page into several files in order to separate code and content. (If the content can only be injected in one place in the template, in many case this separation is not possible). As far as I understand all partials must live in the _includes directory. But it's awkward to have part of the content in one place and another part elsewhere:

src
├── _includes
│   ├── layout.njk
│   └── shapes-partials
│       ├── circle-specs.md
│       ├── circle-options.md
│       ├── triangle-specs.md
│       └── triangle-options.md
└── shapes
    ├── circle.md
    └── triangle.md

I tried creating the partials where the page lives:

src
├── _includes
│   └── layout.njk
└── shapes
    ├── circle.md
    ├── circle-specs.md
    ├── circle-options.md
    ├── triangle.md
    ├── triangle-specs.md
    └── triangle-options.md

When I do this, a page is created for each partial. Adding the partials to .eleventyignore is not a working solution as hot reloading doesn't work anymore. It seems like partials have to live in the _includes directory in order to be handled properly. I had the same issue whether I imported the partial file with include or renderFile.

The best I was able to come with is specifying a separate directory for the layouts in .eleventy.js and move the location of the includes directory closer to the content. But if I use partials in different locations on my site it will still be awkward.

Maybe I'm thinking about this the wrong way? Is there another way to have different pieces of content injected in different places in a template?

Describe the solution you'd like

I think the simplest fix would be to allow several directories for includes:


  return {
    dir: {
      includes: ["_includes", "./foo/_includes", "./bar/_includes"]
    }
  }
};```

Or maybe files starting with an underscore could be treated as partials regardless of where they are? (with option to deactivate the feature and/or to configure the prefix. Even though the only issue I can think of is when one needs a url starting with an underscore, but there's always the option to set `permalink`)

### Describe alternatives you've considered

_No response_

### Additional context

_No response_
uncenter commented 1 year ago

You could add layout: false (or "layout": false in a directory data file) for each of the partials and then layout: mylayout.njk for the main page in the folder. I'm not sure how you would include the pages properly though, maybe with the render plugin?

uncenter commented 1 year ago

I'd also like to say that I think you already opened a ticket on the Discord forum for this issue! You'll will definitely get faster and better answers on there instead of here - most issues on this repo don't actively get responses.

mrtnmgs commented 1 year ago

Ok so from what I gather there's no 11ty way to do this... layout: false sounds interesting I'll try that. And noted re: discord vs github issues. Thanks for taking the time