eleventy
eleventy copied to clipboard
Can I use a variable from a collection or data file in front matter
I am trying to store my canonical URLS in a CMS and I want to expose them in the front matter so I can push to a layout. I tried this but I just get an object not the actual URL
This works
`---
layout: home
title: test
description: test123
canonical: https://dev.clockshark.com
this doesnt
layout: home
title: test
description: test123
canonical: {{ settings.home.canonical.url }}
Why do you need this variable in your front matter? You have access to it anywhere in your templates, as is.
If you want a shorter name just declare in your base template:
{% set canonical = settings.home.canonical.url %}
Then inside this base template and any included template you can access the canonical URL as:
{{ canonical }}
Just as you would have done with the frontmatter property (but it's impossible, even with JavaScript frontmatter!).
Tried that it didn't work
Why do you need this variable in your front matter? You have access to it anywhere in your templates, as is.
If you want a shorter name just declare in your base template:
{% set canonical = settings.home.canonical.url %}
Then inside this base template and any included template you can access the canonical URL as:{{ canonical }}
Just as you would have done with the frontmatter property (but it's impossible, even with JavaScript frontmatter!).
Do you have a settings.js
file inside your data directory (see global data doc)?
It should contain this:
module.exports =
{
home:
{
canonical:
{
url: 'https://dev.clockshark.com'
}
}
}
every page has a different canonical. I swithced from liquid to nunjuks and use the inhert block now and got it working. @octoxalis
I have a similar, simple use case: when iterating over data to generate pages, I want to use the dataset properties inside the front matter definition, but that does not work. How to fix? Quoting the (already closed) github.com/11ty/11ty-website/issues/33#issuecomment-1192638468
I have
---
permalink: "auditees/{{ auditee.title | slug }}/"
title: {{ auditee.title }}
---
{% set title = auditee.title %}
still the rendered page title is {{ auditee.title }}
without any interpolation.
The permalink works, but the title does not. How to fix?
@openmindculture You want to use eleventyComputed
for things that are non-permalink
, see https://www.11ty.dev/docs/data-computed/