简体   繁体   中英

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:

在此处输入图像描述

Quesion

What am I doing wrong?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM