Templater code outputs blank - help?

What I’m trying to do

I have this templater code which will copy in text based on which day of the week it is. However, it doesn’t output anything… any ideas?

<%*
if (tp.date.weekday("YYYY-MM-DD", 1, tp.file.title, "YYYY-MM-DD ddd") == tp.date.now("YYYY-MM-DD", 0, tp.file.title, "YYYY-MM-DD ddd") ) {
	tp.file.include("[[Daily Plans#Defaults#Monday]]") }
else {
if (tp.date.weekday("YYYY-MM-DD", 2, tp.file.title, "YYYY-MM-DD ddd") == tp.date.now("YYYY-MM-DD", 0, tp.file.title, "YYYY-MM-DD ddd") ) {
	tp.file.include("[[Daily Plans#Defaults#Tuesday]]") }
else {
if (tp.date.weekday("YYYY-MM-DD", 3, tp.file.title, "YYYY-MM-DD ddd") == tp.date.now("YYYY-MM-DD", 0, tp.file.title, "YYYY-MM-DD ddd") ) {
	tp.file.include("[[Daily Plans#Defaults#Wednesday]]") }
else {
if (tp.date.weekday("YYYY-MM-DD", 4, tp.file.title, "YYYY-MM-DD ddd") == tp.date.now("YYYY-MM-DD", 0, tp.file.title, "YYYY-MM-DD ddd") ) {
	tp.file.include("[[Daily Plans#Defaults#Thursday]]") }
else {
if (tp.date.weekday("YYYY-MM-DD", 5, tp.file.title, "YYYY-MM-DD ddd") == tp.date.now("YYYY-MM-DD", 0, tp.file.title, "YYYY-MM-DD ddd") ) {
	tp.file.include("[[Daily Plans#Defaults#Friday]]") }
else {
if (tp.date.weekday("YYYY-MM-DD", 6, tp.file.title, "YYYY-MM-DD ddd") == tp.date.now("YYYY-MM-DD", 0, tp.file.title, "YYYY-MM-DD ddd") ) {
	tp.file.include("[[Daily Plans#Defaults#Saturday]]") }
else {
if (tp.date.weekday("YYYY-MM-DD", 7, tp.file.title, "YYYY-MM-DD ddd") == tp.date.now("YYYY-MM-DD", 0, tp.file.title, "YYYY-MM-DD ddd") ) {
	tp.file.include("[[Daily Plans#Defaults#Sunday]]") }
} } } } } } %>

Also, is there a better way to do a sequence of if-else statements?

  1. You don’t see output because you are using tp.file.include() incorrectly.

The proper usage from Javascript Execution Command <%* is

<%*
...
tR += await tp.file.include('[[Daily Plans#Defaults#Monday]]');
...
%>
  1. You can simplify if-else nestedness via
<%*
if (condition1) {
  // block1
} else if (condition2) {
  // block2
} else if (condition3) {
  // block3
} else {
  // else block
}
%>
  1. You don’t need if-else blocks in your case at all
<%*
const dayOfWeek = tp.date.now('dddd');
tR += await tp.file.include(`[[Daily Plans#Defaults#${dayOfWeek}]]`)
%>

Thanks so much @mnaoumov! Worked great. The only thing I changed was to replace

const dayOfWeek = tp.date.now('dddd');

with

const dayOfWeek = tp.date.now("dddd", 0, tp.file.title, "YYYY-MM-DD ddd");

Because I created these notes for other days than today.

Thanks again!

1 Like

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