Templater snippet help

Hi all, I posted this on the GitHub Templater plugin discussion but not getting any response, so I thought I would try here as well.

I’m trying to code a Daily Note template to move that note to a storage folder once that day has passed. So today’s daily note will move to storage tomorrow. That way, there is only ever today’s daily note in my “001 DAILY” folder.

The following code does something weird. When a new Daily Note is generated, the code between the ```s disappears. And its disappearing in today’s note, not yesterdays. So the IF statement isn’t working at all.

Thoughts?

Was just thinking, is this normal, that the Templater code was processed and is just waiting for the IF statement to be true in order to trigger the move??

```
<%*
if (tp.file.title(“YYYY-MM-DD”) == tp.date.now(“YYYY-MM-DD”, -1)) {
tp.file.move(“001 DAILY/PAST/” + tp.file.title); }
%>
```

The templater command is firing when you first create the daily note. The if statement is false so nothing happens.

One thing you could do is have the previous note moved on created of a new daily note. To do that you would need to use obsidian api to move the note rather than templater.

<%*
const file = tp.file.find_tfile(tp.date.now(“YYYY-MM-DD”, -1))

if (file) {
await app.vault.rename(file, “path/to/archive/”+file.basename)
}
%>

1 Like

HI @shabegom, thank you for the new idea, I can see where I was going wrong, but I am now getting a templater console error with the new snippet. I changed the path/to/archive correctly.

Regarding “obsidian api”, does that mean I would need to be logged into my obsidian account for this call to work? I am often offline, so I would need a standalone solution.

I don’t have a direct solution for you, but I use the Daily Notes plugin with Templater to write a journal, where it creates (1) nested folders for years and months and (2) files for days. This does put today’s file in the month’s folder (rather than in the in-box) but the day’s journal entry is always accessible from the ‘open today’s daily note’ icon in the side bar. I also embed dataview queries in every note to provide lots of different navigation options.

If of any interest, see the markdown file on this post.

Angel

Thank you @anon12638239, I’m looking for something simpler, but nice dataview example.

This bit is the obsidian api. It’s just the underlying way plugins interact with the obsidian app. Nothing to do with your account or being online.

If you post the error I can help troubleshoot.

Thank you @shabegom, I used the exact same snippet as you provided with the “/PAST/” path as the only change. I used only your snippet in my Daily Note template to ensure it wasn’t something else in the template. This is the console error:

Log.ts:17 Templater Error: Template parsing error, aborting.
Bad template syntax

Invalid or unexpected token

var tR=’’,__l,__lP,include=E.include.bind(E),includeFile=E.includeFile.bind(E)
function layout(p,d){__l=p;__lP=d}
const _prs = [];
tR+=’```\n’
const file = tp.file.find_tfile(tp.date.now(“YYYY-MM-DD”, -1))

if (file) {
await app.vault.rename(file, “/PAST/”+file.basename)
}
tR+=’\n```\n’
const _rst = await Promise.all(_prs);
tR = tR.replace(/rJ2KqXzxQg/g, () => _rst.shift());
if(__l)tR=await includeFile(__l,Object.assign(tp,{body:tR},__lP))
if(cb){cb(null,tR)} return tR

I did some troubleshooting in my vault. I think this should work:

<%*
const date = tp.date.now("YYYY-MM-DD", -1)
const file = tp.file.find_tfile(date)

if (file) {
const name = file.basename
await app.fileManager.renameFile(file, `/PAST/${name}`)
}
%>
1 Like

Thank you @shabegom, that worked with a couple minor changes. Kudos to you for the research. Above and beyond!

I had to use the full path under the main vault or I received a folder not found error . So “001 DAILY/PAST/” instead of “/PAST/”. And I had to include the .md at the end of the api call line or the file created had no extension.

I had actually found the “renameFile” in the api command list on github yesterday but the missing part was “fileManager”. I think that api line will help a lot of people in the future.

```
<%*
const date = tp.date.now(“YYYY-MM-DD”,-1)
const file = tp.file.find_tfile(date)

if (file) {
const name = file.basename
await app.fileManager.renameFile(file, `001 DAILY/PAST/${name}.md`)
}
%>
```

===

Incidentally, there is a new feature in the latest Templater plugin that allows you to create a separate template that runs on Obsidian startup. So I took the above snippet out of my daily note and put it in a separately named template, then pointed Templater to that new template. This way my Daily Note stays clean and doesn’t have empty ```s left over after the snippet runs. Basically, Templater is becoming Obsidian Scripting. Its very cool, I open Obsidian, I see yesterday’s note disappear(rename/move to PAST folder) and today’s daily note pops into existence in the same place.

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