テンプレートの静的解析とリファクタリング
by
aereal
Link
Embed
Share
Beginning
This slide
Copy link URL
Copy link URL
Copy iframe embed code
Copy iframe embed code
Copy javascript embed code
Copy javascript embed code
Share
Tweet
Share
Tweet
Slide 1
Slide 1 text
ςϯϓϨʔτͷ! ੩తղੳͱ! ϦϑΝΫλϦϯά! ! id:aereal
Slide 2
Slide 2 text
ࣗݾհ • id:aereal • aereal.org • Hatena • Web
Slide 3
Slide 3 text
ΞδΣϯμ •Web ΞϓϦέʔγϣϯͱςϯϓϨʔτ Τϯδϯ •Text::Xslate ͱ੩తղੳ •՝ͱల
Slide 4
Slide 4 text
ςϯϓϨʔτΤϯδϯ •Template::Toolkit •Text::MicroMason •HTML::Template • Text::Xslate
Slide 5
Slide 5 text
Web ΞϓϦέʔγϣϯ ͱςϯϓϨʔτΤϯδϯ
Slide 6
Slide 6 text
ߘΛදࣔ͢Δ! ૉͳςϯϓϨʔτ
Slide 7
Slide 7 text
post.tt
[% post.title %]
[% post.body %]
Slide 8
Slide 8 text
ίϯτϩʔϥ $c->render('post.tt', { post => $post });
Slide 9
Slide 9 text
ʮߘΛҰཡදࣔ͠Α͏ʯ
Slide 10
Slide 10 text
•ςϯϓϨʔτΛϞδϡʔϧԽ •post.tt → _post.tt •posts/index.tt → _post.tt •posts/show.tt → _post.tt
Slide 11
Slide 11 text
ʮҰཡͰຊจΛ! লུ͍ͨ͠ʯ
Slide 12
Slide 12 text
_post.tt [%- SET formatted = with_ellipsis ? (post.body | truncate) : post.body %]
[% post.title %]
[% formatted %]
Slide 13
Slide 13 text
िؒޙ……
Slide 14
Slide 14 text
index.tt [% INCLUDE "_post.tt" WITH with_ellipsis = 1, show_social_buttons = 1, ... ... show_category = 1, %]
Slide 15
Slide 15 text
INCLUDE ͢Δͱ͖ʹ! ԿΛͤΑ͍ͷ͔! Θ͔Βͳ͍
Slide 16
Slide 16 text
ʮίϝϯτʹॻ͜͏ʯ
Slide 17
Slide 17 text
_post.tt [%# post, show_social_buttons, show_category, ... %] ...
Slide 18
Slide 18 text
ॿ͔ͬͨ
Slide 19
Slide 19 text
࣌ྲྀΕͨɺ͋Δ
Slide 20
Slide 20 text
ίϝϯτ่յ •_post.tt Ͱ blog ͕ඞཁʹͳͬͨ •index.tt Ͱ blog ͕ఆٛࡁΈͩͬͨ •_post.tt ʹίϝϯτ͕ه͞Εͳ͔ͬͨ
Slide 21
Slide 21 text
_post.tt
...
Slide 22
Slide 22 text
index.tt [%# blog %] ... [% FOR post IN posts %] [% INCLUDE "_post.tt" WITH blog = blog, ... %] [% END %]
Slide 23
Slide 23 text
͏μϝͩ
Slide 24
Slide 24 text
ͦΕ੩తղੳͰ
Slide 25
Slide 25 text
ͳͥ੩తղੳ •نଇ͕͋ΔͷͰϓϩάϥϜͷಘҙ •Text::Xslate ʹϦονͳ API ͕͋Δ
Slide 26
Slide 26 text
ొਓ •Text::Xslate::Parser •(Text::Xslate::Syntax::*) •Text::Xslate::Symbol •Text::Xslate::AST::Walker ˑ
Slide 27
Slide 27 text
Text::Xslate::Parser •࣮ࡍʹ͏ͷαϒΫϥε •e.g. Text::Xslate::Syntax::TTerse •Text::Xslate::Parser#parse •AST ͕ಘΒΕΔ •AST = [ Text::Xslate::Symbol ]
Slide 28
Slide 28 text
T::X::Parser my $template = "my name is [% name %]"; my $parser = Text::Xslate::Syntax::TTerse->new; my $ast = $parser->parse($template);
Slide 29
Slide 29 text
Text::Xslate::Symbol •#arity •ࣜͷछྨ(?) •e.g. print, literal
Slide 30
Slide 30 text
Text::Xslate::Symbol •#id •ࣝผࢠ •e.g. name
Slide 31
Slide 31 text
Text::Xslate::Symbol •#first #second •Φϖϥϯυ͕ฦΔ •#is_value ͳͲ
Slide 32
Slide 32 text
มͰ͋ΔτʔΫϯ grep { $_->arity eq 'variable' } @$ast
Slide 33
Slide 33 text
͏ͪΐͬͱஸೡʹ •ߏͳͷͰ࠶ؼతʹ୳͞ͳ͍ͱ͍͚ͳ ͍ •#first #second
Slide 34
Slide 34 text
Text::Xslate::AST::Walker https://github.com/aereal/Text-Xslate-AST-Walker
Slide 35
Slide 35 text
ະఆٛͷม͕ͳ͍ ͜ͱΛظ͢ΔσϞ
Slide 36
Slide 36 text
՝ •INCLUDE ͢Δ࣌ʹ͢ύϥϝʔλͷ໊લະ ఆٛʹݟ͑ͯ͠·͏ •અ͕ೖ (assign) ͩͬͨΒεΩοϓ͢ΕΑ ͦ͞͏ •#traverse Έ͍ͨͳϝιουΛ࣮͍ͯ͠· ͢ •(inspired by Path::Class::Dir#traverse)
Slide 37
Slide 37 text
ύϥϝʔλͷ໊લ [% INCLUDE "..." WITH post = post %] ~~~~
Slide 38
Slide 38 text
՝ •ϦϑΝΫλϦϯάࣗಈԽ͍ͨ͠ •→ நతͰͳ͍ߏจ͕ཉ͍͠
Slide 39
Slide 39 text