Templater is a Plugin for Obsidian that is used to Create new Notes based on a Template. The Templater plugin uses it's own Syntax to execute certain tasks when creating a Note out of a Template or inserting a Template. This Document lists some example Templater Code. ## Templater Configuration There are a lot of advanced options like user scripts or system commands that can be enabled for the Templater Plugin, for now we will focus on the base settings every Templater Plugin should be configured for. Enable the Automatic jump to cursor and set the Template Folder location, the Templater Plugin will look inside that folder for template files. ![[_media/Pasted image 20250407204850.png||800]] At this point you may also want to define some hotkeys inside Obsidian for the Templater plugin, particularly the `Insert Template` and `Create new Note from Template` are useful to assign. ![[_media/Pasted image 20250407205323.png]] ## Templater Syntax When the Templater is triggered, it looks inside the Template file for any Templater Code to execute. Templater Code is always surrounded by `<`/`>` than signs and `%` , a multiline Templater Code block is additionally started with an asteriks `*` after the first `%` Example: ```Templater <% Single Line Templater Code %> <%* Multi line Templater Code %> ``` Comments are written in C/C++ Style by starting with `//` and for multiline surrounding with `/*` and `*/` like this: ```cpp // Single line comment /* Multi line Comment */ ``` ## Templater Workflow There are 2 main ways to use the Templater function, either you `create a new note from Template` or you `Insert a Template`. There is one Important consideration between those two functions: ### Create a new note from Template This Function opens a selection modal and lets you select a Template from your configured Template folder that is going to create a new Note. 1. Templater opens Template file and looks for Template code 2. Templater executes the Template code 3. Obsidian creates the new file Use Case: Creating quickly new notes without affecting the current active note and executing Templater code during the process. ### Insert a Template This function opens a selection modal and lets you select a Template which is inserted into the current active note. 1. Templater opens Template file and looks for Template code 2. Templater executes the Template code 3. Templater inserts the remaining Template into the active note. Use Case: Inserting the Format of an already existing Template and executing Templater code on the current active note. ### Special Use cases There is one particular use case for the `Insert a Template` Method that is not very obvious first. For Templater Code that should be run before actually creating a new Note it might make sense to use the `Insert a Template` Method even when you want to create a completely new note. This allows for example to check if a file with the same name you want to create already exists and switch to it. If the file does not exist it will trigger the creation of the Template as usual. For this you want to create a 'Script Note' which only contains Templater code and executes the Template itself if necessary. For an Example See ## Templater Examples ### Moving a note into a specific Directory and renaming it ``` <%* // Define the Folder Path where the note should be moved into const FolderPath = "/Intern/Meetings/"; // Define Title of note const Title = tp.date.now("YYYY-MM-DD") + " Meeting"; // Move note into the Directory await tp.file.move(FolderPath + tp.file.title); // Rename the file await tp.file.rename(Title); %> ``` ### Checking if a note exists already and switching to it This example is executed with the `Insert a Template` method and is split into 2 parts, the `Script Note` code and the `Template` code. This Example checks if a specific Note already exists, if it does it will open it instead of creating a new Note. The `Script Note` code should be stored in the configured Template folder in a subfolder, the Script Note should only contain the code, nothing else, it is executed with the `Insert a Template` method. The actual Template should be stored in the base folder with all of the other wanted format. It is used to create a new note inside the folder structure when the condition inside the `Script Note` is met. Script Note: ``` <%* // Define path to folder where the meeting note should be created const FolderPath = "/Intern/Meetings/Weekly" + "/" + tp.date.now("YYYY") + "/" + tp.date.now("MM"); // Define Title of Meeting note const Title = tp.date.now("YYYY-MM-DD") + " Meeting Weekly"; // Construct Full Path of Meeting Note const FullPath = `${FolderPath}/${Title}.md`; // Check if file already exists in the path const FileExists = await tp.file.exists(FullPath); // If the file does not exist, create it if (!FileExists) { tp.file.create_new(tp.file.find_tfile("_Template Meeting Weekly"), Title, true, app.vault.getAbstractFileByPath(FolderPath)); } // Else, open the already existing File else { const pathTFile = tp.file.find_tfile(Title); app.workspace.getLeaf("tab").openFile(pathTFile); } %> ``` Template: ``` <% await tp.file.move("/Intern/Meetings/Weekly/" + tp.date.now("YYYY") + "/" + tp.date.now("MM") + "/" + tp.file.title) %> <% tp.file.rename(tp.date.now() + " Meeting Weekly") %> ```