Hi @daftspunk,
I have a idea for improving the email templates.
Create a new tab in the secondary tabs when editing a template, titled “Tokens,” where the content would include instructions in Markdown format explaining which tokens are available and how to use them.
My approach was:
I added a custom mail template token provider concept for my plugin SmartForms.
OctoberCMS already supports registered mail templates via registerMailTemplates() and Twig variables passed through Mail::send(), but there is no native way to describe which variables are available for a specific mail template in the backend editor.
My solution introduces a token provider class responsible for three things:
- Declaring available tokens
The provider returns grouped token definitions, for example:
{{ formName }}
{{ referenceKey }}
{{ placeholders.name }}
{% for name, detail in fieldDetails %} ... {% endfor %}
- Building runtime variables
The same provider prepares the actual data passed to Mail::send(), so the token list shown in the backend and the real email variables come from the same source.
- Providing documentation
The provider can load Markdown instructions, which are displayed in a custom Tokens tab inside the mail template editor.
The plugin keeps mail template codes defined once, using constants and registerMailTemplates(). The token provider only checks whether a template is supported based on those registered codes.
Where each provider could expose:
getTokenGroups()
getInstructionsMarkdown()
makeVariables(array $context)
I think this feature would be very helpful for creating emails.
What you think?