slouchpie

slouchpie

Hi

I have been going through the Cldr docs and I don’t see a clear way to do this.

How can I configure Cldr so I can overwrite the to_string function for certain things?

In particular I want to write the formatter so money:

<%= @money %>

is displayed as "$20" if the amount is {:USD, 20} and displayed as "$20.10" if the amount is {:USD, "20.10"}.
I know I can use Money.to_string with formatting options but I want to keep it simple for other devs and have custom Cldr formatter.

Anyone have any idea or pointers to help me out?

Showing Posts 1 to 10

LostKobrakai

LostKobrakai

If this in phoenix templates? If so then you likely want to implement Phoenix.HTML.Safe for the Money struct.

slouchpie

slouchpie OP

I’m using Money v6.0.0 — Documentation
It already has the Html.Safe protocol implemented.

slouchpie

slouchpie OP

OK cool it finally “clicked” for me. I just put this in my code:

  defimpl Phoenix.HTML.Safe, for: Money do
    def to_iodata(money) do
      "this is how I do it!"
      |> Phoenix.HTML.Safe.to_iodata()
    end
  end

and now my money looks however I want it to look. Thanks @LostKobrakai

kip

kip

ex_cldr Core Team

Oh, interesting. I’ll look at adding that as an implementation on the next version of ex_money. I’m only just now getting back to web dev and hadn’t realised there is both the Html.Safe and Phoenix.HTML.Safe protocols (and maybe there isn’t a difference any more!)

D’Oh, should have read my own code first. This is the implementation:

  defimpl Phoenix.HTML.Safe, for: Money do
    def to_iodata(money) do
      Phoenix.HTML.Safe.to_iodata(Money.to_string!(money))
    end
  end

Does it not do what you expect?

You can actually store formatting options in a Money.t if you need - that capability was primarily added to support this protocol. Of course if thats not enough you can certainly do your own implementation as you showed.

Example

iex> m = Money.new(:USD, 1234, format: :long)
#Money<:USD, 1234>
iex> Money.to_string m                       
{:ok, "1,234 US dollars"}
slouchpie

slouchpie OP

For illumination, here is exactly the code I ended up writing:

  defimpl Phoenix.HTML.Safe, for: Money do
    def to_iodata(%Money{amount: %Decimal{coef: coef, exp: 0}} = money) when is_integer(coef) do
      money
      |> Money.to_string!(fractional_digits: 0)
      |> HTML.Safe.to_iodata()
    end

    def to_iodata(money) do
      money
      |> Money.to_string!()
      |> HTML.Safe.to_iodata()
    end
  end

So you can see there are 2 functions.

The 2nd function is the “fallback” function and is identical to the existing Money source code.

The 1st function pattern-matches for “integer” money amounts.

The result of this is displaying money as “$20.34” when it is not an integer amount and as “$20” when it is an integer amount (there is no ".00" at the end).

UPDATE: this code actually breaks my deployments. I get an error from mix release:

** (Mix) Duplicated modules:
     'Elixir.Phoenix.HTML.Safe.Money' specified in my_module and ex_money

I even get this error with elixirc_options: [ignore_module_conflict: true]] in my project in mix.exs.

slouchpie

slouchpie OP

I don’t understand this. Can you provide an example?

UPDATE: ignore this. I understand you mean Money.new(..., options). Sadly, this doesn’t fit my needs.

I need to fully override the protocol. Would it be possible to make this overrideable?

kip

kip

ex_cldr Core Team

Thanks for the explanation and I understand the use case. Let me think on this and revert soon.

kip

kip

ex_cldr Core Team

As of commit 8c14f4f there is a compile-time option to configure whether or not the default protocol implementation is defined. The changelog entry is:

  • Add :define_phoenix_html_safe as a compile-time configuration option. The default is true. If set to a falsy value then the default protocol implementation will not be generated and users can define their own implementation.

Basically, in config.exs and friends:

config :ex_money,
  define_phoenix_html_safe: false

The commit is on a development branch that will be published Lunar New Year’s Day (next Tuesday) and therefore can’t really be used easily as a GitHub dependency.

If the need becomes urgent I can do a backport to the current main branch and publish a release, but I’d prefer to wait until Tuesday if thats possible. Let me know?

slouchpie

slouchpie OP

It’s really cool that you decided to make this possible.

I wonder if maybe there is a more extensible approach?

What comes to mind is a compile-time configuration option :exclude_protocol_implementations with default []. If set to, e.g. [Gringotts.Money, Inspect, Jason.Encoder] then these 3 protocol implementations would not be defined.

For my case, I would then only need exclude_protocol_implementations: [Phoenix.HTML.Safe].

I think this approach will be easier for future customizations. Also the name exclude_protocol_implementations provides immediate insight into what is happening, even which file in the source code to check.

I left some comments on the commit, to illustrate what I’m thinking.

Thanks again!

P.S. This is not urgent at all. We are using a show_money view helper until something more elegant is possible. :+1:

kip

kip

ex_cldr Core Team

I have pushed commit 72f78b4 to ex_money master branch implementing your suggestion. The changelog entry reads:

Enhancements

  • Adds configuration option :exclude_protocol_implementations to omit generating implementations for one or more protocols from the list Json.Encoder, Phoneix.HTML.Safe and Gringotts.Money. Thanks to @jgough-playoxygen for the suggestion.

If you have a chance to take it for a spin from GitHub that would be great. Pending your feedback I’ll then publish to hex.

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
thiagogsr
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews