Script User Functions

This type of user functions allows you to call JavaScript functions from JavaScript files and retrieve their output.

To use script user functions, you need to specify a script folder in Templater's settings. This folder needs to be accessible from your vault.

Define a Script User Function

Let's say you specified the Scripts folder as your script folder in Templater's settings.

Templater will load all JavaScript (.js files) scripts in the Scripts folder.

You can then create your script named Scripts/my_script.js (the .js extension is required) for example. You will likely have to create the file outside of Obsidian, as Obsidian only creates markdown files.

You will then be able to call your scripts as user functions. The function name corresponds to the script file name.

Scripts should follow the CommonJS module specification, and export a single function.

function my_function (msg) {
    return `Message from my script: ${msg}`;
}
module.exports = my_function;

In this example, a complete command invocation would look like this:

<% tp.user.my_script("Hello World!") %>

Which would output Message from my script: Hello World!.

Global namespace

In script user functions, you can still access global namespace variables like app or moment.

However, you can't access the template engine scoped variables like tp or tR. If you want to use them, you must pass them as arguments for your function.

Functions Arguments

You can pass as many arguments as you want to your function, depending on how you defined it.

You can for example pass the tp object to your function, to be able to use all of the internal variables / functions of Templater: <% tp.user.<user_function_name>(tp) %>