Accessing frontmatter from within a Templater template note

What I’m trying to do

I have a template note whose frontmatter is, say,

number: 10

In the body of the template note (below the frontmatter) I have a reference of the kind

The number is <% tp.frontmatter.number %>.

When I create a note from this template, I expect to see, in the body,

The number is 10.

but I get

The number is undefined.

Why? All I am trying to do is replicate in the body of the instance note a value from the frontmatter which is present in both the template and the instance.

Things I have tried

  • Looked at the Templater documentation. It seems simple enough, but it doesn’t work. No idea what I am doing wrong.
  • Searched this forum - such a simple question has not been asked.
  • Searched the web - found no answer.

The problem is, the “tp.frontmatter” function is looking for preexisting frontmatter in target file, not in the template file itself. If you want to reuse some data in the template, you better save it as variable.

Hi reaty, what do you mean by the target file? Both the template and the file created from the template have the same frontmatter variables defined, as I explained. Perhaps if you could explain how you think the ‘tp.frontmatter’ function can be used successfully, I might understand better.

The file, that you created from template, will have the same frontmatter only after the template code is executed. Templater tries to run tp.frontmatter function BEFORE it pastes the frontmatter into the file, that’s why it can’t find anything. You can only use tp.frontmatter if you create file first, write the frontmatter in it manually, and only then insert the template.

If you want to create the file from template and paste the same thing several times, you should use js variable instead. For example:

<%* let myVar = 10 %>

number: <%* tR += myVar %>

The number is <%* tR += myVar %>.
1 Like

Thank you for explaining this, reaty. I understand it now. Thank you also for the alternative.

If I put

<%* let myVar = 10 %>

above the frontmatter

number: <%* tR += myVar %>

then the frontmatter is not recognised. If, however, I put it inside like this

number: <%* let myVar = 10 %><%* tR += myVar %>

then it all works.

A simpler variant to reference a declared variable is as follows:

<%* let myVar = 10 %>

Hi there,  I think <% myVar %> is a nice number.

The number is still <% myVar %>

It’s only the first block which needs to be a command execution block to define the variable. The other can be normal block, and just reference the variable directly.

In addition these examples should take care to not add extra whitespace in front of a frontmatter block, so the recommended syntax for the OP’s original template would be:

  let myNumber = 10
number: <% myNumber %>

The number is <% myNumber %>

Notice the use of _%> to end the leading template block to remove any extra newlines at the top.


Hi holroy, your idea is missing from your post. Would you post it again?

Already done… I hit the wrong button while I was still writing it… :smiley:

Many thanks, holroy, both for the simplifying and for the method of adding the declaration at the top without affecting the frontmatter.

1 Like

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