Tokyoex6 EEx

152 views

Published on

tokyo.ex
EEx

Published in: Engineering
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
152
On SlideShare
0
From Embeds
0
Number of Embeds
30
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Tokyoex6 EEx

  1. 1. Template Engine for Elixir Embedded Elixir 2016/09/22 tokyo.ex #6 おーはら
  2. 2. Agenda 自己紹介 |> 趣旨 |> TemplateEngineとは |> EExとは |> EExの構成 |> eval_(string|file) |> function_from_(string|file) |> compile_(string|file) |> 仕組み |> ASTとは |> compile |> Tokenizer |> Engine |> まとめ
  3. 3. 自己紹介 • おーはら@ohrdev – 基盤技術部@ドリコム • 仕事/技術スタック – 広告サービスのお世話 – 全社基盤システムのお世話 – Elixir/Erlang/Ruby/LFE/Lisp/etc… • コミュニティ – ElixirMeetUp、tokyo.ex、VR.tokyo – tokyo.erl(予定、10月前半頃に告知します)
  4. 4. 趣旨 • ターゲット – EEx is 何? な方 • ゴール – EExの使い方がわかる – EExの仕組みがわかる
  5. 5. TemplateEngineとは • テンプレート(雛形)と入力デー タを合成してドキュメント(文字 列)を出力するソフトウェア – ex) jinja2, jade, erb, haml, slim, etc • ウェブページ(マークアップ)を出 力するエンジンを特に、ウェブ テンプレートエンジンと言う – WAFにバンドルされてる事が多 い(Railsのerbなど)
  6. 6. EExとは • Embedded Elixir (EEx) • 文字列、テンプレートにElixirのコードを評価し て埋め込む処理を提供するモジュール • Elixirにデフォルトでバンドル – https://github.com/elixir- lang/elixir/tree/master/lib/eex
  7. 7. EExの構成 • 主要API – eval_string, eval_file • 入力データと文字列/ファイルを直接評価して文字列を 出力、実行毎にcompile/evalするので遅い – function_from_string, function_from_file • 文字列/ファイルから、入力データを引数にして文字列 を出力する関数を定義するマクロ – compile_string, compile_file • 上2つのAPIの内部で使用される、文字列/ファイルを ElixirのASTにコンパイルする
  8. 8. eval_string EEx.eval_string <source>, <binding>, <option>
  9. 9. eval_file EEx.eval_file <file>, <binding>, <option>
  10. 10. function_from_(string|file) EEx.function_from_string <type>, <name>, <source>, <args>, <option> EEx.function_from_file <type>, <name>, <file>, <args>, <option>
  11. 11. function_from_string(public)
  12. 12. function_from_string(private)
  13. 13. function_from_file
  14. 14. 仕組み:eval_(string|file) <source> <file> EEx.compile_string EEx.compile_file EEx.eval_string <source>, <binding> EEx.eval_file <file>, <binding> EEx.do_eval Result Document <binding> ElixirのAST Code.eval_quoted 実行毎に、compile, evalされる 遅い! <file>が更新されて も、即反映される
  15. 15. 仕組み:function_from_(string|file) <source> <file> EEx.function_from_string EEx.function_from_file EEx.function_from_string <type>, <name>, <source>, <args> EEx.function_from_file <type>, <name>, <file>, <args> Result Document BEAM macro def(p) name(args) do <source> or <file> end compile expand VMname(a1, a2 …) load compileしてから実行 phoenix_live_reloader 等でリロード <file>が更新されると、 再compileが必要
  16. 16. ASTとは • Abstract Syntax Tree • Elixirの式の内部表現、以下のタプルで表す – { :関数名, メタデータ, 引数 } – ex) 1 + 1 #=> {:+, [metadatas], [1, 1]} • quote でASTを確認できる
  17. 17. ASTとは • Code.eval_quoted でASTを評価できる – eval_quoted <ast>, <binding> • 詳しくは、 Metaprogramming Elixir を参照 – https://www.amazon.co.jp/dp/B00U1VU2GA
  18. 18. compile • compile_string, compile_file の実体は、 EEx.Compiler.compile <source>, <option> • EEx.Engine|EEx.SmartEngine を使って、出力 文字列のASTを返却する
  19. 19. compile
  20. 20. compileの流れ EEx.Tokenizer.tokenize <source> <tokens> EEx.Engine.init EEx.Engine.handle_body buffer <AST> EEx.Engine.handle_text EEx.Engine.handle_expr 入力文字列をトークン のリストに変換
  21. 21. Tokenizer • Elixirコード(ロジックやバインディング)を含む 文字列をTokenのリストに変換する
  22. 22. compileの流れ EEx.Tokenizer.tokenize <source> <tokens> EEx.Engine.init EEx.Engine.handle_body buffer <AST> EEx.Engine.handle_text EEx.Engine.handle_expr Engine 初期Buffer 後処理 BufferとTokenから quoteされた式を返却
  23. 23. Engine • Tokenをバッファを使って、quoteされた式 (AST)に変換する • 4つのコールバックを実装する必要がある(ビ ヘイビア) – init/1, handle_text/2, handle_expr/3, handle_body/1 • デフォルトはEEx.SmartEngine • compile時の:engineオプションで使用する Engineを指定
  24. 24. カスタムEngine • カスタムエンジンの使い方 – init, handle_test, handle_expr, handle_bodyの callbackを実装したエンジンモジュールを作成 – コンパイル時の:engineオプションで指定
  25. 25. まとめ • ElixirのテンプレートエンジンのEExを紹介しま した • テンプレートとデータからドキュメント(文字列) を出力する流れを説明しました • コンパイル時のトークナイザー、エンジンの処 理を説明しました • カスタムエンジンの作り方を説明しました

×