Can I allow users to design cards using new {{myaddon:function(something)}}
placeholders provided by my add-on?
I would also like them to be able to accept arbitrary arguments
{{myaddon:function("a literal value")}}
, {{myaddon:function(AnotherField)}}
abdo
January 12, 2023, 9:35pm
2
I think something like {{myaddon function(something):}}
instead could be implemented (notice the colon at the end), because Anki will treat anything after the colon as a field name (and shows an error if the field doesn’t exist).
See for an example of implementing custom field filters using hooks:
# Copyright: Ankitects Pty Ltd and contributors
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
"""
An example of how you can add content to a card's question and answer
dynamically in Anki 2.1.20.
One way to accomplish this would be to automatically insert a
"GeneratedField" field into the list of fields when a card is being
rendered. The user could then use {{GeneratedField}} on their card template
to show the extra information. But this is not a great solution, as when
the user switches to a mobile devices or a device without the add-on
installed, they'd get an error message that the field doesn't exist.
Another downside of that approach is that if you're creating hundreds
of fields, you may be doing extra work that does not end up getting used.
A better approach is to leverage Anki's field filter system. If the user
places {{myfilter:a_valid_field}} on their template, the field_filter hook
will be called, allowing your add-on to modify or add to the text contained
in a_valid_field.
This file has been truncated. show original
4 Likes