IF statements (weekday) in Templater?

Hi all,

What I’m trying to do

I’m trying to re-create weekday templates I used in Roam before seeing the light.

I want a template that will display different content depending on the day of the week (today’s weekday on creation) and exclude content intended for other weekdays

It seems the templater plugin would be best?

Something like:

IF day of week = monday Today is Monday

IF day of week = tuesday Today is Tuesday

…and if I use the template on a Monday, “Today is Monday” would appear but not “Today is Tuesday”.

Things I have tried

A bunch of things, lots of googling.

Using the Templater plugin, you can easily get the day of the week from a dynamic execution context, so something like the following would print “Hooray” on days like the day I wrote this.

<%* const dayOfWeek = moment().weekday(); console.log( "m", dayOfWeek) -%>
Humpty, dumpty, some other text
<%* if ( dayOfWeek == 1 ) { -%>
Too early?

<%* } else if ( dayOfWeek == 2 ) { -%>
<%* } else { -%>
not so good
<%* } -%>

Some explanations are in order:

  • When using dynamic execution code, <%*, it allows for javascript execution of code
  • The moment().weekday() returns the day of the week as number, which allows us to check on that later on, so store that in dayOfWeek
  • This can be going back and forth between ordinary text, which can be a little confusing, but you’ll get the drift of it
  • Using -%> to end the template blocks, re-assures that the this template block does not add a newline into the created note
  • The %%before%% and %%after%% doesn’t serve any purpose beside helping to see the newlines. Feel free to remove them after you played around a little with either %> or -%> to end the template blocks
1 Like

While holroy’s answer is probably the most complete one for showing/hiding sections of the note depending on the week, if you just want to change the title to contain the name of the weekday that is even easier.

Today is <%* tR += moment().format(“dddd”); %>

Which will return ‘Today is Wednesday’ currently

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