Cannot invoke special form alias inside HEEX template

While trying the HEEX template engine for Phoenix I ended up receiving a very strange error that was not having previously with EEX or LEEX, and is that I can not use alias for modules inside them, like this example:

<%
   alias Some.Module.XXXX
   some_var = XXXX.some_func()
%>
#Rest of the HTML

The error is: (ArgumentError) cannot invoke special form alias/1 inside HEEx templates

Why is not possible to use aliases inside HEEx templates? Is this a bug or is intentional?

I know aliases are not a must but saves a lot of writing into the code.

Thanks!

Not sure about the direct rationale for the error, but I believe if you simply do the alias inside of the module or function that the heex template is a part of it should still work.

2 Likes

Just to be that guy: I would encourage you not to set variables in your templates, there’s truly never a need for it. You can instead define helper functions in your View or LiveView and call them from your template.

3 Likes

Thanks for the replies!

Sure! It is not a blockstopper since as you point it can be done inside the view as a helper. I just got confused about why this is now an error while previously was allowed and if it is intentional or not, since many times, this kind of code snippets are done for quick tests, etc.

1 Like

Ah, cool cool! I don’t mean to be annoying, I just point these things in out in case those less experienced were to see it as good practice.

1 Like

I would really like to understand why calling alias from template is a bad idea. Can someone explain that?

Probably for the same lexical-scoping reasons that force the disabling of change tracking when declaring variables.

Notice they mention imports specifically in this comment block for analyse_and_return_tainted_keys, and alias is classified here as one of the other :special_form items that raise.

  ## Assigns tracking

  # Here we compute if an expression should be always computed,
  # never computed, or some times computed based on assigns.
  #
  # If any assign is used, we store it in the assigns and use it to compute
  # if it should be changed or not.
  #
  # However, operations that change the lexical scope, such as imports and
  # defining variables, taint the analysis. Because variables can be set at
  # any moment in Elixir, via macros, without appearing on the left side of
  # `=` or in a clause, whenever we see a variable, we consider it as tainted,
  # regardless of its position.
  #
  # The tainting that happens from lexical scope is called weak-tainting,
  # because it is disabled under certain special forms. There is also
  # strong-tainting, which are always computed. Strong-tainting only happens
  # if the `assigns` variable is used.

Templates are always compiled into some module and even without the complexities of change tracking in heex I prefer managing aliases at the module level instead of per template. The latter just brings a more tech. reason to the table.