1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Phoenixを触ってみる その9

Posted at

今回はtemplate。
だいぶ前回と被るので、短いです。

Template

今までも出てきたeexのこと。
単純な変数埋め込みや関数呼び出しは前回出ましたね。

繰り返し

templates/page/index.eex
  <%= for key <- connection_keys(@conn) do %>
    <p><%= key %></p>
  <% end %>

こんな感じで繰り返しが可能。
注意したいのは、key参照時に"@"がついていないこと。
どうやら以下っぽい。

  • reder時に渡された変数への参照 -> @をつける
  • template内で宣言された変数への参照 -> @不要

rendering chain

templates/page/index.eex
  <%= for key <- connection_keys(@conn) do %>
    <p><%= render(HelloWeb.SharedView, "key.html", key: key) %></p>
  <% end %>

template内から他のtemplateをrender出来る。
前回の説明通り、単なるマークアップの埋め込みではなく、Viewの1関数としてのtemplateを埋め込める。
関数呼び出しなので、呼び出し元と呼び出された側でちゃんとスコープが分かれているのも良いポイント。

呼び出すtemplateは同一Viewに限らず、別Viewも可能。(同一Viewなら第1引数は省略可能
なので共通パーツとしてのView+eexを用意することも出来る(Layoutがこれに該当するか)

これ、だいぶ便利そうなのだが実際どのレベルでtemplateって分けてるんだろう。
jsの世界ではatomic designとかあったりするけど、同じ考え方をしたほうがよいのだろうか・・・?

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?