tomekowal
I’ve recently found myself writing many Phoenix templates and wanted to streamline my experience.
I often want to set some defaults, e.g. in Bootstrap a table usually has a class “table” <table class="table"> But then I’d like to take the default and specify that this time the table should be striped.
I can’t use Phoenix.HTML.Tag.content_tag in this case. Everything in Phoenix.HTML produces final io lists that can’t be modified later. I figured I need some middleware.
defmodule ComposableHtml.Tag do
defstruct [:tag_name, :content, :attrs]
end
This way I can define a tag and extend it:
table = %Tag{tag_name: :table, content: ..., attrs: [class: "table"]}
...
table_striped = tag |> update_attr(:class, &"#{&1} table-striped")
I saw the same problem in the Elm community https://www.youtube.com/watch?v=PDyWP-0H4Zo
The talk concludes that a data structure that allows overwriting its fields is a nice API.
I’d like to turn my helpers into a lib generic enough to be a building block for other libraries like API Reference — ExEffectiveBootstrap v0.1.17
The idea is also to be able to use the ComposableHtml.Tag directly in the templates by implementing Phoenix.HTML.Safe protocol:
defmodule ComposableHtml.Tag do
...
def to_phoenix_html(%Tag{} = tag) do
if tag.content do
PhoenixTag.content_tag(tag.tag_name, to_phoenix_html(tag.content), tag.attrs)
else
PhoenixTag.tag(tag.tag_name, tag.attrs)
end
end
def to_phoenix_html(list_of_tags) when is_list(list_of_tags) do
Enum.map(list_of_tags, &to_phoenix_html/1)
end
# We pass the data in the `content_tag`, so we don't have to escape here
def to_phoenix_html(data), do: data
end
defimpl Phoenix.HTML.Safe, for: ComposableHtml.Tag do
alias ComposableHtml.Tag
def to_iodata(data) do
{:safe, data} = Tag.to_phoenix_html(data)
data
end
end
I’ve found existing HTML builder that uses macros GitHub - dczombera/html_builder: A small Elixir HTML library that uses its own DSL for easily creating HTML tags · GitHub
But it is not composable.
My questions are:
- Would you find such a library useful?
- If it is useful, are there any existing libraries for composing HTML that I missed?
- If I am going to create a new library, could you help me with API design?
Ad.3 The with_* approach from Brian’s talk seems compelling but it is more specific than I like. E.g. in the talk there is |> withRole Danger |> withFormat Outline and in Bootstrap they would both translate to a class <button class="btn btn-outline btn-danger">
I am not sure what would be some proper names for functions that append to an attribute (e.g. changing class="table" to class="table table-striped"), and what names to use for functions overwriting the attributes.
Trending in Questions
Other Trending Topics
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
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










First 6 of 6 Posts
sfusato
There’s this new library that has just been announced which seems to be doing what you want (and more than that): x_component. If there’s something that I miss from Vue/React, it’s this kind of component composability. Looks very powerful (also check the benchmarks).
Then there’s also Surface which is aimed directly at Liveview (discussion).
tomekowal
Thank you! Those are very useful links!
Both libraries emphasize composability which makes me think I am on a good track
However, the scope of what I am thinking about is much much smaller. I don’t want to define new templating language.
Let me give a before and after example.
Let’s say I want to define a table helper for bootstrap:
In my
view_helpers.exI need this:In the template, I can use
<%= bootstrap_table(@content) %>But then in some other place, I need “table-striped” so I modify the herlper
But then in yet another place I need to set border, so I modify the helper:
And before I realize, I add
cellpadding,cellspacingand finally all attributes supported bytabletag.Let’s say that instead, I use
ComposableHtml.TagThe usage in the template would look the same
<%= bootstrap_table(content) %>provided that%Tag{}struct implementsPhoenix.HTML.Safe.But now, if I need to add a class, I can do it differently:
Now in the template, I can do
<%= bootstrap_table(content) |> striped() |> bordered() %>Another nice example is what @wojtekmach wants here: Proposal: conditionally setting css classes · Issue #276 · phoenixframework/phoenix_html · GitHub
Let’s say you want to conditionally add
activeclass based on current path:If
link_toreturnedComposableHtml.Tag, you could write:and define a helper:
I’d like the library to make HTML composable in a similar way that
Ecto.QuerymakesSQLcomposableBrainiac
It seems like a natural extension of
Phoenix.HTML’s template functions. It would make more sense for the composability to be an in-built feature of the framework instead of a separate library.tomekowal
I think I would start with a separate library to test how it feels and what would be a nice API. If it catches on and Crhis likes the idea, it could become part of
Phoenix.HTMLabitdodgy
I started working on
ex_componentunaware of this thread. My idea was to be able to create standardised, reusable components and keep them consistent in different related projects. Here is an example that uses the library to generate all possible Bootstrap grid HTML.The lets you do:
You can checkout out ExBs which was written on top of it as a proof of concept. The idea is to produce a lib specific to our company projects and not let the HTML layer turn into the wild west.
Curios to know your thoughts!
Just an edit, here’s what I came up with for the tables:
Which gets you:
dorgan
For my use cases I have this simple helper:
I also use it for stimulus controllers:
I reuse common elements with helpers, too:
For this last case the ComposableHtml thing may be less verbose and allow for more complex composition, but so far this has served me well