Templater plugin (1.5 and newer): New Syntax and Examples

Hey @janpeeters, thanks for your interest!

These are not “self-updating” (though at least the moment.js stuff could in theory be used on a web page), but intended to be used as templates, i.e. called up and used only once (on invocation of the template).

Most people would probably use some of these with their “Daily Notes” template, to create links or nice-looking Date/Time strings. Others might keep some in small “snippet” template files (often in a separate “templates” folder) and call them up when needed, for instance with the Alt/Opt+E key combination.

Here are some possible usage ideas:

  • Within the “Daily Notes” template, to create links like “Yesterday’s Note”, “Tomorrow’s Note”, “Weekly Note”, and so on.
  • For multilingual people, to easily create standardized date/time strings in the other languages.
  • Within to-dos and calendar-type notes like “only xx days till our anniversary—get a present”.
  • “Just because we can.” Because it’s great to play with possibilities (and get distracted from the real work, hee hee …)

Have fun finding your use case!

4 Likes

Hi @Moonbase59 thanks for all your info, this clarifies a lot. I hope you don’t mind if I have some follow-up questions. If this gets too much off topic I hope I can contact you via pm.

It’s a pity that the info cannot be updated after e.g. changes to a note. I thought that the Templater: Replace templates in the active file function would be able to do this. Do you know what this function does?

Do you know of a way how to have an updating ‘last modified’ header in the note?

It replaces templater code within the currently open file, so you don’t have to load an external template. Like, you type <% tp.file.last_modified_date() %>, then press Alt/Opt+R and you’ll get the file’s last modified date and time.

I never had much use for this (except for testing), since the templater code is replaced with its result and thus gone afterwards.

Nope. If you find one, let me know :wink: Then again, Obsidian’s File Explorer pane shows the last modified date if you hover the mouse over a file, and it could possibly be retrieved “live” using the Dataview plugin.

We must remember, after all, that Obsidian is a system to create/handle plain Markdown text notes and not a “live”, variable- or event-driven system like a website could be.

1 Like

Thanks a lot! Crystal clear.

1 Like

FYI, here’s a link to the Templater Docs.

Since this thread is not only about durations anymore, I’ll change its title to “New Syntax and moment.js Examples” :slight_smile:

1 Like

Here’s another example that came up today: Make a H1 heading that has the first letter of the note’s title uppercased.

# <% tp.file.title.charAt(0).toUpperCase() + tp.file.title.slice(1) %>

Shows nicely how to unleash the powers of Javascript using the Templater plugin.

2 Likes

Another one: Cursor placement in Templates plugin (preferably also for Daily Notes template) - #7 by Moonbase59

1 Like

I asked the author of Dataview on GitHub and it is possible:
Put an inline codeblock like this in your file with this code.

`= this.file.mtime`
2 Likes

Now this is a great find—thank you!

Now we’ll only have to wait for …

  • date/time string formatting
  • date/time strings being localized into the currently set Obsidian language, not the system language
2 Likes

Templater 1.6+: Combining dynamic commands and Javascript commands (undocumented)

Say you have a front matter field deceased: but only want to calculate/show the date if it’s defined (or non-empty), and a simple - if not.

You can actually combine Templater’s <%+ (dynamic) and <%* (Javascript) “introducers” as follows:

Use <%+*—this should produce dynamic results (if I’m not being caught by caching again):

<%+* if (tp.frontmatter["deceased"]) {tR+=moment(tp.frontmatter.deceased).format("ddd, YYYY-MM-DD")} else {tR+="-"}; %>

Now isn’t Templater great?

8 Likes

Thanks for this discovery! This should be in the documentation already. I was looking for this since the day dynamic commands were released but in my attempts to try this, it never worked, I think I tried the asterisk before the plus and then tried the documentation and gave up. This could be so useful especially since we can call external APIs using fetch inside the template and process it as we like with JS.

Life is full of possibilities!

3 Likes

In case it’s helpful, here’s syntax for adding next/back date links to your daily note based on the note title – not today’s date:

<< [[<% tp.date.now("YYYY-MM-DD",-1,tp.date.now("YYYY-MM-DD", 0, tp.file.title, "YYYY-MM-DD"),"YYYY-MM-DD") %>]] | [[<% tp.date.now("YYYY-MM-DD",1,tp.date.now("YYYY-MM-DD", 0, tp.file.title, "YYYY-MM-DD"),"YYYY-MM-DD") %>]] >>

This assumes files are named in YYYY-MM-DD format; you’ll need to update all 8 date format declarations if you use a different format.

5 Likes

When I try to do the “Days until Christmas” example. I get “PT0.219S.” What am I doing wrong? It’s been years since I’ve done anything like this.

4 Likes

Hm. Thanks for bringing this up!

Something must have changed, or I made a stupid mistake. I also get PT0.219S (ISO duration, meaning 0.219 seconds) where it should be 219 days.

At least

<% moment("12-24", "MM-DD").diff(moment(), "days") %>

shows 219 (the correct # days). Which should be enough for the intended “days til Xmas”.

If, for some reason, it needs to be more exact, one could probably use

<% moment.duration(moment("12-24", "MM-DD").diff(moment())).as('days') %>

to arrive at 219.278469375 (at this moment in time).

Sorry if I messed something up!

4 Likes

That totally worked.

…can I ask you an off-topic question because I can’t for the life of me figure out how to search for “u”…is {{date:u}} just impossible?

1 Like

Sounds like PHP, right? u is the timestamp in integer microseconds, right? I don’t think moment.js ever supported that.

In case of using (PHP) v (milliseconds), this is x with moment.js.

u is the number day of the week, with 1 being Monday.

it’s not vital, I’m just trying to get something to link to files in the format I had already used. I really appreciate you trying to help!

I just like interesting problems :wink:

Day-of-the-week could be either d, e or E, according to the docs. For today, they all show 2 here:

<% moment().format('d - e - E') %>

You probably want E which returns 1–7 for Mon–Sun.

2 Likes

E worked! you are fantastic. :heart_eyes:

2 Likes

Happy to help :slight_smile:

1 Like