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
Dynamic permalink data variable / string interpolation doesn't work in .11ty.js templates #2318
Comments
@JohJonker I think you're close, but a couple small issues… permalink: '/categories/${category.slug}/index.html', This wouldn't work since:
Here's my small sample files: // src/categories.11ty.js
exports.data = {
pagination: {
data: 'CATEGORIES',
size: 1,
alias: 'category',
},
- // permalink: '/categories/${ category.slug }/index.html',
+ eleventyComputed: {
+ permalink(data) {
+ return `/categories/${ data.category.slug }/`;
+ }
}
};
exports.render = async function getPage({ category }) {
return `<h1>${category.title}</h1>`;
}; // .eleventy.js
module.exports = function (eleventyConfig) {
// I was too lazy to make a global data file, so I used `.addGlobalData()` (Eleventy v1.0.0+).
// Note my inconsistently silly slugs.
eleventyConfig.addGlobalData("CATEGORIES", [
{ slug: "one", title: "Page ONE" },
{ slug: "Two", title: "pAge tWO" },
{ slug: "ThReE", title: "paGe tHrEe" },
{ slug: "foUR", title: "pagE FOur" },
]);
return {
dir: {
input: "src",
output: "www"
}
};
}; Now, running npm run build will give me the following glorious output: > 11ty-2318@1.0.0 build
> eleventy
[11ty] Writing www/categories/one/index.html from ./src/category.11ty.js
[11ty] Writing www/categories/Two/index.html from ./src/category.11ty.js
[11ty] Writing www/categories/ThReE/index.html from ./src/category.11ty.js
[11ty] Writing www/categories/foUR/index.html from ./src/category.11ty.js
[11ty] Wrote 4 files in 0.02 seconds (v1.0.0) Note that it's using my exact We can solve that by enforcing slugified URLs using the built-in eleventyComputed: {
permalink(data) {
- return `/categories/${ data.category.slug }/`;
+ return `/categories/${ this.slugify(data.category.slug) }/`;
}
} And now if I rerun npm run build locally, I get the following (where my category slugs are lowercased and slugified): > 11ty-2318@1.0.0 build
> eleventy
[11ty] Writing www/categories/one/index.html from ./src/category.11ty.js
[11ty] Writing www/categories/two/index.html from ./src/category.11ty.js
[11ty] Writing www/categories/three/index.html from ./src/category.11ty.js
[11ty] Writing www/categories/four/index.html from ./src/category.11ty.js
[11ty] Wrote 4 files in 0.03 seconds (v1.0.0) Possibly not a big deal depending on your exact values for |
Awesome investigation @pdehaan! One thing I would also note here is that you needn’t use // src/categories.11ty.js
exports.data = {
pagination: {
data: 'CATEGORIES',
size: 1,
alias: 'category',
},
- permalink: '/categories/${ category.slug }/index.html',
+ permalink: function(data) {
+ return `/categories/${this.slugify(data.category.slug)}/index.html`;
+ }
}
};
exports.render = async function getPage({ category }) {
return `<h1>${category.title}</h1>`;
}; |
|
Describe the bug
I'm trying to specify a custom permalink with string interpolation in an 11ty.js template. I tried the instructions from the Docs, but I note that "These will be parsed with the current template’s rendering engine" and therefore understand that the
{{ ... }}
is not working in my 11ty.js template. However, there doesn't seem to be a different way to bring in variables. I've tried:In each case I can see the string is not being interpolated in the error output:
The workaround is to explicitly set
templateEngineOverride: 'njk'
in the frontmatter to allow the{{ ... }}
syntax, but that causes some obvious downstream issues with the template.Is this me missing something fundamental or is this a valid bug / feature request / documentation update request?
To Reproduce
Here is my code from sample file
category.11ty.js
:Expected behavior
I would assume there has to be a way to do dynamic permalinks in 11ty.js templates.
Environment:
Additional context:
I looked at a bunch of related issues, and this one seems closest, but for Pug: #1899
The text was updated successfully, but these errors were encountered: