Adding Frontmatter with Template

What I’m trying to do

I am working on a template to use for my Daily Note. I am trying to get the template to title the note and add some YAML front matter. The template works as written, but the title: in the front matter always returns Untitled. That situation always corrects itself later when the file is closed. Linter takes care of it. But I am wondering if there might be a way to get this accomplished by the template when the file is created. Here is my template:

<% "---" %>
title: <% tp.file.title %>
aliases: []
created: <% tp.file.creation_date("dddd, MMMM Do YYYY") %>
modified: <% tp.file.last_modified_date("dddd, MMMM Do YYYY") %>
tags: DailyNote
<% "---" %>

<% tp.file.rename(tp.date.now("YYYY-MM-DD")+ " - Daily Note") %>

Things I have tried

I have tried placing the last line at the top of the command list, but that messes up the YAML front matter. So the situation occurs because the front matter is executed before the file gets named by the template and at that time the note is Untitled. How can I get the title portion of the front matter to display the renamed file name using the template?

Give this a try:

<%_ tp.file.rename(tp.date.now("YYYY-MM-DD")+ " - Daily Note") _%>

<% "---" %>
title: <% tp.date.now("YYYY-MM-DD")+ " - Daily Note" %>
aliases: []
created: <% tp.file.creation_date("dddd, MMMM Do YYYY") %>
modified: <% tp.file.last_modified_date("dddd, MMMM Do YYYY") %>
tags: DailyNote
<% "---" %>
  • Added <%_ and _%> to strip the whitespace above the YAML section.
  • changed title: to <% tp.date.now...

A Templater pro will probably have a better solution, but this seems to work.

I’m not sure if I’m a pro, but there are a few changes I would like to make, so let me show them first:

<%*
const new_title = tp.date.now("YYYY-MM-DD") + " - Daily Note"
await tp.file.rename(new_title)
_%>
<% "---" %>
title: <% new_title %>
aliases: []
created: <% tp.file.creation_date("dddd, MMMM Do YYYY") %>
modified: <% tp.file.last_modified_date("dddd, MMMM Do YYYY") %>
tags: DailyNote
<% "---" %>

The most important part of this change is to add await in front of the renaming, to allow for the system to wait properly before continuing with other stuff. In a simple case like this you might get away with not doing it, but you shouldn’t do much changes before things start to misbehave without it.

The second change is to store the new title into new_title so we can use it later on. This allows for not doing the generation of the title more than once, but we can use it as many times as we want.

2 Likes

Thanx Ariehen. Your response worked and I learned much from you!

Thanx Holroy for your thoughts and ideas. I appreciate you!

Holroy & Ariehen:

Admittedly, I’m am barely learning Templater, let alone Java Script.

If you have time perhaps you can teach me a bit more. The template you wrote works just fine when I set Templater to use it every time I create a new note in my journal directory. However, if I happen to mistakenly create a second “new note” the same day I get an error because the note already exists. It would be nice to be able to check for my erroneous behavior and simply open the existing note when this occurs. Can that be done in Templater?

regards,

ken

I’m not entirely sure how easy it would be to open the previously created file using only Templater as you’ve already created a new file. One way around that is to actually use the commands to create daily note already available, and specify your name in the settings for a daily note. This could still trigger the template as defined by you, and would actually make it simpler since it doesn’t need to rename the file.

To do it this way you’d do the following:

  • Go to Settings > Daily notes and set the following:
    • Date format according to your needs: YYYY/YYYY-MM-DD[ - Daily notes]
    • New file location to the base folder of your daily notes, i.e. Daily notes
    • Template file location with the full name of your Templater template
  • Go to Settings > Templater and do the following:
    • Enable the “Trigger Templater on new file creation”

Using these settings if you now trigger the Open daily note either through the command or through the icon in the toolbar, it’ll look for a file named something like “Daily Notes/2024/2024-02-07 - Daily notes”. If found open it, and if not create it (and if the year folder is not created it create that as well). And Templater will run the template as expected, but you could now use tp.file.title within your template, and skip that first part with the setting of new_title and await tp.file.rename() altogether.

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