Using templater to retrieve frontmatter from another file

Things I have tried

I have adapted the following snippet from the forum:

<%* 
const tfile = "Beginners Guide to Companion Planting";
var get_frontmatter = this.app.metadataCache.getFileCache(tp.file.find_tfile(tfile))
tR = get_frontmatter.file.title
-%>

Where “Beginners Guide to Companion Planting” refers to a pre-existing file in my vault. When I try to run the above, the script errors out in the console:

plugin:templater-obsidian:61 Templater Error: Template parsing error, aborting. 
 Cannot read properties of undefined (reading 'title')

In other words, it seems like tp.file.find_tfile(tfile) is not finding the note Beginners Guide to Companion Planting. However, when I try:

<%* 
const tfile = "Beginners Guide to Companion Planting";
if(tp.file.find_tfile(tfile) != null){
	tR = "aha"
}
-%>

the note returns aha, which suggests that templater found something. So I don’t really understand why the original script is failing.

What I’m trying to do

I have a file with the following YAML:

---
creation_date: 2023-02-28
tags: wot_i_thought
parent: "[[Beginners Guide to Companion Planting]]"
---

I would like to have a templater script that:

  1. extracts the string “Beginners Guide to Companion Planting” from the parent
  2. is then able to open and access content from that parent, e.g. the file title or its front matter

I know how to do this dynamically with dataview, using =this.parent.something but one of the issues with this approach is that dataview continuously refreshes. There are some file properties that I might only want to set once, for which templater seems like a better choice.

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