StormByte

StormByte

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!

Most Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

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.

sodapopcan

sodapopcan

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.

StormByte

StormByte

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.

sodapopcan

sodapopcan

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.

Where Next?

Popular in Questions Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
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 Postg...
New

Other popular topics Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement