They are basic templates but it’s the point of this project.
My main template is:
<%*
let default_title = tp.file.title;
let title = default_title;
if (title.startsWith("Untitled")) {
try{
title = await tp.system.prompt("Title");
if(title==="" || title === null){
throw new Error("Title is empty or null!")
}
}
catch(error){
console.error("Enter or Esc was pressed on prompt:", error)
title = default_title;
}
finally{
await tp.file.rename(`${title}`)
}
}
tR += "---"
%>
created:<%tp.date.now("YYYY-MMM-DD")%>
---
<HR style="border-color: RGBA(139, 108, 239 ,0.75)">
<%tp.file.cursor()%>
<HR style="border-color: RGBA(139, 108, 239 ,0.75)">
[[## SYSTEM TAGS|]]
#<%title[0].toUpperCase()%>
I added some try-catch block in the begining of my template, because if I trying to rename title and pressed just Enter(title=“”) or Esc(title=null), template not working, obsidian creates just some empty file.
So now tempate is working how I want)
created a github repo for this, this scales better imho GitHub - louis030195/awesome-obsidian-templates: a collection of obsidian templates
Isn’t that somewhat overcomplicated? Wouldn’t the following do just the same?
<%*
let title = tp.file.title
if (title.startsWith("Untitled")) {
title = await tp.system.prompt("Title")
if (title==="" || title === null){
console.error("Enter or Esc was pressed on prompt, will use default title")
title = tp.file.title
} else {
await tp.file.rename(title)
}
}
tR += "---"
%>
created:<%tp.date.now("YYYY-MMM-DD")%>
---
... The same from here ...
As far as I can tell there is no need for that extra exception stuff with throwing and catching and finally stuff. Keep it simple, if you can.
I see this in several templates: tR += "---"
.
Why not just put it as part of the template text? I mean outside od the templater section
You could, and I believe it’s a matter of convenience and habits. To me it kind of serves as a reminder that I’m within a javascript block, and it’s often optional whether I’m wanting to output the dashes or not, and as such I like to have it within the block.
But in some cases, like the one above, it could indeed be outside of the javascript block.