Dataviewjs in embedded template

I’d like to store receipts in my vault, and have my daily notes show a list of receipts from that day at the bottom of the note. My notes are named in the format YYYY-MM-DD_ddd, and my receipts (all PDFs) use the name format “YYYYMMDD - .pdf”. I have this dataviewjs code that works when included directly in a daily note:

```dataviewjs
const filename = dv.current().file.name
let grabdate = filename.split("_")[0]
let grabparts = grabdate.split("-")
let year = grabparts[0]
let month = grabparts[1]
let day = grabparts[2]
let newdate = year + month + day
const folderpath = '\_Resources/Receipts'
const pdfFiles = app.vault.getFiles().filter(file => file.extension === 'pdf' && file.path.includes(folderpath) && file.name.includes(newdate))
dv.list(pdfFiles.map(file => dv.fileLink(file.path)))
```

What I’d like to do is create a template (“Journal Metadate (Template)”) that contains the header ‘Documents’, followed by the code which would display the list of matching files. I’d then append the following to the end of every daily note:
![[Journal Metadata (Template)]]
This leaves the daily note markup much cleaner (easier to read in other systems) than if I were to include all the code.

When I try this, I get the header “Documents”, but it doesn’t show any results from the code. I also tried this in the template:

<%*
tR += \```dataviewjs
const filename = dv.current().file.name
let grabdate = filename.split("_")[0]
let grabparts = grabdate.split("-")
let year = grabparts[0]
let month = grabparts[1]
let day = grabparts[2]
let newdate = year + month + day
const folderpath = '\_Resources/Receipts'
const pdfFiles = app.vault.getFiles().filter(file => file.extension === 'pdf' && file.path.includes(folderpath) && file.name.includes(newdate))
dv.list(pdfFiles.map(file => dv.fileLink(file.path)))
\```
%>

But it just shows that exact code in the daily note, not the results of the code. I feel like I must be close, can anyone see where I’m going wrong? Bonus points if you can improve the coding I have, which is based on my copy/paste knowledge of dataviewjs.

I would suggest using a dv.view() function instead of trying to trigger a template like you’re dying right now.

That will give you a good way to update the query in the future, and give you a nice look within the daily note.

I can write you some code, but maybe not before tomorrow. If you’d like to try it yourself, I recommend to make a dedicated folder fir this kind of scripts, and then simply move all your dataviewjs code into a file, yourScript.js, and call it using something similar to:

```dataviewjs
dv.view("your/folder/yourScript")
```

Notice the file must be a plain text file, with the .js file extension.

Brilliant. Got it working. This should offer a lot of flexibility as a ‘footer’ for all my journal notes. Doesn’t seem to fully respect the css (my H1 is larger than the other H1 on the page) but I’ll do some searching on how to rectify that. Thanks!

1 Like

Nice to give help, especially when someone picks up on the gist of the idea, and is able to execute it like you just did!

1 Like

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