danirukun

danirukun

Decouple HEEx from LiveView?

While working on a Phoenix-related project, I stumbled upon an interesting issue - it is impossible to use HEEx templates without having LiveView as a dependency. The culprit is that the HTML Engine for it is inside of Phoenix LiveVIew: phoenix_live_view/lib/phoenix_live_view/html_engine.ex at v0.17.11 · phoenixframework/phoenix_live_view · GitHub

This was really annoying to me, since I would expect that HEEx is nothing more than a templating language that should work regardless if the app is using LiveView or not. For very simple apps, I don’t think it’s worth the extra work for having LiveView. So now I need to have LiveView as a dependency, even though I have 0 live views! There was a suggestion on the Elixir Slack that we could move it out of the LiveView package, but I also want to hear everyone’s thoughts here.

Most Liked

josevalim

josevalim

Creator of Elixir

There are no plans to extract it at the moment but I also want to point out that “without pulling LiveView related code that I don’t use” is most likely a micro-optimization. I just checked LiveView source code and at the moment it defines 57 modules. Plus a quick estimate shows that roughly half of these modules (~28) have Engine/HEEx functionality. That is tiny compared to the number of modules you will have in your application. For example, Livebook has 959 modules, and that’s not counting the ones coming from Elixir or Erlang/OTP. Therefore, it is hard to see how “not pulling LiveView-related code” is going to give any meaningful benefit.

One could argue that, if you don’t care about LiveView, having to debug only the HEEx bits is going to be easier, and I would agree with that. But that comes at the cost of putting a layer (and a dependency) between LiveView and HEEx, which will make the lives of everyone using, maintaining, and debugging LiveView - which is perhaps the majority - much harder. So the costs are really hard to justify here.

And it is not like we are averse with breaking things apart. We extracted Phoenix.HTML, Phoenix.PubSub, and Phoenix.View/Template (the latter pretty much based on user demand) in the past. And it actually has led to the downside of fragmenting the documentation - which is a complaint that needs to be addressed… So breaking LV+HEEx might happen in the future but 99% not worth it now.

11
Post #5
Sebb

Sebb

I’m using heex as a template engine to create PDFs. Was using py/jinja2 before and I have to say (as objectively as i can express myself) heex is easily 1000 times better. But its really good hidden in LV. If you want to use it - and can’t use embed_templates - you have to

  require EEx

  source = "sample.html.heex"

  EEx.function_from_file(
    :def,
    :sample,
    source,
    [:assigns],
    # Phoenix.LiveView.HTMLEngine pre 0.18
    engine: Phoenix.LiveView.Engine,
    caller: __ENV__,
    source: source
  )

  def render(assigns) do
    sample(assigns) |> Phoenix.HTML.Safe.to_iodata() |> to_string()
  end

which is not obvious and not documented at all.

You’d really make the world a better place if you’d free heex and treat it as a general purpose template engine.
Maybe when LV hits 1.0?

kevinlang

kevinlang

I agree that having phoenix_live_view as a necessary dependency in a Phoenix application is a pragmatic choice. I’ve had to worked with over-factored applications before in other languages and I agree it is laborious to coordinate changes across packages that can take the joy out of programming.

For my specific case, I want to use Heex without using Phoenix at all, which I should have mentioned in my reply. I have the beginnings of a single-script-file static site generator that I coded up today that uses heex for templating. Because phoenix_live_view also depends on phoenix, my “static site generator” ends up also bringing in Phoenix and its dependencies as well. :laughing:

iex(1)> Mix.install([:phoenix_live_view], force: true)
Resolving Hex dependencies...
Resolution completed in 0.332s
New:
  castore 1.0.1
  mime 2.0.3
  phoenix 1.7.2
  phoenix_html 3.3.1
  phoenix_live_view 0.18.18
  phoenix_pubsub 2.1.1
  phoenix_template 1.0.1
  plug 1.14.2
  plug_crypto 1.2.5
  telemetry 1.2.1
  websock 0.5.1
  websock_adapter 0.5.1

It would be nice if it is broken out one day, but also understand if it isn’t, or if it takes a while. Certainly won’t stop me from using it till then :smile: . It really is a great templating language.

Where Next?

Popular in Discussions Top

Rustixir
Hi everyone, im working on find best language/framework/system for high concurrency, high performance and stable performance after wor...
New
mmport80
I have put far too much effort into Dialyzer over the last year or so - and basically - I doubt it’s worth the effort. It’s not as easy ...
New
mmmrrr
Just saw that dhh announced https://hotwire.dev/ Is it just me or is this essentially live view? :smiley: Although I like the “iFrame-e...
New
AstonJ
If a newbie asked you about Phoenix Contexts, how would you explain the basics to them? Feel free to be as concise or in-depth as you li...
New
pillaiindu
I want to convert a Phoenix LiveView CRUD website to a CRUD mobile app. What do you think is the easiest way to do so?
New
ricklove
I was just introduced to Elixir and Phoenix. I was told about the 2 million websocket test that was done 2 years ago. From my research, t...
New
tmbb
This is a post to discuss the new Phoenix LiveView functionality. From Chris’s talk, it appears that they generate all HTML on the serve...
342 18272 126
New
mbenatti
Following https://github.com/tbrand/which_is_the_fastest |> https://raw.githubusercontent.com/tbrand/which_is_the_fastest/master/imgs...
New
restack_oslo
Hello, Please pardon me for any faux paux. I am 46 and this is my first time on a forum of any kind. I wanted to to get answers from tho...
New
opsb
We’re considering our architecture from a viewpoint of scaling our traffic heavily over the next 6 months. Our current deployment is runn...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31307 143
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

We're in Beta

About us Mission Statement