How do quickly comment out this code in a heex file?

I have this code:

<ul>
    <%= if function_exported?(Routes, :live_dashboard_path, 2) do %>
        <li><%= link "LiveDashboard", to: Routes.live_dashboard_path(@conn, :home) %></li>
     <% end %>
</ul>

How do I comment out the part between the <ul> tags?

Is there some quick way how to do it?

1 Like

Kind of. It has been requested but not received with much enthusiasm from the maintainers. There is a stale topic in the mailing list about it.

The suggestion is to create your own empty function component (just "") and use that around the things you want to comment out.

1 Like

Can you expand it a bit further?

your comment Function Component:

def ignore(_assigns) do
  ""
end

From the GItHub issue…

in render:

<.ignore>
  <ul>
    <%= if function_exported?(Routes, :live_dashboard_path, 2) do %>
        <li><%= link "LiveDashboard", to: Routes.live_dashboard_path(@conn, :home) %></li>
     <% end %>
  </ul>
</.ignore>

Or you can use Surface and then you get {!-- ... --} :heart_eyes:

Interesting thanks. I will try it, but where should I put the ignore function so it is accessible in every heex file?

You have helpers in myapp_web.ex so you probably want it imported along with them.

It doesn’t work for me.

Where exactly should I put which code?

They say you can lead a person to a module, but you can’t make them import funtions. You’ll probably want it imported along with the live_view and live_component helpers, and unless you define your own component helper (these are the functions called when you use CheeseWeb, :live_view etc.), you’ll have to import it manually if you’re in a module that just uses Phoenix.Component. I presume your app is for Cheese.

defmodule CheeseWeb.Helpers do
  use Phoenix.Component
  def ignore(_assigns), do:  ""
end

and you’ll have something like this in cheese_web.ex:

  def live_view do
    quote do
      use Phoenix.LiveView,
        layout: {CheeseWeb.LayoutView, "live.html"}

      unquote(view_helpers())
    end
  end

so go down to defp view_helpers do and import your Helpers module.

defp view_helpers do
    quote do
      # Use all HTML functionality (forms, tags, etc)
      use Phoenix.HTML

      # Import LiveView and .heex helpers (live_render, live_patch, <.form>, etc)
      import Phoenix.LiveView.Helpers

      # Import basic rendering functionality (render, render_layout, etc)
      import Phoenix.View

      import CheeseWeb.ErrorHelpers
      import CheeseWeb.Gettext
      import CheeseWeb.Helpers <------------------------- add this
      alias CheeseWeb.Router.Helpers, as: Routes
    end
  end
2 Likes

Thanks, I will try it tomorrow and let you know.