Get Dataview inline field from different file using Templater

@Twita Gladly.

It’ll probably be easiest to explain by just demonstrating my use case. I’m a pastor and I use Templater to set up a template for a new sermon each week. I wanted the template to automate as much metadata as I could. My structure is essentially one main MOC for each book/sermon series and then individual documents for each week’s sermon.

So when I call the template, here’s what happens, along with the code that accomplishes it. (Note, it’s all wrapped in Templater’s tags <%* -%>

(1) I am prompted for the sermon series. I’m preaching in Matthew, right now, so I would select = Matthew Sermon Series MOC. This assigns the .basename of that file (i.e. “= Matthew Sermon Series MOC”) to the variable series_MOC.

let series_MOC = (await tp.system.suggester((item) => item.basename, app.vault.getMarkdownFiles())).basename

(2) I am prompted for the sermon text. I enter something like “Matthew 11:20–30”. It’s assigned to the variable text.
let text = await tp.system.prompt("Enter your sermon text.")

(3) I want access to the metadata of my = Matthew Sermon Series MOC file. This metadata is in a callout box at the front of the file that looks like this:

> [!meta]+ Extra Meta
> parent:: [[Sermon Preparation MOC]]
> series_book:: [[Matthew]]
> series_name:: "The King and His Kingdom"

I did it this way because the link titles update automatically if the linked filename changes (and they are visible as backlinks) vs. linked filenames in YAML which do not update nor show as backlinks.

To access this metadata, I assign the results of a Dataview Page query to a variable using this code:

let sermonMeta = DataviewAPI.page(tp.file.find_tfile(series_MOC).path)

(4) This enables me to call each metadata item in the body of the template. For instance, at the beginning of each sermon file, I have a callout box that looks like this in the template:

> [!meta]+ Extra Meta
> parent:: [[<% series_MOC %>]]
> series_order:: <% tp.user.getSeriesNumber(tp, "[[" + series_MOC + "]]") %>
> sermon_text:: "<% text %>"
> sermon_title:: 
> series_name:: "<% sermonMeta.series_name %>"
> series_book:: <% sermonMeta.series_book %>

You can see that I am calling the metadata using the variable sermonMeta and dot syntax, e.g. .series_name which reflects the Dataview inline keys from the sermon series MOC (series_name:: "The King and His Kingdom in this case).

This is an example of using Dataview for the query and then assigning the result as a static result using Templater, which is the heart of this system and what I was trying to do. I could just have a simple dataview query there, but I needed a static output, which is why I needed to access the inline field from a specific file. There might be a better way to do it, but this is working so far. :slight_smile:

(5) I do something similar with a javascript snippet for finding the series number. I call the snippet with tp.user.getSeriesNumber as seen above. The code in the snippet looks like this:

function getSeriesNumber (tp, seriesTag) {
  let result = "";
  
  const sermonList = app.plugins.plugins.dataview.api
    .pages(seriesTag + 'AND !"000 Home" AND !"Resources/Templates"')
    .sort(p => p.series_order, 'desc')
    .map(p =>[p.series_order]);

  result = sermonList[0];
  result++;
  return result;
}

module.exports = getSeriesNumber;

Essentially what’s happening is the same thing as above. I’m using a Dataview query to get a specific sermon (in this case the most recent), find it’s series_order value, increment it by 1, and return it for the new series order. I needed this both because it’s helpful information and also so I can sort my table in the Sermon MOC by series_order as my files are named Sermon - Matthew 11_20–30 and therefore don’t sort by filename (and cdate isn’t always preferable to me).

Hope that’s helpful in understanding how this is working for me. I only know a little javascript and I gain most of my understanding from trying to read documentation, so my code may be wonky at times. But everything’s working now thanks to those above who helped me figure out why it wasn’t (and some kind hearts on the Obsidian Discord!).

9 Likes