aliases:
- Templater plugin
tags:
- Type/Tech/Platform/Obsidian
- area/tech/obsidian
publish: true
version: 1
dateCreated: 2023-08-17, 07:59
dateModified: 2024-02-26, 15:14
from:
- "[[Obsidian Plugins]]"
related:
contra:
to:
url:: Obsidian Templater Plugin | |
---|---|
docs:: Introduction - Templater |
%% 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
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:
<%*
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
-%>
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.
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;
%>
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.