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
Virtual Templates: more control over collisions between physical and virtual templates? #3254
Comments
“‘first one wins, last one wins, die on error’ with the latter as a default would be super dope”— @eaton |
FYI, the use case that I've been wrestling with for a while in 11ty is slightly tangly, but I believe it's relevant to the issue. I wrote a plugin that
For "special" pages like the home page, customized landing pages, etc, a This works great, EXCEPT when special high profile articles need special presentation/layout treatment. In a perfect world, I'd love to simply drop a custom template into the 11ty directory, add the I need to do some testing with alpha-6, but using the new "inject a custom template" feature seems like it would simplify some of the work: programmatically inserting a custom template for each content item would allow the plugin to ensure no on-disk templates have "claimed" the content ID before proceeding. Alternately, allowing finer grain control over the template and output path collision handling could also make these case easier — allowing a paginated page to supply "fallback" versions of pages the can be over-written, for example. |
This would manifest as something like below, right? src/posts/my-post.md
// .eleventy.js
eleventyConfig.addTemplate('posts/my-post.md', "my remote content", {
uuid: '0000-0000-0000-0000',
some: 'data',
more: 'data'
}) In essence, I think of a Virtual Template as providing two pieces of data to an input path:
The collision on an
So, one declarative approach would be to state your desired outcome, with a method signature like: addTemplate(path: String, content?: Object, metadata?: String, mode?: String = 'add') The data types for each argument neatly allow us to have these shortcuts: // Metadata without content
addTemplate(inputPath, {
some: 'data',
more: 'data'
}, 'merge');
// Content without metadata
addTemplate(inputPath, 'Some content', 'merge') An alternate, imperative approach:
This set of methods would allow us to express any collision outcome: if (config.hasTemplate(inputPath)) {
// mode = write
config.addTemplate(inputPath, ...);
// mode = add
throw new Error('template already exists');
// mode = skip
continue;
// mode = merge
config.mergeTemplate(inputPath, ...);
} |
A third way to handle collisions, and possibly the most flexible without any additional API surface area would be the callback variant: config.addTemplate(inputPath, function(content, data) {
return ({ content: ..., data: ... });
});
|
Is your feature request related to a problem? Please describe.
No response
Describe the solution you'd like
With fixing #1612, Eleventy gains the ability to produce ad-hoc, Virtual Templates that work as if they existed as physical files on the disk. Right now (if I understand correctly), Eleventy throws when a virtual template collides with an existing physical template.
This can be guarded against with file-system checks (e.g.
fs.exists()
), but if Eleventy has a good idea that a physical file already exists, maybe there’s an opportunity for a more graceful API. I’m starting this issue to explore possible directions for this feature.Describe alternatives you've considered
No response
Additional context
No response
The text was updated successfully, but these errors were encountered: