What I’m trying to do
Hi, my daily notes have YAML properties that I use to track my habits. I want to have the following structure from Monday to Friday
habit1: false
habit2: false
habit3: false
habit4: false
And on weekends I would like the daily note to be generated with this other structure:
habit1: false
habit2: false
Things I have tried
---
habit1: false
habit2: false
---
<%*
const dayOfWeek = tp.date.now("dddd");
if (["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"].includes(dayOfWeek)) {
const file = app.vault.getAbstractFileByPath(tp.file.path());
if (file) {
await app.fileManager.processFrontMatter(file, (frontmatter) => {
frontmatter.habit3= 0;
frontmatter.habit4= 0;
});
}
}
%>
Thank you for your support (this post was written with the help of a translator, sorry if it’s not so clear)
holroy
January 29, 2025, 3:33pm
2
I’d switch the template around a little, to better accommodate for your request and write like the following:
<%---%>
<%*
const dayOfWeek = tp.date.now("dddd");
let habits = [
"habit1: false",
"habit2: false"
]
if (["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"].includes(dayOfWeek)) {
habits.push("habit3: false")
habits.push("habit4: false")
}
tR += habits.join("\n") + "\n"
_%>
<%---%>
This would aleviate the need for changing the frontmatter through app.fileManager.processFrontMatter()
, when you’re creating the file, which can remove other issues as well.
The code is untested, but it should work (if I’ve not done anything stupid )
1 Like
holroy:
<%---%>
<%*
const dayOfWeek = tp.date.now("dddd");
let habits = [
"habit1: false",
"habit2: false"
]
if (["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"].includes(dayOfWeek)) {
habits.push("habit3: false")
habits.push("habit4: false")
}
tR += habits.join("\n") + "\n"
_%>
<%---%>
Hello, thank you for your response. When restarting Obsidian, I get the following error:
I asked the AI to help me review the code, and it gave me this:
---
<%*
const dayOfWeek = tp.date.now("dddd")
let habits = [
"bañarme: false",
"dientes: false",
"desayunar: false",
"columna: false",
"librosLeidos: ",
"librosTiempo: 0",
"tesis: 0",
"meditar: false",
"cocinar: false"
];
if (["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"].includes(dayOfWeek)) {
habits.push("pomodorosTrabajo: 0");
habits.push("habit4: false");
}
tR += habits.join("\n");
%>
---
It looks like this way works , I’ll confirm tomorrow if the conditional does too.
holroy
February 2, 2025, 5:06pm
4
It seems like you pasted with cmd+V , when you needed to do cmd+shift+v . The latter would keep the lineshift, if I don’t get them mixed up :-). The point being, when there is no semicolons, ;
, you do need the lineshifts as in my original post.
Other than that, I don’t see at a quick glance why it should give that error message.
thank you @holroy it’s work! I share the final code (I think I did not copy the above code correctly):
---
<%*
const dayOfWeek = tp.date.now("dddd")
let habits = [
"bañarme: false",
"dientes: false",
"desayunar: false",
"columna: false",
"librosLeidos: ",
"librosTiempo: 0",
"tesis: 0",
"meditar: false",
"cocinar: false"
];
if (["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"].includes(dayOfWeek)) {
habits.push("pomodorosTrabajo: 0");
}
tR += habits.join("\n");
%>
---