How to extend Phoenix.HTML.Safe for new datatypes?

I’m using Postgrex to connect directly to the database (because I need to modify the connection info at runtime), and I’m running into an error where I can’t convert the Postgrex.Timestamp struct into a string in the HTML template.

protocol Phoenix.HTML.Safe not implemented for %Postgrex.Timestamp{day: 19, hour: 14, min: 11, month: 12, sec: 4, usec: 0, year: 2015}

How do I extend Phoenix.HTML.Safe to include new Data Types like Postgrex.Timestamp can be shown in the *.html.eex templates?

You must implement the Phoenix.HTML.Safe protocol for the datatype. If you are not familiar with Protocols, you can learn more about them in the Elixir guides. For examples of Phoenix.HTML.Safe implementations, you can check Phoenix’ source.