Templates - eevdriet/obsidian-anki GitHub Wiki
A template can be used to define how notes are imported into and exported from Obsidian. Each template consists of a combination of literal text and replacement patterns. To define notes in a more flexible way, you can also use regular expressions.
Patterns
Each pattern is typeset with delimiters {{
and }}
and is either a note pattern or field pattern.
Note patterns
A note pattern sets some property of the note directly from the note itself. Currently, the following note patterns are available:
{{Deck}}
sets the deck the note is exported to{{Tags}}
sets the tags that are attached to the note in Anki
When {{Deck}}
or {{Tags}}
is set directly from the note, it takes precedence over other places where the deck/tags may be defined.
See the Notes page for more information on how the deck & tags are determined for each note.
Field patterns
A field pattern sets one of the fields for the given note type.
For example, the Basic
note type has field patterns {{Front}}
and {{Back}}
.
If you've used a regular expression before, you can think of a field pattern like a capture group.
A special field pattern is {{Fields}}
, which should set one or more fields each on their own line.
When matched in a note, it matches each line of the note as {{Field}}: {{Value}}
.
For example, consider how it can be used for the Basic
note type:
Front: el gato <!-- OK: "Front" is set to "el gato" and "Back" is set to "the cat" -->
Back: the cat
Front: el gato <!-- OK: "Front" is set to "el gato" and "Back" is not set -->
<!-- ERROR: {{Fields}} must set at least one field -->
Requirements
For a template to be valid, it needs to meet the following requirements:
- At least one field pattern needs to be included in the template that matches the note type
- At least one literal text string needs to be included in the template. If not, any text can be matched against the template which is clearly not useful.
- When two patterns are placed on the same line, literal non-whitespace text needs to separate them.
Consider
{{Front}} {{Back}}
: there is no way of determining what part of the line is matched to{{Front}}
and which part to{{Back}}
. - If no field pattern is included,
{{Fields}}
needs to be included - No patterns may be included outside of field and note patterns.
For example, for the
Basic
note type, only the following patterns are allowed:{{Front}}
(field){{Back}}
(field){{Fields}}
(field){{Deck}}
(note){{Tags}}
(note)
The rule modal will display a helpful error message if one or more of the requirements are not met.
Examples
To get an understanding of how templates work in practice, below are some examples. These highlight what errors might come up when defining your own templates and when notes are or are not matched to a given template.
Common errors
The template can never be empty
The template should include {{Front}}
, {{Back}}
or {{Fields}}
to make it valid
The template includes invalid pattern {{Middle}}
, which should be removed
Matching notes
For these examples, we will use the Basic
note type.
- First consider the following template:
Below are some notes defined in the text and whether they match the template.Deck: {{Deck}} {{Tags}} {{Front}} -> {{Back}}
Deck: Language::Spanish <!-- MATCH: note exported with --> <!-- - {{Deck}} set as "Language::Spanish" and {{Tags}} not set --> el gato -> the cat <!-- - {{Front}} set as "el gato" and {{Back}} set as "the cat" --> Deck: <!-- MATCH: note exported with --> language/spanish, language/word <!-- - {{Deck}} not set and {{Tags}} set as ["language/spanish", "language/word"] --> el gato -> <!-- - {{Front}} set as "el gato" and {{Back}} not set --> Deck: <!-- MATCH: note exported with --> <!-- - {{Deck}} and {{Tags}} not set --> -> the cat <!-- - {{Front}} not set and {{Back}} set as "the cat" --> Deck: Language::Spanish <!-- NO MATCH: literal text "->" in the template is not matched --> language/spanish, language/word el gato - the cat