Search code examples
eleventy

Use a date in 11ty title


I have a page where I want to use the date as the title. I see that permalinks allow you to use that data, however, I cannot do the same to use the date in my title. Is there a way to do this in Eleventy?

---
title: "{{date}}"
date: 2020-10-10
---

This returns {{date}} in the title.


Solution

  • Eleventy currently supports using data variables and template syntax only for the permalink field, but not for any other frontmatter fields:

    permalink: Change the output target of the current template. Normally, you cannot use template syntax to reference other variables in your data, but permalink is an exception. Read more about Permalinks.

    Source. See also: Use data variables in Permalink.

    However, you can use template strings and data variables if you use Computed Data. Something like this should work:

    ---
    eleventyComputed:
      title: "{{ date }}"
    ---
    

    See Using a template string. Alternatively, you can do the same thing with JavaScript (if you need more control over the date format, for example). If you want to derive your title from date for all pages in a specific directory or of a specific layout template, you can also use directory data files or layout front matter.