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?
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
LostKobrakai
If this in phoenix templates? If so then you likely want to implement
Phoenix.HTML.Safefor theMoneystruct.slouchpie
I’m using Money v6.0.0 — Documentation
It already has the
Html.Safeprotocol implemented.slouchpie
OK cool it finally “clicked” for me. I just put this in my code:
and now my money looks however I want it to look. Thanks @LostKobrakai
kip
Oh, interesting. I’ll look at adding that as an implementation on the next version ofex_money. I’m only just now getting back to web dev and hadn’t realised there is both theHtml.SafeandPhoenix.HTML.Safeprotocols (and maybe there isn’t a difference any more!)D’Oh, should have read my own code first. This is the implementation:
Does it not do what you expect?
You can actually store formatting options in a
Money.tif 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
slouchpie
For illumination, here is exactly the code I ended up writing:
So you can see there are 2 functions.
The 2nd function is the “fallback” function and is identical to the existing
Moneysource 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:I even get this error with
elixirc_options: [ignore_module_conflict: true]]in myprojectinmix.exs.slouchpie
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
Thanks for the explanation and I understand the use case. Let me think on this and revert soon.
kip
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:
:define_phoenix_html_safeas a compile-time configuration option. The default istrue. If set to afalsyvalue then the default protocol implementation will not be generated and users can define their own implementation.Basically, in
config.exsand friends: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
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_implementationswith 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_implementationsprovides 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_moneyview helper until something more elegant is possible.kip
I have pushed commit 72f78b4 to ex_money master branch implementing your suggestion. The changelog entry reads:
Enhancements
:exclude_protocol_implementationsto omit generating implementations for one or more protocols from the listJson.Encoder,Phoneix.HTML.SafeandGringotts.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.