Using DataviewJS inside Templater

I’d LIKE to grab various values across linked files and insert them permanently into a file, using DataviewJS. However, there are certain DataviewJS functions that just won’t work inside Templater, it seems to me. For instance:

<%* 
dv = app.plugins.plugins.dataview.api;
tR += await dv.span("Hello!"); 
%>

This throws a parsing error: dv.span is not a function

What was driving me crazy for a long time was not being able to use dv.current to pull values from metadata of the current page, when the same action worked flawlessly outside of Templater. Same with dv.span. However, inexplicably, dv.page works completely fine.

<%* 
dv = app.plugins.plugins.dataview.api;
tR += await dv.page("Sites/Liberty Bell").address; 
%>

This results in Philadelphia, since in the YAML of the “Liberty Bell” note resides address: Philadelphia.

It’s fine that this works, but I want to be able to grab this information using dv.current, like so:

<%*
dv = app.plugins.plugins.dataview.api;
tR += await dv.page(dv.current().site.path).address;
%>

This would pull the YAML from the current page: site: "[[Liberty Bell]]", find the path, and use that to get the metadata from the address field. But it always throws the parsing error that dv.current doesn’t exist.

Is something broken on my end? I have all the checkboxes flipped that allow arbitrary DataviewJS, and such. And like I said, my method for fetching that address using DataviewJS just on its own is entirely successful.

I think it’s more a matter of context, and where you are when issuing your commands. Stuff like dv.span() and dv.paragraph() and most likely most of the functuon creating html output will not work in your current context.

dv.page() on the other hand is retrieving data, so that’s on another level.

So I suggest output stuff using the markdown-variant of functions, and to do a dv.page() on the current page, as you get it within the templater context, instead of doing dv.current. However, be aware that you’re building that file, so the file and all it’s metadata might not be available yet, so you’ll possibly need to use stuff like tp.frontmatter or similar depending on how you’ve triggered your template.

Okay, I can kind of understand why dv.span() or dv.paragraph() might not work, but why wouldn’t dv.current() work, since it would seem to be the same kind of function as dv.page()? I’m being pretty careful in how I’m building my metadata (in fact I’m not dynamically building my metadata at all, until I can get this function to work).

I guess there’s gotta be a way to get dv.page() to find the current page using different code, but I’ve looked at this for way too long to get my brain around it any more. Any alternate suggestions would be amazing.

For the current page you can use dv.page(tp.file.path(true)).

1 Like

Perfect! So my solution for finding the “site” note in the metadata of the current page and fetching the “address” metadata from that “site” note now looks like this:

<%* 
dv = app.plugins.plugins.dataview.api;
tR += await dv.page(dv.page(tp.file.path(true)).site).address; 
%>

Seems to work perfectly! Thanks, all!