danirukun

danirukun

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.

Showing Posts 1 to 10

Sebb

Sebb

I’m using heex as a templating engine which is very nice, but

thiagomajesk

thiagomajesk

This was my expectation at first as well, see this comment in the original HEEx roadmap issue: HEEx roadmap · Issue #1506 · phoenixframework/phoenix_live_view · GitHub. I’m not sure if they are waiting for a v1 to push the dependency out of LiveView or have abandoned the plans entirely. I also think it’s kind of overkill tbh, I’d rather have it as part of phoenix_html for instance.

kevinlang

kevinlang

I’d also like to see this happen. I like using Heex, but not LiveView. It would be nice to use it without also pulling in a LiveView-related code that I don’t use.

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 #4
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.

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?

stevensonmt

stevensonmt

off-topic but can you post a sample of how you do the PDF creation?

Sebb

Sebb

the ingredients are CSS3 paged media module, tailwind, heex, markdown and prince.xml (paid) as a processor. Be warned that its a little tricky to get tailwind and prince together right now. I will make a post about it.

LostKobrakai

LostKobrakai

That one is in the phoenix_template package, which you could pull in as well. It includes all the template management you know of phoenix.

Sebb

Sebb

I (think) I can’t use embed_templates because I need more control, not because I can’t use it outside Phoenix (which I think is what you mean).

— All posts loaded —

Where Next? Top

Trending in Discussions Top

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
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
axelson
Hi there! :wave: @frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
New
achempion
I’ve been using Emacs as my main code editor for more than a two years. It’s a custom build version although I’ve tried doom emacs and sp...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New
jtormey
Lately I’ve been thinking about how to organize components as a LiveView application grows. One of the pain points I’ve found (for myself...
New
Null-logic-0
What IDE or editor are you using for Elixir development? Personally, I use Zed, and I really like it, but sometimes I wish there were a ...
New

Other Trending Topics Top

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
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
kip
In 2021 I started a new library called Tempo with the objective of modelling time as a set of intervals - not as instants. In 2022 I gave...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews