Is it Possible to Nest Templater Commands? (●'◡'●)

The goal is to import a different template based on the day of the week for my daily notes.

The Command I have so far is this: <% tp.file.include(“[[<% tp.file.now(“dddd”) %> Template]]”) %>

The nested command on its own, exports the day of the week: <% tp.file.now(“dddd”) %>

If I could order the templater commands, I’d have it run the nested command - to export Monday for example - and then the master command would insert the “[[Monday Template]]” into the note. Therefore automating the insertion of a day-specific template.

However I’ve had no luck with this setup - I’m trying to avoid using any code snippets, I was hoping I could use the templater commands in a cleaver way…

You could try an if or a switch :blush:

<%*
const today = tp.date.now("dddd", 0, tp.file.title, "YYYY-MM-DD")

if (today == "Monday") {
 tR += await tp.file.include("[[Monday Template]]")
  } else if (today == "Tuesday") {
    tR += await tp.file.include("[[Tuesday Template]]") 
}
// etc ...
%>

There are other ways to get to similar results though :blush:
This is just an example :innocent:

1 Like

Thanks heaps for this!! Works a charm!! Love it, appriciate the help!

1 Like

My pleasure :smile: !


Just in case :innocent: , but here’s another idea to get the appropriate template inserted when needed :blush: :

If you have a different template for each weekday, this could work :blush:

<%*
// Array storing the needed templates
// 1 template for each day of the week
const templates = [
  "[[Monday Temp]]",
  "[[Tuesday Temp]]",
  "[[Wednesday Temp]]"
  // other templates ...
];

// Find out which template is needed "today" based on the isoWeekday
// number of "today" (where Monday = 1, Tuesday = 2, etc...)
// Minus 1 as arrays are zero-indexed 
// So, if it's 0 it'll include "[[Monday Temp]]", if it's 1 > "[[Tuesday Temp]]", etc...   

const tempIndex = moment(tp.file.title, "YYYY-MM-DD").isoWeekday() - 1;

tR += await tp.file.include(templates[tempIndex]);
%>

Now, if you don’t have a template to insert everyday, this could also work:

<%*
// Array storing the needed templates or blank ("") as a placeholder
// if there's no template to insert that day
const templates = [
  "[[Monday Temp]]",
  // Blank Tuesday placeholder 
  "", 
  "[[Wednesday Temp]]",
  "",
  "[[Friday Temp]]",
  "",
  ""
];

// Find out which template is needed "today" based on the isoWeekday
// number of "today" (where Monday = 1, Tuesday = 2, etc...)
// Minus 1 as arrays are zero-indexed 
// So, if it's 0 it'll include "[[Monday Temp]]", if it's 1 > "", 2 > "[[Wednesday Temp]]", etc...   

const tempIndex = moment(tp.file.title, "YYYY-MM-DD").isoWeekday() - 1;

if (templates[tempIndex] != "") {
  tR += await tp.file.include(templates[tempIndex])
}
%>

The if might not be needed though :innocent: … as far as I’ve tested it, if the value coming from the constant templates is blank (""), tp.file.include() outputs “blank” in place of the template :smile:

But again, it’s just an example :blush:

Edit: Corrected typos I’ve made :sweat_smile: ! Thanks Holroy :raised_hands: !

1 Like

Damn that’s clean :smiling_face_with_tear::heart_eyes:
I might integrate that tomorrow hahah