简体   繁体   English

如何在 Phoenix 渲染功能中用 ~H 替换已弃用的 ~E sigil

[英]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:在我学习 Phoenix LiveView 的过程中,我发现自己使用的渲染 function 使用了已弃用的印记:

  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.现在,我知道在 Elixir 中使用eex代码是安全的。 However the compiler says I should replace it with ~H .但是编译器说我应该用~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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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