anki.template
=============

.. py:module:: anki.template

.. autoapi-nested-parse::

   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
----------

.. autoapisummary::

   anki.template.TemplateReplacementList


Classes
-------

.. autoapisummary::

   anki.template.TemplateReplacement
   anki.template.PartiallyRenderedCard
   anki.template.TemplateRenderContext
   anki.template.TemplateRenderOutput


Functions
---------

.. autoapisummary::

   anki.template.av_tag_to_native
   anki.template.av_tags_to_native
   anki.template.templates_for_card
   anki.template.apply_custom_filters


Module Contents
---------------

.. py:class:: TemplateReplacement

   .. py:attribute:: field_name
      :type:  str


   .. py:attribute:: current_text
      :type:  str


   .. py:attribute:: filters
      :type:  list[str]


.. py:data:: TemplateReplacementList

.. py:class:: PartiallyRenderedCard

   .. py:attribute:: qnodes
      :type:  TemplateReplacementList


   .. py:attribute:: anodes
      :type:  TemplateReplacementList


   .. py:attribute:: css
      :type:  str


   .. py:attribute:: latex_svg
      :type:  bool


   .. py:attribute:: is_empty
      :type:  bool


   .. py:method:: from_proto(out: anki.card_rendering_pb2.RenderCardResponse) -> PartiallyRenderedCard
      :classmethod:



   .. py:method:: nodes_from_proto(nodes: collections.abc.Sequence[anki.card_rendering_pb2.RenderedTemplateNode]) -> TemplateReplacementList
      :staticmethod:



.. py:function:: av_tag_to_native(tag: anki.card_rendering_pb2.AVTag) -> anki.sound.AVTag

.. py:function:: av_tags_to_native(tags: collections.abc.Sequence[anki.card_rendering_pb2.AVTag]) -> list[anki.sound.AVTag]

.. py:class:: 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.


   .. py:method:: from_existing_card(card: anki.cards.Card, browser: bool) -> TemplateRenderContext
      :staticmethod:



   .. py:method:: from_card_layout(note: anki.notes.Note, card: anki.cards.Card, notetype: anki.models.NotetypeDict, template: dict, fill_empty: bool) -> TemplateRenderContext
      :classmethod:



   .. py:attribute:: extra_state
      :type:  dict[str, Any]


   .. py:property:: question_side
      :type: bool



   .. py:method:: col() -> anki.collection.Collection


   .. py:method:: fields() -> dict[str, str]


   .. py:method:: 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.



   .. py:method:: note() -> anki.notes.Note


   .. py:method:: note_type() -> anki.models.NotetypeDict


   .. py:method:: latex_svg() -> bool


   .. py:method:: qfmt() -> str


   .. py:method:: afmt() -> str


   .. py:method:: render() -> TemplateRenderOutput


.. py:class:: TemplateRenderOutput

   Stores the rendered templates and extracted AV tags.


   .. py:attribute:: question_text
      :type:  str


   .. py:attribute:: answer_text
      :type:  str


   .. py:attribute:: question_av_tags
      :type:  list[anki.sound.AVTag]


   .. py:attribute:: answer_av_tags
      :type:  list[anki.sound.AVTag]


   .. py:attribute:: css
      :type:  str
      :value: ''



   .. py:method:: question_and_style() -> str


   .. py:method:: answer_and_style() -> str


.. py:function:: templates_for_card(card: anki.cards.Card, browser: bool) -> tuple[str, str]

.. py:function:: apply_custom_filters(rendered: TemplateReplacementList, ctx: TemplateRenderContext, front_side: str | None) -> str

   Complete rendering by applying any pending custom filters.


