在视图中连接字符串

时间:2016-04-30 07:28:46

标签: elixir phoenix-framework

我如何连接last_namehonorific_prefix并添加full_name,如果它defmodule MyApp.UserView do use MyApp.Web, :view alias MyApp.User def full_name(%User{first_name: first_name, last_name: last_name, honorific_prefix: honorific_prefix}) do first_name |> String.upcase end end 中没有空?

<div>
    Search by name:
    <input type="text" name="txtSearch" />
    @Html.ActionLink("Search", "SearchName") 
</div>

2 个答案:

答案 0 :(得分:2)

您可以将这些元素视为集合,拒绝空元素并加入它们:

[honorific_prefix, first_name, last_name]
|> Enum.reject(&(&1 == ""))
|> Enum.join(" ")

答案 1 :(得分:1)

我只使用if<>

def full_name(%User{first_name: first_name, 
                    last_name: last_name, 
                    honorific_prefix: honorific_prefix}) do
  if honorific_prefix == "" do
    ""
  else
    honorific_prefix <> " "
  end <> first_name <> " " <> last_name
end