notesy::templates

Templates are notes to put in a note, or start one from: the notes in the templates folder (Settings, Daily notes, says which; Templates unless it says), put in with Insert template and New note from template in the palette, or from the sidebar (right-click a template, a folder, a New note button, or the room around the notes), and the daily note's template too. What's written in them to fill in is filled in as they're used. A plugin can add functions of its own for them to call, which takes the templates permission.

#What a template can say

Obsidian's way and Templater's are both read, in any template.

Written Filled in with
{{date}}, {{date:dddd, MMMM D}} the day it's for (today, or a daily note's), in a format
{{time}}, {{time:HH:mm:ss}} now
{{yesterday}}, {{tomorrow}} the days around it, formats as date's
{{title}} the note's name
{{folder}}, {{path}} its folder in the vault, and its path
{{selection}}, {{clipboard}} what was selected where it's put in, and what's copied
{{cursor}} nothing: where the caret goes once it's in
Written Filled in with
<% tp.date.now(format, offset) %> the day, moved by offset: a number of days (7, -1), or "P1W", "P1M", "P1Y"
<% tp.date.tomorrow(format) %>, <% tp.date.yesterday(format) %> the days around it
<% tp.date.weekday(format, n) %> this week's day n, Sunday its first (0)
<% tp.file.title %> the note's name
<% tp.file.folder() %>, <% tp.file.folder(true) %> its folder's name, or its path in the vault
<% tp.file.path() %> its path in the vault
<% tp.file.creation_date(format) %> when it's made
<% tp.file.selection() %>, <% tp.system.clipboard() %> what was selected, and what's copied
<% tp.file.cursor() %> nothing: where the caret goes once it's in
<% tp.system.prompt(question, default) %> what the user types
<% tp.system.suggester(labels, values, false, question) %> the value beside the one of labels the user picks

Formats are Obsidian's (moment.js tokens: YYYY, MM, DD, dddd, HH:mm, [literal text]), or strftime's when they have a %. Every question a template asks (its prompts and suggesters) is asked at once, in one dialog, before it's put in; Cancel puts nothing in. <%- and -%> take the line break before or after the tag with them, and <%_ and _%> all the space.

What a function is given is plain values: text in quotes, numbers, true and false, and lists of them in [ ]. Nothing in a template is run: Templater's <%* … %> stays as it's written, as does anything notesy doesn't know, so a template shared by someone can't do anything but fill itself in.

A template with template-for: <folder> in its front matter starts every new note made in that folder (template-for: Meetings); that line isn't put in the note. Right-click a template and pick Use for new notes in… to set it (a folder has one template: the one it had stops). A note made from a template says which in its template property (template: "[[Meeting]]").

#Functions of a plugin's own

rustuse notesy::{net, templates};

pub fn ready() {
    // <% tp.weather.today("Bangor") %>
    templates::add("today", |call| {
        let town = call.args.get(0).unwrap_or("Cardiff");
        net::fetch(`https://wttr.in/${net::encode(town)}?format=3`, |result| {
            let text = match result {
                Ok(response) => response.text,
                Err(e) => "no forecast",
            };
            templates::answer(call.id, text);
        });
    });
    // <% tp.weather.greeting() %>: answered at once.
    templates::add("greeting", |call| `Bore da from ${call.note.title}`);
}
Function What it does
templates::add(name, handler) A function templates call as tp.<plugin>.<name>(…). <plugin> is the last part of the plugin's id, - as _ (example.weather is tp.weather). handler(call) gets #{ args, note, id }: what it was given (a list), the note (#{ title, path, folder }) and the call's id. It returns what to put in, text or a number, or nothing, to answer later with templates::answer. Adding it again changes it.
templates::answer(id, text) What to put in for call id, from a function that answers once it knows.

A template waits 4 seconds for its plugins' functions (while the user answers its questions, too); a call with no answer by then, or to a function that isn't there or fails, stays in the note as it was written. Two plugins can't both be tp.weather: the second's functions aren't added, and its log says so.

Every page