anki.template#
This file contains the Python portion of the template rendering code.
Templates can have filters applied to field replacements. The Rust template rendering code will apply any built in filters, and stop at the first unrecognized filter. The remaining filters are returned to Python, and applied using the hook system. For example, {{myfilter:hint:text:Field}} will apply the built in text and hint filters, and then attempt to apply myfilter. If no add-ons have provided the filter, the filter is skipped.
Add-ons can register a filter with the following code:
from anki import hooks hooks.field_filter.append(myfunc)
This will call myfunc, passing the field text in as the first argument. Your function should decide if it wants to modify the text by checking the filter_name argument, and then return the text whether it has been modified or not.
A Python implementation of the standard filters is currently available in the template_legacy.py file, using the legacy addHook() system.
Attributes#
Classes#
Holds information for the duration of one card render. |
|
Stores the rendered templates and extracted AV tags. |
Functions#
|
|
|
|
|
|
|
Complete rendering by applying any pending custom filters. |
Module Contents#
- anki.template.TemplateReplacementList#
- class anki.template.PartiallyRenderedCard#
- qnodes: TemplateReplacementList#
- anodes: TemplateReplacementList#
- css: str#
- latex_svg: bool#
- is_empty: bool#
- classmethod from_proto(out: anki.card_rendering_pb2.RenderCardResponse) PartiallyRenderedCard#
- static nodes_from_proto(nodes: collections.abc.Sequence[anki.card_rendering_pb2.RenderedTemplateNode]) TemplateReplacementList#
- anki.template.av_tag_to_native(tag: anki.card_rendering_pb2.AVTag) anki.sound.AVTag#
- anki.template.av_tags_to_native(tags: collections.abc.Sequence[anki.card_rendering_pb2.AVTag]) list[anki.sound.AVTag]#
- class anki.template.TemplateRenderContext(col: anki.collection.Collection, card: anki.cards.Card, note: anki.notes.Note, browser: bool = False, notetype: anki.models.NotetypeDict | None = None, template: dict | None = None, fill_empty: bool = False)#
Holds information for the duration of one card render.
This may fetch information lazily in the future, so please avoid using the _private fields directly.
- static from_existing_card(card: anki.cards.Card, browser: bool) TemplateRenderContext#
- classmethod from_card_layout(note: anki.notes.Note, card: anki.cards.Card, notetype: anki.models.NotetypeDict, template: dict, fill_empty: bool) TemplateRenderContext#
- extra_state: dict[str, Any]#
- property question_side: bool#
- fields() dict[str, str]#
- card() anki.cards.Card#
Returns the card being rendered.
Be careful not to call .question() or .answer() on the card, or you’ll create an infinite loop.
- note() anki.notes.Note#
- note_type() anki.models.NotetypeDict#
- latex_svg() bool#
- qfmt() str#
- afmt() str#
- render() TemplateRenderOutput#
- class anki.template.TemplateRenderOutput#
Stores the rendered templates and extracted AV tags.
- question_text: str#
- answer_text: str#
- question_av_tags: list[anki.sound.AVTag]#
- answer_av_tags: list[anki.sound.AVTag]#
- css: str = ''#
- question_and_style() str#
- answer_and_style() str#
- anki.template.templates_for_card(card: anki.cards.Card, browser: bool) tuple[str, str]#
- anki.template.apply_custom_filters(rendered: TemplateReplacementList, ctx: TemplateRenderContext, front_side: str | None) str#
Complete rendering by applying any pending custom filters.