tadasajon

tadasajon

How can I easily comment out lines in a *.html.heex file?

Suppose I have a *.heex template file that I am iterating rapidly on and I just want to comment out a chunk of it for a few moments. What is the syntax for this? Most of the approaches I have tried just throw errors.

I have resorted to just creating a new empty file, then deleting the lines I want to comment out entirely, saving them in the new file, then copy-pasting them back in later when I want to uncomment them. This is super-laborious.

Marked As Solved

tadasajon

tadasajon

Here is the solution I came up with:

  1. create a new file in the lib/myapp_web directory and define a new module in it:
defmodule HeexIgnore do
    use Phoenix.Component

    def ignore(assigns), do: ~H""
end
  1. In lib/myapp_web.ex find the private function defp view_helpers do... which contains several import statements. Add this line as one of the imports in that function:
import HeexIgnore
  1. Now all your templates should be able to add the <.ignore> ... </.ignore> incantation in order to completely ignore large chunks of lines.

Thanks to @chrismccord for giving me some tips in the GitHub issue referenced by @kokolegorille

Also Liked

werkzeugh

werkzeugh

for the record:
this is solved with <%!-- --%> since elixir v1.14.0 09/2022:

<%!--  comment
  over
  multiple lines --%>

https://github.com/elixir-lang/elixir/pull/11505

getting it to work in vscode:

vscode-elixir-ls supports it since 0.12.0:
https://github.com/elixir-lsp/vscode-elixir-ls/commit/9afc4d21eb7ed870f296e3d2fd34ef95cd4d16ea

i just had to add this to my settings.json to use it in vscode with the default comment-shortcut:

  "custom-language-properties": {
    "phoenix-heex.comments.blockComment": ["<%!--", "--%>"]
  },

(phoenix-heex. might be named html-eex. or similar in your setup)

APB9785

APB9785

Creator of ECSx

The parser identifies <!-- as a comment in handle_text, and passes it off to handle_comment, which needs to find a closing --> or it will raise. The problem is that it seems to “give up” on the search as soon as it hits a <%

APB9785

APB9785

Creator of ECSx

The opening <% is fine, it’s the closing %> that will end the comment prematurely.

Where Next?

Popular in Questions Top

nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
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
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
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
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Other popular topics Top

Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29703 241
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement