3

If I pass a numeric value to a SendGrid dynamic template, is there any way of formatting the value?

ie, if I pass the following object to the templating engine:

{
"value": 8.7
}

is there a way to format it, say, to 2 dp?

{{format value $0.00}} would return $8.70

I'm just trying to avoid having to create new properties for every numeric/price on all of my models purely for displaying a monetary value.

Example of what the formatting would be is similar to C#: https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings

Thanks

evorgevol
  • 93
  • 7

1 Answers1

2

Twilio SendGrid developer evangelist here.

I'm afraid there is not a function for formatting currencies in a SendGrid template. You can see the full SendGrid templates Handlebars reference here and while there is a date formatter, that is the only formatter available.

I probably wouldn't create new properties for every numeric/currency, just format the properties that are currencies before you pass them to the template to send the email.

philnash
  • 70,667
  • 10
  • 60
  • 88
  • Thanks for the response. Are there any plans to have this in the future, as I think it would be a fairly common request? I will probably have to apply it to all properties as the business use case can change, and I'd have to redeploy the website to update if I missed one. Having dynamic formatting (maybe not even just for currency) would surely be beneficial for many situations. The Email Templating platform we used to use had this and it was used a lot. Cheers – evorgevol Jun 28 '21 at 23:21
  • 2
    I genuinely don't know if there are plans to extend the templating right now, but I can send this in as feedback! – philnash Jun 28 '21 at 23:23
  • 1
    Typical SendGrid response. Just change all your decimals to strings, that shouldn't screw up your code or waste your time at all. You are the customer, you should do all your work around our needs. Also, how hard is it to have a function to multiply two numbers? Come on. – rooter Nov 15 '22 at 14:39