nickurban

nickurban

I’ve been converting my EEX and LEEX to HEEX and so far it’s been working fine. But I ran into one problem that I couldn’t figure out.

If you have some javascript with string interpolation (e.g. to set a token), it throws errors about it.

Is HEEX intended to support <script> tags?

I was trying to do something like this:

<script>
  fun("<%= @token %>");
</script>

I also tried this:

<script>
  fun({@token});
</script>

The error is:

UndefinedFunctionError{arity: 1, function: :init, message: nil, module: AllegroWeb.Router, reason: nil}, [{AllegroWeb.Router, :init, [[]], []}, {AllegroWeb.Endpoint, :plug_builder_call, 2, [file: 'lib/allegro_web/endpoint.ex', line: 1]}, {AllegroWeb.Endpoint, :"call (overridable 3)", 2, [file: 'lib/plug/debugger.ex', line: 132]},

I know it looks weird that it can’t find Router.init, but I can confirm that is truly the error that happens, and if I change it back to leex it works fine again.

Thanks!

Showing Posts 1 to 8

ruslandoga

ruslandoga

<script>
  <%= render_script(@token) %>
</script>

defp render_script(token) do
  ~E"fun(<%= token %>)"
end

Might be not ideal, but that seems to work for me.

APB9785

APB9785

Creator of ECSx

is fun an Elixir function? If so, that should be inside the <%= %>, not outside.

I have script tags which are calling Elixir functions in several of my heex templates - no issues so far.

nickurban

nickurban OP

Ah, I didn’t make that clear. It’s a JavaScript function.

APB9785

APB9785

Creator of ECSx

Can you do something like this?

<script>
  const myToken = "<%= @token %>";
  fun(myToken);
</script>
benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

I would use hooks for this and not this sort of thing.

BlueHatbRit

BlueHatbRit

This is a minor problem for me as well, I’ve got some integrations in a phoenix (non-liveview) app such as Posthog which requires a block such as:

<script>
    !function(t,e){var o,n,p,r;e.__SV||(window.posthog=e,e._i=[],e.init=function(i,s,a){function g(t,e){var o=e.split(".");2==o.length&&(t=t[o[0]],e=o[1]),t[e]=function(){t.push([e].concat(Array.prototype.slice.call(arguments,0)))}}(p=t.createElement("script")).type="text/javascript",p.async=!0,p.src=s.api_host+"/static/array.js",(r=t.getElementsByTagName("script")[0]).parentNode.insertBefore(p,r);var u=e;for(void 0!==a?u=e[a]=[]:a="posthog",u.people=u.people||[],u.toString=function(t){var e="posthog";return"posthog"!==a&&(e+="."+a),t||(e+=" (stub)"),e},u.people.toString=function(){return u.toString(1)+".people (stub)"},o="capture identify alias people.set people.set_once set_config register register_once unregister opt_out_capturing has_opted_out_capturing opt_in_capturing reset isFeatureEnabled onFeatureFlags".split(" "),n=0;n<o.length;n++)g(u,o[n]);e._i.push([i,s,a])},e.__SV=1)}(document,window.posthog||[]);
    posthog.init('TOKEN_HERE',{api_host:'https://app.posthog.com'})
</script>

When putting this into a .heex file it just gives the error

expected attribute name

I’ve just pulled it out into it’s own .eex file for now but it’s the only one left in my project unless I can figure out a more elegant solution. Any suggestions?

I’m not quite sure if this falls into the “bug” category but it certainly makes some things like this a bit more tricky, especially when various integrations provide this copy and paste block which a lot of people would hope “just works”. I could pull this into it’s own JS file I suppose but it feels like it adds some friction just to people using heex that eex or other systems don’t suffer from.

Apologies for reviving a slightly older thread, I thought it better for people searching this in the future to have a single thread to land on.

sodapopcan

sodapopcan

Where is it you’re putting this? root.html.heex? Somewhere else? And just to be clear, is that error from Elixir or JavaScript?

kartheek

kartheek

Heex performs html validation and there were few bugs in its html validation. You might be experiencing this bug:
https://github.com/phoenixframework/phoenix_live_view/issues/1656

Check the version of phoenix_live_view you are using. Upgrading to the latest {:phoenix_live_view, "~> 0.17.6"} will help - many html validation bugs have been fixed in recent releases.

If problem still exists after upgrading to latest - log a bug on phoenix_live_view repository.

Previous discussion in the forum:

— All posts loaded —

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
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
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
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
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
georgeguimaraes
Just published claude-code-elixir, a plugin marketplace for Claude Code with Elixir support. These are the plugins I’ve been using for my...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews