Evan Harmon - Memex

Obsidian Templater Plugin

Run plugin command from a template

%% Get commands so you can get the id: %%
app.commands.commands
app.commands.executeCommandById(_)
app.commands.executeCommandById("obsidian-wikipedia:wikipedia-get-active-note-title")

app.workspace.activeLeaf.view.file

Manual pause, sleep, timeout, setTimeout

Run user script

You can define a templater user function that is bound to a local script. E.g., with that, then you can run whatever your system can run:

Update existing note's yaml/properties

<%*
setTimeout(() => {
  app.fileManager.processFrontMatter(tp.config.target_file, frontmatter => {

    // Update or add as many fields as you want
    frontmatter['Some property'] = 'Some value'
    frontmatter['Another property'] = 'Some other value'

    // You can even remove properties if you want
    delete frontmatter['Unwanted property']

  })
}, 200) // the reason for the timeout is to let the template complete first
-%>

Do something after all templates have finished running

Other objects and functions available

tp.obsidian - Templater
app.vault.getAllLoadedFiles()
tp.obsidian.htmlToMarkdown("\<h1>Heading\</h1>\<p>Paragraph\</p>")

// HTTP request
<%*
const response = await tp.obsidian.requestUrl("https://jsonplaceholder.typicode.com/todos/1");
tR += response.json.title;
%>

file.

  • path
  • parent
  • name
  • extension
  • basename

Originating (From) File, eg. like Note Refactor Plugin

Originating file: <% tp.config.active_file.basename %>
Link to that file: [[<% tp.config.active_file.path %> | My parent ]]

<%* let last_file = "" let recent_leaf = this.app.workspace.getMostRecentLeaf(); let back_history = recent_leaf.history.backHistory;
%>

tR var

Sometimes, you may want to output something when using a JS execution command.

When our templating engine generates a replacement string using all of our commands results, it is stored in a variable named tR. This is the string that will contain the processed file content. You are allowed to access that variable from a JS execution command.

This means that, to output something from a JS execution command, you just need to append what you want to output to that tR string variable.

For example, the following command: <%* tR += "test" %> will output test.

Obsidian Templater Plugin
Interactive graph
On this page
Obsidian Templater Plugin
Run plugin command from a template
Manual pause, sleep, timeout, setTimeout
Run user script
Update existing note's yaml/properties
Do something after all templates have finished running
Other objects and functions available
Originating (From) File, eg. like Note Refactor Plugin
tR var