Obsidian Templater Plugin Cheat Sheet
NOTE
This Cheatsheet is for the *Obsidian Community Plugin Templater.
Sources:
This cheat sheet is based on the Official Templater Documentation.
Start and End Syntax
- All Templater Code is placed within the start and end Templater “tags”.
for example:
Functions in Templater
can be called in command snippets
Types of functions
Object hierarchy
tp.'function'.*
Function invocation
Function documentation syntax
Where:
arg_name
represents a symbolic name for the argument, to understand what it is.type
represents the expected type for the argument. This type must be respected when calling the internal function, or it will throw an error.
If an argument is optional, it will be appended with a question mark ?
, e.g. arg2_name?: type
If an argument has a default value, it will be specified using an equal sign =
, e.g. arg3_name: type = <default_value>
.*
If an argument can have different types, it will be specified using a pipe |
, e.g. arg4_name: type1|type2
*
Function Modules
Config Module
The active file (if existing) when launching Templater.
The RunMode
, representing the way Templater was launched (Create new from template, Append to active file, …)
The TFile
object representing the target file where the template will be inserted.
The TFile
object representing the template file.
Date module
Retrieves the date.
Arguments:
-
format
: Format for the date, refer to format reference -
offset
: Offset for the day, e.g. set this to-7
to get last week’s date. You can also specify the offset as a string using the ISO 8601 format -
reference
: The date referential, e.g. set this to the note’s title -
reference_format
: The date reference format.
Retrieves tomorrow’s date.
Arguments:
format
: Format for the date, refer to format reference
Arguments:
-
format
: Format for the date, refer to format reference -
reference
: The date referential, e.g. set this to the note’s title -
reference_format
: The date reference format. -
weekday
: Week day number. If the locale assigns Monday as the first day of the week,0
will be Monday,-7
will be last week’s day.
Retrieves yesterday’s date.
Arguments:
format
: Format for the date, refer to format reference
Moment.js
Templater gives you access to the moment
object, with all of its functionalities.
More informations on moment.js here
File Module
Retrieves the file’s content
Creates a new file using a specified template or with a specified content.
Arguments:
-
filename
: The filename of the new file, defaults to “Untitled”. -
folder
: The folder to put the new file in, defaults to obsidian’s default location. -
open_new
: Whether to open or not the newly created file. Warning: if you use this option, since commands are executed asynchronously, the file can be opened first and then other commands are appended to that new file and not the previous file. -
template
: Either the template used for the new file content, or the file content as a string.
Retrieves the file’s creation date.
Arguments:
format
: Format for the date, refer to format reference
Sets the cursor to this location after the template has been inserted.
You can navigate between the different tp.file.cursor using the configured hotkey in obsidian settings.
Arguments:
order
: The order of the different cursors jump, e.g. it will jump from 1 to 2 to 3, and so on. If you specify multiple tp.file.cursor with the same order, the editor will switch to multi-cursor.
Appends some content after the active cursor in the file.
Arguments:
content
: The content to append after the active cursor
Checks if a file exists or not. Returns a true / false boolean.
Arguments:
filename
: The filename of the file we want to check existence, e.g. MyFile.
Retrieves the file’s folder name.
Arguments:
relative
: If set to true, appends the vault relative path to the folder name.
Includes the file’s link content. Templates in the included content will be resolved.
Arguments:
include_link
: The link to the file to include, e.g. MyFile, or a TFile object. Also supports sections or blocks inclusions, e.g. MyFile#Section1
Retrieves the file’s last modification date.
Arguments:
format
: Format for the date, refer to format reference.
Moves the file to the desired vault location.
Arguments:
new_path
: The new vault relative path of the file, without the file extension. Note: the new path needs to include the folder and the filename, e.g. /Notes/MyNote
Retrieves the file’s absolute path on the system.
Arguments:
relative
: If set to true, only retrieves the vault’s relative path.
Renames the file (keeps the same file extension).
Arguments:
new_title
: The new file title.
Retrieves the active file’s text selection.
Retrieves the file’s tags (array of string)
Retrieves the file’s title.
Frontmatter Module
Retrieves the file’s frontmatter variable value.
If your frontmatter variable name contains spaces, you can reference it using the bracket notation like so:
<% tp.frontmatter["variable name with spaces"] %>
Obsidian Module
This module exposes all the functions and classes from the obsidian API.
This is mostly useful when writing scripts.
Refer to the obsidian API declaration file for more informations.
Systems Module
Retrieves the clipboard’s content
Spawns a prompt modal and returns the user’s input.
Arguments:
-
default_value
: A default value for the input field -
prompt_text
: Text placed above the input field -
throw_on_cancel
: Throws an error if the prompt is canceled, instead of returning anull
value
Spawns a suggester prompt and returns the user’s chosen item.
Arguments:
-
items
: Array containing the values of each item in the correct order. -
placeholder
: Placeholder string of the prompt -
text_items
: Array of strings representing the text that will be displayed for each item in the suggester prompt. This can also be a function that maps an item to its text representation. -
throw_on_cancel
: Throws an error if the prompt is canceled, instead of returning anull
value
Web Module
Retrieves and parses the daily quote from the API https://api.quotable.io
Gets a random image from https://unsplash.com/
Arguments:
-
query
: Limits selection to photos matching a search term. Multiple search terms can be passed separated by a comma,
-
size
: Image size in the format<width>x<height>
User Functions
Generally there are two types:
1. Script user functions
2. Systemcommand user functions
They can be invoked by:
Script User Functions
First you should define your scripts folder in the settings. You will then be able to call .js files from it. For further information about Javascript click here.
The function call name corresponds to the script name.
System Commands
To define it go to the Templater plugin settings and associate a function name with a working system function like curl or echo. You can define the desired shell binary in the settings too.
You can pass function arguments, which need to be js-Objects. They will be avaliable as environment arguments.
Note that you can use internal Templater functions inside of System commands.
The invocation is under the namespace
tp.user.*
Command Types
<%
: Raw display command. It will just output the expression that’s inside.<%~
: Interpolation command. Same as the raw display tag, but adds some character escaping.<%*
: JavaScript execution command. It will execute the JavaScript code that’s inside. It does not output anything by default.
The closing tag for a command is always the same: %>
In addition there are two command utilities .
Dynamic commands
They will be resolved upon entrance of the preview mode.
To declare a command dynamic add a plus sign after the opening tag. Like this:
Execution Commands
Allow to execute Javascript
.
They allow for global namespace variables.
The JS templating engine Eta allows for the return of execution functions to be parsed as a string which is stored in the variable tR
.
This can be used for multiple purposes. You can append something to that string. This can be quite handy for debugging purposes.
Whitespace Control
By default, commands in Templater are not removing any newlines. Commands are replaced with their values and that’s it.
It can sometimes be useful to have some whitespace control after commands are inserted, which is exactly what this command utility offers.
Let’s have an example. The following template:
<%* if (tp.file.title == "MyFile" ) { %>
This is my file!
<%* } else { %>
This isn't my file!
<%* } %>
Some content ...
Will produce the following output if the condition is false (the same happens when it’s true), notice the blank lines:
This isn't my file!
Some content ...
You may want to remove the blank lines produced by the execution commands, that do not produce any output.
A specific syntax exists for whitespace control:
- An underscore
_
at the beginning of a tag (<%_
) will trim all whitespace before the command - An underscore
_
at the end of a tag (_%>
) will trim all whitespace after the command - A dash
-
at the beginning of a tag (<%-
) will trim one newline before the command - A dash
-
at the end of a tag (-%>
) will trim one newline after the command.
In our example, to fix our template to remove the blank lines, we would use the following template (notice the dashes -
at the end of the tags), to remove the blank newlines after the execution commands:
<%* if (tp.file.title == "MyFile" ) { -%>
This is my file!
<%* } else { -%>
This isn't my file!
<%* } -%>
Some content ...
Which would produce the following output:
This isn't my file!
Some content ...
Appendix
Note created on 2024-04-18 and last modified on 2024-04-18.
Backlinks
(c) No Clocks, LLC | 2024