Execute Templater Script in Callout

What I’m trying to do

I have created this template to insert 5 random notes with incomplete status tag into my daily notes for me to process, which works perfectly. However, I now also want to have it be inserted into a folded callout box, so that the 5 notes are hidden until I open the callout. I have attached a screenshot of what it should look like after the template code.

The template:

<%*
const noOfNotes = 5
const dv = this.DataviewAPI
const files = await dv.tryQuery
 (`
   LIST
   WHERE status = "incomplete"
 `)

let randomList = []

for (let i = 0; i < noOfNotes; i++) {
  const random = Math.floor(Math.random() *
                            (files.values.length - 1))
  randomList.push(files.values[random])
}

tR += dv.markdownList(randomList)
%>

What it looks like currently, without any callouts in the template:

What I want (note the blue callout box around the section):

Things I have tried

I have tried searching, but everytime I mention templater & callouts together all I can find are templates to insert callouts.
I have tried adding “>” before all lines, before just the dv.tryQuery parts - ending at “`)”.

I also tried this with a dataview command like below, which works as I want, although I want the results to be printed with templater, so they do not refresh each time I open the note:

The template

> [!5 Random Notes (Dataviewed)]
>  ```dataview
>   LIST
>   WHERE status = "incomplete"
>   Limit 5
> ```

This returns:

The Issue

It seems like templater can’t recognise commands with a “>” in front of them, unlike when doing the same thing with dataview. I can’t really figure out a solution with my limited knowledge of Templater & js

Note, your logic doesn’t protect against duplicates. Here is a quick version that I ran on my Book collection for status = "unread". A bit brute force, but it works.

<%*
const noOfNotes = 5
const dv = this.DataviewAPI
const files = await dv.tryQuery
 (`
   LIST
   WHERE status = "unread"
 `)

let randomList = []

tR += '> [!unread]-\n'

for (let i = 0; i < noOfNotes; i++) {
  const random = Math.floor(Math.random() *
                            (files.values.length - 1))
  tR += '> ' + files.values[random] + `\n`
}
%>
1 Like

Hey thank you so much for this! that is fantastic :slight_smile:

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