eleventy icon indicating copy to clipboard operation
eleventy copied to clipboard

Can I use a variable from a collection or data file in front matter

Open clockshark opened this issue 5 years ago • 6 comments

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 }}

clockshark avatar Nov 19 '19 21:11 clockshark

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!).

octoxalis avatar Nov 20 '19 08:11 octoxalis

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!).

clockshark avatar Nov 25 '19 18:11 clockshark

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'
    }
  }
}

octoxalis avatar Nov 26 '19 09:11 octoxalis

every page has a different canonical. I swithced from liquid to nunjuks and use the inhert block now and got it working. @octoxalis

clockshark avatar Dec 06 '19 09:12 clockshark

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 avatar Jul 25 '22 16:07 openmindculture

@openmindculture You want to use eleventyComputed for things that are non-permalink, see https://www.11ty.dev/docs/data-computed/

pdehaan avatar Jul 25 '22 17:07 pdehaan