Tokens - formatting syntax
One of the issues with our tokens is around formatting - or the lack of options around it - consider the following requirements (these are a little artificial but are representative of various requests)
- I want to insert a birth date in the standard formatting (I think that is 'long date'
- I want to insert a birth date using short format
- I want to insert Month of birth into an email, in the language in use
- I want to add text if someone was born before a certain date
- I want to use conditional that checks whether the month of birth is higher than today's month
We can visualise these as
Hey
Your birthday was on {contact.birth_date}, or for short {contact.birth_date|short}.
You were born in the month of {contact.birth_date|F}
{if {now|m} > {contact.birth_date|m}sorry I missed your birthday {/if}
{if {contact.birth_date|raw} < '1960-01-01'}You are pretty old ya know{/if}
Note that similar concerns apply to 'amount' fields where the need for 'raw' amounts is quite common.
Options as I see them are
- never do anything to support format flexibility
- define a pseudotoken formatting syntax for them - somewhat like above
- define a pseudofield formatting syntax for them - this is basically 2 but with dots instead of pipes - we could also use double underscores
- add a process (I thought we might be able to do this in token smarty but I'm not quite sure) that assigns a smarty token for every resolved token. ie if {contact.birth_date} is resolved then ALSO assign {$contact__birth_date}. Always assign in raw format. This would give template users the option of using smarty variables as interchangeable, more flexible versions. Over time we could alter what is advertised & promote smarty as preferred, if we choose.
Some notes
-
I haven't dug into how hard 4 would be - my gut says it is doable but is likely to wind up in the too hard basket if we extend that conversation to other templating engines (eg. twig) - which might be a good reason to look at 2 or 3 instead
-
It turns out that where smarty is enabled 'now' can already be printed out - ie
{$smarty.now|date_format:'%d %b %Y %H:%M:%S'}
. this is interesting because there are at least 3 extensions that make it available as a custom token.... -
Formatting thoughts - smarty uses strftime formatting eg. '%d %b %Y %H:%M:%S'. I feel like most of us would be more comfortable with strtotime formatting however, my efforts to understand the difference suggest that it is strftime not strtotime that handles formatting by locale so I feel pretty sure we should use strftime formats + our own standins - ie
dateformatDatetime
,dateformatFull
dateformatPartial
dateformatYear
(we have a few more that we are currently storing strtotime style - iedateformatFinancialBatch
dateformatshortdate
anddateInputFormat
(brain explodes)