Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Elixir 如何处理;“地位”;Phoenix模板中的变量_Elixir_Phoenix Framework - Fatal编程技术网

Elixir 如何处理;“地位”;Phoenix模板中的变量

Elixir 如何处理;“地位”;Phoenix模板中的变量,elixir,phoenix-framework,Elixir,Phoenix Framework,我正在尝试在我的eex模板中执行以下操作: <% current_production_date = nil %> <%= for widget <- @widgets do %> <%= if (current_production_date != widget.production_date) do %> <!-- ... output a new date header and re-assign current producti

我正在尝试在我的eex模板中执行以下操作:

<% current_production_date = nil %>
<%= for widget <- @widgets do %>
  <%= if (current_production_date != widget.production_date) do %>
    <!-- ... output a new date header and re-assign current production_date -->
    <% current_production_date = widget.production_date %>
  <% end %>
  <%= render "_widget.html", widget: widget %>
<% end %>


您可以使用
Enum.reduce/3
累积结果,然后输出结果

<% 
current_production_date = nil
{result, _}  = 
Enum.reduce(@widgets, {[], current_production_date}, 
fn %{production_date: production_date} = widget, {acc, current_date} ->
    if product_date != current_date do
      output = "<h1>output a new date header and re-assign current production_date</h1>"
      {[output, Phoenix.View.render_to_string(PageView, "widget.html", widget: widget) 
        |acc], production_date}
    else
        {[Phoenix.View.render_to_string(PageView, "widget.html", widget: widget) |acc], current_date} 
    end
end) %>

<%= for w <- Enum.reverse(result) do %>
    <%= raw(w) %>
<% end %>

如果产品日期!=当前日期做什么
output=“输出新的日期标题并重新分配当前生产日期”
{[output,Phoenix.View.render_to_字符串(PageView,“widget.html”,widget:widget)
|acc],生产日期}
其他的
{[Phoenix.View.render_to_string(PageView,“widget.html”,widget:widget)| acc],当前_日期}
结束
完)%>

尽管@Badu的答案在技术上是正确的,但它并不是完全惯用的灵丹妙药,因为它有代码重复,并且使用了错误的抽象来表示数据块

你所拥有的实际上是一个区块列表,所以你可能需要的是

chunk\u fun=fn
小部件,[]->
{:cont[widget]}
#                  ⇓⇓                                ⇓⇓  模式匹配!
%{production_date:pd}=widget,[%{production_date:pd}|]=prev->
{:cont[widget | prev]}
小部件,acc->
{:cont,Enum.reverse(acc),[]}
结束
_fun=fn之后
[]->{:续,[]}
acc->{:cont,枚举反向(acc),[]}
结束
widgets=Enum.chunk\u while(@widgets,[],chunk\u fun,after\u fun)
现在在
widgets
中,您有了按日期分组的
@widgets
。让我们输出它们:


对于[%{production\u date:date}| ]=chunk谢谢你的建议,我最终还是从Elixir论坛上得到了一个建议。。。使用
分组依据

<%= for {date, widgets}
  <- Enum.group_by(@widgets, fn(x) -> DateTime.to_date(x.production_date) end)
  |> Enum.sort(fn({date1, _widget1}, {date2, _widget2}) ->
    case Date.compare(date2, date1) do
      :lt -> true
      _ -> false
    end
  end) do %>
  <%= render "_date_header.html", date: date %>
  <%= for widget <- widgets do  %>
    <%= render "_widget.html", widget: widget %>
  <% end %>
<% end %>
Enum.sort(fn({date1,_widget1},{date2,_widget2})->
案例日期。比较(日期2,日期1)是否
:lt->true
_->错误
结束
结束)do%>

前导
当前生产日期=nil
是多余的,并且绝对没有效果。在
Enum.reduce/3
中使用
{[],nil}
来初始化累加器。另外,如果
使用不同的子句来代替
,例如:
fn%{production\u date:current\u date}=widget,{acc,current\u date}->。。。;fn%{production\u date:production\u date}=widget,{acc,current\u date}->。。。结束