Search code examples
elixirphoenix-frameworksigils

How to replace deprecated ~E sigil with ~H in Phoenix render funciton


Background

In my quest to learn Phoenix LiveView I found myself using a render function that uses a deprecated sigil:

  def render(_assigns) do
    ~E"""
    <menubar>
      <menu label="<%= gettext "File" %>">
          <hr/>
          <item onclick="quit"><%= gettext "Quit" %></item>
      </menu>
      <menu label="<%= gettext "Extra" %>">
          <item onclick="browser"><%= gettext "Open Browser" %></item>
      </menu>
    </menubar>
    """
  end

Now, I understand this is a safe for to use eex code inside of Elixir. However the compiler says I should replace it with ~H. So my firs try is the following:

  def render(assigns) do
    ~H"""
    <menubar>
      <menu label="{@gettext('File')}">
          <hr/>
          <item onclick="quit"><%= gettext "Quit" %></item>
      </menu>
      <menu label="{@gettext 'Extra'}">
          <item onclick="browser"><%= gettext "Open Browser" %></item>
      </menu>
    </menubar>
    """
  end

Which does not work and does not show the text properly in the menu:

enter image description here

Quesion

What am I doing wrong?


Solution

  • Answer

    The problem in my attempt was the @ character. I likely miss understood the error message and concluded the @ had to be part of the variable.

    The correct version looks like this:

      def render(assigns) do
        ~H"""
        <menubar>
          <menu label={gettext("File")}>
              <hr/>
              <item onclick="quit"><%= gettext "Quit" %></item>
          </menu>
          <menu label={gettext("Extra")}>
              <item onclick="browser"><%= gettext "Open Browser" %></item>
          </menu>
        </menubar>
        """
      end