Template Helpers

Summary

Built-in helper functions that can be used with handlebar templates

Helpers

eq

  • return: true|false

Compares two arguments for equality.

---
arg1: 1
arg2: 1
---
{{eq fm.arg1 fm.arg2}}

fnameToDate

  • return: javascript Date object
{{! given file: daily.journal.2022.05.31 }}
{{! results in new Date(2022, 4, 31) (javascript Date uses 0-based month) }}
{{ fnameToDate }}

Extract the date portion of the current filename. By default, it will match the first section of the filename that matches (?<year>[\d]{4}).(?<month>[\d]{2}).(?<day>[\d]{2}) and create a date object You can override by passing in a custom format as long as you used named captures for year, month, and day

getDayOfWeek

  • return: number, day of week
{{! returns 2 since 2022-05-31 was a tuesday}}
{{ getDayOfWeek (fnameToDate) }}

Given a day, return the day of the week. This is the same as javascript getDay method (0 = sunday, 6 = saturday)

  • NOTE: be sure to add parenthesis () when using with fnameToDate due to how handlebars handles subexpressions

match

  • parameters:
    • text: text where pattern will be applied against
    • pattern: pattern to capture
  • return: capture text or false
{{! given file: daily.journal.2022.05.31 }}
{{! results in: [[writing.journal.2022.05.31]] }}
[[writing.journal.{{ match FNAME "\d{4}.\d{2}.\d{2}" }}]];

On error, will return false which means you can use it inside of if statements

{{#if (match FNAME 2022) }}
It's still 2022
{{else}}
It's no longer 2022
{{/if}}

{{! given filie daily.journal.2021.01.01, 
will output 
It's no longer 2022  }}

Examples

Conditionally add a block depending on day of week

  • NOTE: since we are getting the day of the week based on the file name instead of the current day, this will work even when you create the note in advance
{{#if (eq (getDayOfWeek (fnameToDate)) 0) }}
- [ ] do laundry
{{/if}}

Backlinks