Using template snippets to insert predefined content to SiYuan Note
Templates
- Markdown files. You put them in data/templates.
- Templating logic with Go’s text/template.
.action{action}
instead of{{action}}
- Additional functionality via Sprig
- Remember Go’s thing about date formatting
Template Variables
Beyond whatever’s defined by Sprig
Variable | Description |
---|---|
title |
current document’s name |
id |
current document’s id |
name |
current document name 🤔 |
alias |
current document alias |
queryBlocks |
result of a db query as blocks |
querySpans |
result of a db query as spans |
parseTime |
parse a string into time.Time |
Template Invocation
Command: /Template
- Invoking
/Template
pulls up template list along with expected output. - Select template
- Template is entered at point
Resources
Date Formatting
Date formatting in templates.
Sprig
Useful template functions for Go Templates
Pasting from their front page until I dig deeper on my own:
The Sprig library provides over 70 template functions for Go’s template language.
- String Functions: trim, wrap, randAlpha, plural, etc.
- String List Functions: splitList, sortAlpha, etc.
- Integer Math Functions: add, max, mul, etc.
- Integer Slice Functions: until, untilStep
- Float Math Functions: addf, maxf, mulf, etc.
- Date Functions: now, date, etc.
- Defaults Functions: default, empty, coalesce, fromJson, toJson, toPrettyJson, toRawJson, ternary
- Encoding Functions: b64enc, b64dec, etc.
- Lists and List Functions: list, first, uniq, etc.
- Dictionaries and Dict Functions: get, set, dict, hasKey, pluck, dig, deepCopy, etc.
- Type Conversion Functions: atoi, int64, toString, etc.
- Path and Filepath Functions: base, dir, ext, clean, isAbs, osBase, osDir, osExt, osClean, osIsAbs
- Flow Control Functions: fail
- Advanced Functions
- UUID Functions: uuidv4
- OS Functions: env, expandenv
- Version Comparison Functions: semver, semverCompare
- Reflection: typeOf, kindIs, typeIsLike, etc.
- Cryptographic and Security Functions: derivePassword, sha256sum, genPrivateKey, etc.
- Network: getHostByName
Related
Backlinks
Added to vault 2024-01-28. Updated on 2024-01-28