Return/Echo `dataviewjs` result in a Templater script

What I’m trying to do

I’m trying to make a Templater template file output/add the result of a dataviewjs query to the file this template is associated with.

Things I have tried

The DataView script I’d like to use “inside” a Templater script works well in itself:

``` dataviewjs
dv.taskList(
	dv.pages('"pages"').file.tasks.filter(task => 
		!dv.func.contains(task.tags, "untask") && 
		!dv.func.contains(task.tags, "scheduled") && 
		!task.completed
	)
	.sort(() => 0.5 - Math.random()).slice(0,1) ) 
```

Things stop working when I try to make the Templater script return/add the query result to the template content:

<%* 
const dv = this.app.plugins.plugins["dataview"].api 
tR += await dv.taskList(
	dv.pages('"pages"').file.tasks.filter(task => 
		!dv.func.contains(task.tags, "untask") && 
		!dv.func.contains(task.tags, "scheduled") && 
		!task.completed
	)
	.sort(() => 0.5 - Math.random()).slice(0,1) )
%>

Using this Templater script results in a Templater Error: Template parsing error, aborting. Cannot read properties of undefined (reading 'createEl') error.

Thoughts? :thinking: Thanks in advance for your help.

Try using dv.markdownTaskList, it’ll return pure markdown instead of manipulating the DOM which don’t work within Templater.

Hey @holroy - thanks for your reply.

As far as I see, dv.markdownTaskList does something similar to dv.taskList (the method used in my first code example) - I’ll definitely check how the two are different.

But my question was mainly about the second code snippet - if it’s possible, at all, to use Templater’s own syntax in combination with the DataView methods. Based on what I see on this forum (in other topics), this type of combination works, generally, but there’s no topic/resource showing or explaining how it works specifically with dv.taskList (or dv.markdownTaskList).

I’m really just curious - my template works after all, with the inclusion of my first code example, but but I see that many Obsidian users use a "Templater syntax with DataView methods" combo instead of the "DV only" approach.

Did you even try your second code snippet with dv.markdownTaskList() instead of dv.taskList()? If so, what happened?

The main difference between those two is that the markdown variant returns the markdown text, whilst the other manipulates the DOM (or html output). And when you’re still in the template execution the DOM is not available.

Damn it, it works!! :smiley: - Sorry, @holroy , I think I totally didn’t misunderstood your previous reply, but I just tested the dv.markdownTaskList() function inside the Templater script, and it works beautifully!!

Thanks for your help. :bowing_man:

For others facing the same problem/curious about the solution that works:

<%* 
const dv = this.app.plugins.plugins["dataview"].api 
tR += await dv.markdownTaskList(
	dv.pages('"pages"').file.tasks.filter(task => 
		!dv.func.contains(task.tags, "untask") && 
		!dv.func.contains(task.tags, "scheduled") && 
		!task.completed
	)
	.sort(() => 0.5 - Math.random()).slice(0,1) )
%>

This is the way!

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