Creating a templater variable for a dataview script

What I’m trying to do

I’m using both Templater and Dataview.

I’d like to assign the results from a dataview js script to a templater template which will be used in my daily note template.

The script parses a note with writing prompts setup as fields and chooses a random prompt. The note is identified with a tag #wp and setup with fields like so:

writing_prompt:: Animals take over the world.

I’ve tweaked a script @Craig wrote to do this and it works fine. [Dataviewjs] Get an automatic humanized, "Morning", "Afternoon", "Evening". **Great for user greetings! - #4 by Craig

Dataview Script:

// List of writing prompts
let prompts = [];

// Extract writing prompts from pages that have them
dv.pages("#wp")
	.forEach(page => {
		dv.array(page.writing_prompt)
			.forEach(writing_prompt => {prompts.push(writing_prompt);})});

let prompt = prompts[Math.floor(Math.random()*prompts.length)] 

dv.paragraph(prompt);

The reason I’d like it as a template and not just the script is I’m trying to keep the note files themselves as clean as possible and for the actual prompt to be written in the file vs. the dataview script.

Now what I’m trying to find out is if I can have this script run as a template I can put in my Daily notes.

Things I have tried

I’ve got the dataview script running inside a note. The actual file doesn’t include the prompt if viewed outside of obsidian. (expected)

But I haven’t been able to get the dataview script running as a user function and in a template.

I’ve set a Templater user scripts folder. But I’m having trouble figuring out how to tweak this script to live and execute within a templater template. I’ve seen the example on the dataview page - "Burning out" dataviews · Issue #42 · blacksmithgu/obsidian-dataview · GitHub but am not 100% on how to use that to achieve what I’m trying to do.

I believe from reading the docs, I need to turn that script into a function and would need to use the dataview.api to gain access to the fields and then I think… assign it to a template? Which I could then call in the Daily note template.

I’ve attempted to do this but my js skills aren’t there yet. Is there any chance anyone can validate if this is the right approach or if there is a better approach?

If it’s the right approach, I’d be very thankful for any advice or examples on what tweaks I should be make to get this working.

2 Likes

Using the examples in the github issue and your DataView script, I put together the following Templater template. I cannot test it since I don’t currently use the DataView plugin, but it should put you in the ballpark.

<%*
const dv = this.DataviewAPI;

// List of writing prompts
let prompts = [];

// Extract writing prompts from pages that have them
dv.pages("#wp")
	.forEach(page => {
		dv.array(page.writing_prompt)
			.forEach(writing_prompt => {prompts.push(writing_prompt);})});

let prompt = prompts[Math.floor(Math.random()*prompts.length)];
-%>
writing_prompt:: <% prompt %>
2 Likes

Thank you for helping with that last step!

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