Transcription
In today's video, we're talking about Templater in Obsidian. Templater is essentially a much more robust and complex version of the Template plugin. It can get overwhelming, so while there are no requirements for this video, if you're just starting out with Obsidian, it might be best to go over my Obsidian playlist before watching this video.
So, what's the difference between the Template plugin and Templater? The main difference is automation. Templater can apply any template you want based on a ton of different variables, such as the day of the week or what folder the note was created in. It takes longer to set up, that's for sure, but once it's set up, it's a lot more powerful. Templater has a bunch of functionality, and really, I probably don't even use 10% of it. When I first started using Templater, I went through all the documentation, which is beautifully written, by the way, and I adopted only what added value to me. That's what we're going over in today's video.
Templater's functionality is divided between internal functions and user functions. In this video, I'm just going over the internal functions. Templater also gives you access to the Moment object, which is out of the scope of this video, but if there's interest, let me know in the comments, and I'll happily make a video dedicated to Moment and user functions.
So, let's get started.
Okay, so as always, here we are at our Mastering Obsidian vault, and this right here is what we're doing in the last video, which was tracking our long-term quality of life using weekly journaling in Obsidian. If that sounds interesting to you, make sure to give that a go. I'm going to leave a link to it somewhere on the screen right now.
So, let's first download the Templater plugin. Mine is probably already installed. Yes, it is. If you don't have it, come here to Community Plugins, browse for the Templater plugin, enable it, and then come here to Templater settings. We're going to leave everything as default for now; we're going to come back to this page later on.
So, let's open up a new page and go over the basics of Templater. I'm going to title this "Template Tutorial." Regardless of what function you're doing in Templater, it always starts and ends the same way: left arrow percentage and percentage right arrow. Your function is going to go in between these two.
Let's probably do one of the most common ones in Templater. We're going to start with `tp.date.now` for the year, month, day. The first point of frustration is actually activating a template because once you input the template syntax into your note, you can't just go into preview mode and see the results. Instead, what you need to do is press Command + P for the command palette on Templater and come over here to "Replace templates in the active file." Once you do that, it does it for you.
A much easier way is to assign a hotkey to it. In my case, I have mine set up as Option + R, and you can change that under Obsidian's hotkeys section. If you come over here to our settings and then hotkeys, and we search here for "replace," it's going to give us Templater replace, and you can change this to whatever you want. I have mine as Option + R, so over here, you can press "Customize this command," and then you type in your command. Now I can just come here and press Option + R. Much simpler, right?
Now, I know what a lot of you might be thinking right now: why would you type all of this out every single time you want to use Templater? But that's not really the point of it. The point is to have the setup so it gets automatically placed for you based on the parameters that you give it, such as a folder location, hotkeys, integrating it with your journaling, etc. We're going to go over all of that in the next chapter of this video, but for now, let's just go over the syntax.
Now, Templater has a lot of syntax. In this video, I'm just going to show you what I find useful for my workflow personally, but if you have really specific use cases, definitely check the documentation, which I have linked to below. Out of the seven internal functions, I find `tp.date` and `tp.file` to be the ones that I use more frequently. So, let's look at some examples for those two.
I'm going to create a new page here. I'm going to call it "TP Date," and I'm going to copy and paste the function we have on the previous page. If I do Option + R, yes, it works! The first thing I want to show you is another way of displaying this. For a lot of people, seeing it as 2022-07-22 is not really appealing, and Templater has a more user-friendly format. So, we can just get rid of this part here, and I'm going to type in `tp.date.format` followed by `YYYY-MM-DD`. I'm going to activate it, and now it looks a little cleaner for many people. For me, I prefer the standard way, as I've already gotten used to it.
You can also add a `tp.date` for tomorrow or for yesterday. So, if I come here and I go back and I do `tp.date.tomorrow` and I activate it, we have the 23rd of July, and today is the 22nd. In here, I can also put in yesterday, and we have yesterday's date. Some of you might already be thinking, why can't we just use the Natural Language Dates plugin, which is the one where you put in "at" followed by "today," "tomorrow," "two days ago," and it inputs the date for you? Yes, you can use it this way, but the difference between the two is that this one here is automated. As you'll see in a bit, once you put both of these into a template and you activate that template, this is going to return you "at today," not the actual date, whereas this will always give you, well, in this case, it'll give you yesterday's date every single time. That's the main difference.
All right, so let's get rid of this one here, and let's now look at the syntax for adding dates in the past or future—not just last week, but last month or even last year. Let's start by taking today's date. So, I'm going to put in `tp.date.now` and I'm just going to do the YMD version. Now, let's say we wanted to pull this exact date but for June, so for last month. We can just do `tp.date` and leave everything else here the same, but then we just add a second one here for `p-1m`. So, once we come out of this one and we toggle it, Option + R, you can see that it took June 22nd, so this exact date of today but for last month. If you went for two months ago, just put in `p-2`, and then once you toggle it on, you have May 22nd. If you want it for two months from now, you just put in the plus sign. So, when I come here and I toggle this on, it's going to give us September 22nd.
So, what if you want to do this, but instead of months, you want it for years? You come here, and instead of "m," you just change this to "y." So, in this case, let's say we want today's date of last year. Today is July 22nd; we want July 22nd for 2021. In this case, I can just put in `-1y`, come out of this, Option + R, and now we have July 22nd of last year.
All right, so let's now take it one step further. You can take any Templater function and turn the result into a link, and this is when it starts to get really powerful. So, let's take a look at this example here that we have, and let's actually get rid of this one here because we're not doing much with it. In here, all we need to do is put in square brackets around it, right? Once we toggle it on, we have last year's date but as a link. Depending on your Obsidian configuration, if you have a daily note for this exact date, once you click it, it'll take you there.
We're going to explore a bunch of cool things we can do with this in the next chapter, but some examples are, for instance, on our daily notes, we can have an "On This Day" line that pulls a daily note of that exact date from last year. That is super helpful, and I use it a lot to kind of see what my thoughts were at that time last year. I also use it for my weekly planning, which I typically do on Mondays. So, I like to have a link on the previous Monday and the following Monday so that when I'm browsing my weekly reviews, I can just bounce between the two at the click of a button. I do the same for the daily notes, but I'm going to get to all of that in the next chapter of this video.
For now, let's look at `tp.file`. I'm going to make a new page. I'm going to title it "TP File." I only use three functions here, and they all go together right after the YAML header. The first one is `tp.file.creation_date`, and I'm going to close it. When I activate it, it's going to give me the file creation date for the specific file—in this case, it's today at 7:33, which is the exact time right now.
The next one is `tp.file.last_modified`, and it looks like this: `tp.file.last_modified`. If I toggle this on, you can see that I created the file at 7:33, and the last time I modified it was 7:37. Then we have one more here, which is `tp.file.folder`, and we have just a very simple `tp.file.folder`. So now, if I come here and I toggle it on, we're not going to have anything here because we're not in the folder. If I move the TP file into, say, the Scripts folder and I refresh this, you're going to see that it tells us we're in the Scripts folder.
Okay, so before moving on to the next chapter, I just want to show you guys `tp.web`. This is another internal function of Templater that pulls either a random image or a random quote from the web. I don't use the image feature, but I do use the quote for my journals, as it's a nice low-effort way to come across interesting quotes. Plus, when I'm journaling, I tend to be a little more relaxed and susceptible to actually ponder on these different quotes.
So, I'm going to create a new page called "TP Web," and the function for the daily quotes is pretty simple: `tp.web.daily_quote`. Then we just close it, come here, and activate it. Now we have a daily quote here, and every single time that we activate it, we should get a different quote. If I do Command + Z again and then activate it again, I'm getting a new quote here as well. This one by Shaquille O'Neal: "That's the first time I started hearing about money, money, money. I just want to play the game, drink Pepsi, and wear Reebok." Sometimes we don't get lucky. All jokes aside, most of the time, it's cool stuff that I'm happy to see. "The only real mistake is the one from which you learn nothing."
So, TP Web only has a daily quote and the random image. For me, I like to use the daily quote; maybe you like it as well.
All right, so let's now look at some examples that I personally use. As I said a few minutes ago, all of my daily notes have a line after the YAML header that says "On This Day," and it links to that exact same date of last year. To do that, we need to come here into our daily note template, which mine is over here in the Templates > Daily Note, and I just have a line here that says "On This Day," and I have `tp.date.now` and then `p-1y`. Don't forget to turn it into a link.
There's one more thing we need to do: we need to compare the Templater, and we need to trigger Templater on new file creation. So, I'm going to toggle this on, and let's create our daily note for today. Today is July 22nd. I'm going to press here to create, and as you can see, we now have a link here for July 22nd of last year. I don't have a note, so it's not going to take me anywhere because I don't have a note in this vault for that day, but if you do, it's going to take you to that specific daily note.
Another thing that I really enjoy adding on the daily notes is a sort of breadcrumbs. Let me show you what I mean. I'm going to come here to the daily note template, and I pulled this from my main vault because I'm going to type it out here. All this really is is a `tp.date.now` with a `-1` for the day before and a `+1` for the day after. These arrows here are just a signal that, hey, this one will take you to the day before, and this one to the day after.
So, let's pretend that we already have notes for the 23rd and for the 22nd. Now let's delete the 22nd; it's already deleted, and I'm going to create a note there. Now we have a link here that will take us to the daily note of the day before and the day after. I do a similar but different thing for the weekly planning, which, like I said, I typically do on Mondays. Just like the daily note, I also want to have a link, but this time I want to have that link for the current Monday. So, if I'm here on a daily note, say I'm here on the 22nd, I want to have a link here that takes me straight to the Monday of this week.
I like to have this week's Monday here as a link, so every time I click here, it'll take me to this week's Monday. Templater has a solution for this, and this is when `tp.date.weekday` comes into play. Now I put the usual YMD, and now in here we can have numbers: 0 is for Monday, 1 for Tuesday, 2 for Wednesday, and all that. This just takes the date that we're on today. Today is July 22nd, and you can see here this is the week we're on. Based on the number that you put here, if I put a 0, it's going to give me the Monday for this week, and the Monday for this week is July 18th.
So, let's see if it works. I'm going to put in a 0 and close all of this. I forgot to even open it; I just realized. Now, if I come here and I press Option + R to activate it, we now have a link to this week's Monday. Like I said, you can just change this here between 0 and 7, with 7 being next week's Monday. If I put in for Tuesday, Option + R, July 19th, as you can see, July 19th is this week's Tuesday.
So, let's now do folder automation. I'm going to create a new folder here, and I'm going to title it "Courses." So, new folder: Courses. I'm going to create a new template, and I'm going to title it "Courses Template." I'm going to grab this here from `tp.file`, and let's pretend we want a YAML header here. Let's put some typical stuff here on the YAML header, close it, and now let's say that we want this to show up on every file that we create inside the Courses folder.
So now we can come back here into the Templater settings, and in here, under Folder Templates, we need to create a new folder. I'm going to tell it to look for the Courses folder, and the template will be "Courses Template." Once we exit out of it and come here to the Courses folder and add a new note, we now have the template that we just created applied to it. The point of this is so that every folder in your vault has a predetermined template because you're not going to want to have the same template going for your Movies folder as you want for your Evergreen or your Articles folder.
For instance, this one here is for our Courses folder, but say that we have an Articles folder, and then here we add an Articles Template. These two can be as different or as similar as you want them to be. So, let's say that actually in the Courses Template, the alias doesn't make a lot of sense because you're not going to want to call your courses anything different than their actual name, but for the Articles, it actually makes more sense. So, you can keep an alias here but not on the Courses.
As always, these can be as complex as you want them to be. Maybe you want to have a bunch of `tp.date` and `tp.file` for specific templates only, and then you can come here to the Templater settings and then assign more and more with the plus sign. In here, you're just going to add, in this case, Articles, and it has its own dedicated template. So, in here, Articles Template, and then you can just keep adding more and more customized templates for every folder in your vault.
I also want to talk about using the hotkey functions in Templater. Although these are simple, these are a great quality of life feature, as you can assign different hotkeys for basically anything. In the Dataview video, we built up a Dataview table with rankings for different books, so we assigned different templates inside here for a star symbol. In here, for five stars, we have a five-star symbol, and we assigned it by Command + 5. So when I press Command + 5, we have the star symbols.
Another use case for the hotkeys is to have a shortcut for each of the templates that you don't want to use in a folder structure. For instance, I have a template for the People page that gets toggled by Option + P. I have another for meetings for Option + M, etc. This is good because I don't want to have all my authors inside an Author folder. If you do, then the Author folder is more straightforward.
This brings me to perhaps the most common way that I use Templater, which is to apply my default template. This is a template that I use on every note that creates spontaneously, and by this, I mean notes that I don't want to organize in a folder structure. As we just went over, the notes that are going into a specific folder have a specific template. So, just like all these other templates, I also have a default template inside the Templates folder.
I can come here and create a new note, which will be called "Default Template," and I'm going to pull this from my main vault, which I'll also leave in the description. I'm not going to go over this in detail, but really quickly, all that this here is is the YAML header. We have tags, aliases, last modified, created, title, and this bit here prompts me to add a title before the note even gets created and uses it to fill all of these parameters.
We're going to take a look at how this works, so for that, we need to come back here into the Templater plugin settings. We're not going to go into the folder templates; we're going to come here to add a new hotkey, and I'm going to call this one here the Default Template. For the hotkey, I'm going to choose Command + M, and I chose this because when I create a new note, I press Command + N, so it's convenient that the letter M is right next to it.
So, let's see this in action. I'm going to come here and close out of this. I'm going to press Command + N for a new note, followed by Command + M, and now it's asking me for a title. I'm going to put in "Example File," and now, as you can see, it filled up a bunch of stuff for me. We have title, created, last modified, aliases, tags, and then I just like to have the actual title over here as header one. I'm sure there's a more automated way of doing this so you don't have to press the letter M next to N every time, but it's so easy, and I'm so used to it that I really haven't felt the need to search for an alternative.
All right, so before we finish up, I want to go over two things, and one of them is Templater GitHub. It's extremely active, and the best part of it is that there's a bunch of people showcasing what they have done with Templater. I'm going to leave a link in the description for you guys to check out what others have created. It's important to note, however, that unless you understand what you're putting into your vault, you shouldn't just blindly copy and paste what you find there.
Lastly, let me know how you're using it, as I always like to see what others are using Templater for. This video was not exhaustive at all, and let me know if you want a more advanced video covering user scripts and the Moment object. Thanks for watching, and I'll see you in the next one. Bye!