Templater and DataView Snippets

Here are some Templaterand DataView-Snippets I use in Obsidian.

Sources

Snippets

The Snippets are not sorted or in any particular order. Some of them are written by my self, others are copied over.

Daily notes block that only shows on certain days of the week

The following daily notes template has two blocks. The first block will only show on daily notes that land on a Monday. The second block will show on every daily note.

<%* if (tp.date.now("dddd", 0, tp.file.title, "YYYY-MM-DD") === "Monday") { %>
Block that only shows on Mondays
<%* } %>
Block that shows every day

We can pass in a format, offset, reference, and reference format toย tp.date.nowย to create links to yesterday and tomorrow relative to the active daily note, instead of being relative to the actual current date.

yesterday 

tomorrow 

Escaping frontmatter in a template

If you donโ€™t want your frontmatter in your templates to be parsed by Obsidian or any Obsidian plugins (like Dataview), then you can escape your frontmatter by wrapping the starting and endingย ---ย with Templater tags to escape it.

<% "---" %>
date: <% tp.date.now() %>
key: value that is not frontmatter because the --- is escaped
<% "---" %>
// Rest of template below

Get result from Modal Forms

<%*
const values = {
    name: tp.file.title,
};

const result = await tp.user.openForm('add_person', { values: values });

let personName = result.get("name")
await tp.file.rename(personName)

let personTags = result.get("tags")
let personMail = result.get("email")
let personCompany = result.get("company")
let personPartner = result.get("partner")
-%>

Here is the required snippet. Save as openForm.js in your templater snippets folder.

const modalForm = app.plugins.plugins.modalforms.api;
module.exports = (formName, options) => modalForm.openForm(formName, options);

Embed YouTube video

<%*
const url = await tp.system.clipboard()
const response = await fetch(`https://youtube.com/oembed?url=${url}&format=json`)
const data = await response.json()

const title = data.title.replaceAll("", "").replaceAll('"', '').replaceAll("\\", "").replaceAll("/", "").replaceAll("<", "").replaceAll(">", "").replaceAll(":", "").replaceAll("|", "").replaceAll("?", "")
const author = data.author_name
const author_url = data.author_url
const html = data.html
const thumbnail = data.thumbnail_url

const regex = /(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/
const m = regex.exec(url)
-%>
> [!youtube]- YouTube
> **Title:** <% title %>
> **Channel:** [<% author %>](<% author_url %>)
> **Link:** [View on YouTube](https://www.youtube.com/watch?v=<% m[1] %>)
> <iframe src="https://www.youtube-nocookie.com/embed/<% m[1] %>?vq=hd1080&modestbranding=1&rel=0&iv_load_policy=3" width="569" height="317" frameborder="0" style="margin: 0 auto; display: block;"></iframe>
<%*
const result = await tp.user.openForm('add_family');

let parent = result.get("parent")
-%>
 und [[]]