Templater: how to dynamically include part of a file?

Things I have tried

I have tried to include part of my Agenda like so:

<% tp.file.include(“[[…/…/0. PLANNING/Agenda#{{date:mmmm yyyy}}]]”) %>

What I’m trying to do

which, ideally, I would like to give me this:

<% tp.file.include(“[[…/…/0. PLANNING/Agenda#june 2022]]”) %>

but that doesn’t happen. Instead, the entire agenda file gets included, which is a whole year. I want only the month of june, and its reference is #june 2022

What did I write wrong in the templater code and how should it read to give me the wanted result, namely a partial inclusion of the Agenda for the month I want?

I am aware that in my code, I don’t specify which month I want. I want the month that is equal to the title of the monthly note, which in my vault are formatted like YYYY-MM.

I wonder if there is a race happening here between the resolution of the {{date: ...}} templating and the Templater tp.file.include code. Something you could try to investigate this:

const sectionLinkToInclude = "[[../../0. PLANNING/Agenda#{{date:MMMM YYYY}}]]";
<% tp.file.include(sectionLinkToInclude) %>

Then open the Console tab in your Developer Console and see whether it prints what you expect or if it prints the version with the {{date... still. If the second one, you could try using the Templater date formatting: tp.date.now("MMMM YYYY") (If you want a date relative to the note’s title rather than today, `tp.date.now(“MMMM YYYY”, 0, tp.file.title, “YYYY-MM-DD”) where you replace that last “YYYY-MM-DD” with whatever the date format of your title actually is.

Note: Be careful of capitalization in your date format strings! Capital M’s refer to months, lowercase m’s refer to minutes. Lowercase y’s don’t seem to exist in moment.js.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.