This follows on from T377455: Add favourite icon to search results and is part of T377186: [STORY] New widget to search and favourite templates.
- Set initial state of favourite icons, from mw.user.options.templatedata-favorite-templates.
- Handle changing state: setting and unsetting a template's favourite status. Use mw.Api.saveOption() and mw.user.options.set(), and update the icon state.
The favourites are stored as an array of page IDs in mw.user.options.templatedata-favorite-templates.
This doesn't handle validation (see T377460: Notify user if they try to favourite a template when they already have the max number of favourites and T377729: Use SaveUserOptions hook to validate template favourites for that).