Save 37% off PRO during our Black Friday Sale! »

テンプレートの静的解析とリファクタリング

3f4be9784f765877f444bc839de29888?s=47 aereal
October 17, 2014

 テンプレートの静的解析とリファクタリング

Talk at テンプレートエンジン Night

3f4be9784f765877f444bc839de29888?s=128

aereal

October 17, 2014
Tweet

Transcript

  1. ςϯϓϨʔτͷ! ੩తղੳͱ! ϦϑΝΫλϦϯά! ! id:aereal

  2. ࣗݾ঺հ • id:aereal • aereal.org • Hatena • Web

  3. ΞδΣϯμ •Web ΞϓϦέʔγϣϯͱςϯϓϨʔτ Τϯδϯ •Text::Xslate ͱ੩తղੳ •՝୊ͱల๬

  4. ςϯϓϨʔτΤϯδϯ •Template::Toolkit •Text::MicroMason •HTML::Template • Text::Xslate

  5. Web ΞϓϦέʔγϣϯ ͱςϯϓϨʔτΤϯδϯ

  6. ౤ߘΛදࣔ͢Δ! ૉ๿ͳςϯϓϨʔτ

  7. post.tt <h1>[% post.title %]</h1> <div class="body">[% post.body %]</div>

  8. ίϯτϩʔϥ $c->render('post.tt', { post => $post });

  9. ʮ౤ߘΛҰཡදࣔ͠Α͏ʯ

  10. •ςϯϓϨʔτΛϞδϡʔϧԽ •post.tt → _post.tt •posts/index.tt → _post.tt •posts/show.tt → _post.tt

  11. ʮҰཡͰ͸ຊจΛ! লུ͍ͨ͠ʯ

  12. _post.tt [%- SET formatted = with_ellipsis ? (post.body | truncate)

    : post.body %] <h1>[% post.title %]</h1> <div class="body">[% formatted %]</div>
  13. ਺िؒޙ……

  14. index.tt [% INCLUDE "_post.tt" WITH with_ellipsis = 1, show_social_buttons =

    1, ... ... show_category = 1, %]
  15. INCLUDE ͢Δͱ͖ʹ! ԿΛ౉ͤ͹Α͍ͷ͔! Θ͔Βͳ͍

  16. ʮίϝϯτʹॻ͜͏ʯ

  17. _post.tt [%# post, show_social_buttons, show_category, ... %] ...

  18. ΍΍ॿ͔ͬͨ

  19. ࣌͸ྲྀΕͨɺ͋Δ೔

  20. ίϝϯτ่յ •_post.tt Ͱ blog ͕ඞཁʹͳͬͨ •index.tt Ͱ͸ blog ͕ఆٛࡁΈͩͬͨ •_post.tt

    ʹίϝϯτ͕௥ه͞Εͳ͔ͬͨ
  21. _post.tt <div class="post" data-blog-id="[% blog.id %]"> ... </div>

  22. index.tt [%# blog %] ... [% FOR post IN posts

    %] [% INCLUDE "_post.tt" WITH blog = blog, ... %] [% END %]
  23. ΋͏μϝͩ

  24. ͦΕ੩తղੳͰ

  25. ͳͥ੩తղੳ •نଇ͕͋ΔͷͰϓϩάϥϜͷಘҙ෼໺ •Text::Xslate ʹ͸Ϧονͳ API ͕͋Δ

  26. ొ৔ਓ෺ •Text::Xslate::Parser •(Text::Xslate::Syntax::*) •Text::Xslate::Symbol •Text::Xslate::AST::Walker ˑ

  27. Text::Xslate::Parser •࣮ࡍʹ࢖͏ͷ͸αϒΫϥε •e.g. Text::Xslate::Syntax::TTerse •Text::Xslate::Parser#parse •AST ͕ಘΒΕΔ •AST = [

    Text::Xslate::Symbol ]
  28. T::X::Parser my $template = "my name is [% name %]";

    my $parser = Text::Xslate::Syntax::TTerse->new; my $ast = $parser->parse($template);
  29. Text::Xslate::Symbol •#arity •ࣜͷछྨ(?) •e.g. print, literal

  30. Text::Xslate::Symbol •#id •ࣝผࢠ •e.g. name

  31. Text::Xslate::Symbol •#first #second •Φϖϥϯυ͕ฦΔ •#is_value ͳͲ

  32. ม਺Ͱ͋ΔτʔΫϯ grep { $_->arity eq 'variable' } @$ast

  33. ΋͏ͪΐͬͱஸೡʹ •໦ߏ଄ͳͷͰ࠶ؼతʹ୳͞ͳ͍ͱ͍͚ͳ ͍ •#first #second

  34. Text::Xslate::AST::Walker https://github.com/aereal/Text-Xslate-AST-Walker

  35. ະఆٛͷม਺͕ͳ͍ ͜ͱΛظ଴͢ΔσϞ

  36. ՝୊ •INCLUDE ͢Δ࣌ʹ౉͢ύϥϝʔλͷ໊લ΋ະ ఆٛʹݟ͑ͯ͠·͏ •અ͕୅ೖ (assign) ͩͬͨΒεΩοϓ͢Ε͹Α ͦ͞͏ •#traverse Έ͍ͨͳϝιουΛ࣮૷͍ͯ͠·

    ͢ •(inspired by Path::Class::Dir#traverse)
  37. ύϥϝʔλͷ໊લ [% INCLUDE "..." WITH post = post %] ~~~~

  38. ՝୊ •ϦϑΝΫλϦϯά΋ࣗಈԽ͍ͨ͠ •→ ந৅తͰ͸ͳ͍ߏจ໦͕ཉ͍͠

  39. ׬