Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
テンプレートの静的解析とリファクタリング
Search
aereal
October 17, 2014
Programming
2
5.6k
テンプレートの静的解析とリファクタリング
Talk at テンプレートエンジン Night
aereal
October 17, 2014
Tweet
Share
More Decks by aereal
See All by aereal
How to send distibuted traces to Datadog using build own OpenTelemetry-Lambda distribution
aereal
3
170
好きな技術《コト》で、 生きていく技術 / life with what you like
aereal
5
2k
qron: Cloud Native Cron Alternativeの今
aereal
2
1.8k
自動作曲入門 / introduction to programatic music composition
aereal
1
530k
はてなブログ タグとCDK / The epic of AWS CDK and Hatena Blog Tag
aereal
3
200k
はてなブログ タグの技術選択 / The technical details of Hatena Blog Tag
aereal
3
200k
ブログサービスのHTTPS化を支えたAWSで作るピタゴラスイッチ / The construction of large scale TLS certificates management system with AWS
aereal
3
400k
AWSではてなブログの常時HTTPS配信をバーンとやる話 / The Epic of migration from HTTP to HTTPS on Hatena Blog with AWS
aereal
14
17k
ScalaとPerlでMicroservices in production / Building microservices with Perl and Scala in production
aereal
0
5.3k
Other Decks in Programming
See All in Programming
Rubyとクリエイティブコーディングの輪の広がり / The Growing Circle of Ruby and Creative Coding
chobishiba
1
220
【TID2024】模擬講義:プログラマと一緒にゲームをデザインしてみよう!
akatsukigames_tech
0
380
現代のVueとTypeScript - 型安全の活用術
minako__ph
4
3.1k
ECMAScript、Web標準の型はどう管理されているか / How ECMAScript and Web standards types are maintained
petamoriken
3
370
iOSDC 2024 SMBファイル共有をSwiftで実装する
kishikawakatsumi
1
110
最高の構文木の設計 2024年版
yui_knk
7
2.4k
状態管理ライブラリZustandの導入から運用まで
k1tikurisu
2
270
dotfiles について話したい #湘なんか
stefafafan
2
280
Rubyのobject_id
qnighy
6
1.3k
メモリ最適化を究める!iOSアプリ開発における5つの重要なポイント
yhirakawa333
0
370
労務ドメインを快適に開発する方法 / How to Comfortably Develop in the Labor Domain
yuki21
1
250
エラーレスポンス設計から考える、0→1開発におけるGraphQLへの向き合い方
bicstone
4
600
Featured
See All Featured
Keith and Marios Guide to Fast Websites
keithpitt
408
22k
Web development in the modern age
philhawksworth
204
10k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
41
6.4k
Building Your Own Lightsaber
phodgson
101
5.9k
Optimizing for Happiness
mojombo
375
69k
Designing the Hi-DPI Web
ddemaree
278
34k
Stop Working from a Prison Cell
hatefulcrawdad
266
20k
VelocityConf: Rendering Performance Case Studies
addyosmani
321
23k
Put a Button on it: Removing Barriers to Going Fast.
kastner
58
3.4k
The Power of CSS Pseudo Elements
geoffreycrofte
71
5.2k
Ruby is Unlike a Banana
tanoku
96
10k
The Cult of Friendly URLs
andyhume
76
5.9k
Transcript
ςϯϓϨʔτͷ! ੩తղੳͱ! ϦϑΝΫλϦϯά! ! id:aereal
ࣗݾհ • id:aereal • aereal.org • Hatena • Web
ΞδΣϯμ •Web ΞϓϦέʔγϣϯͱςϯϓϨʔτ Τϯδϯ •Text::Xslate ͱ੩తղੳ •՝ͱల
ςϯϓϨʔτΤϯδϯ •Template::Toolkit •Text::MicroMason •HTML::Template • Text::Xslate
Web ΞϓϦέʔγϣϯ ͱςϯϓϨʔτΤϯδϯ
ߘΛදࣔ͢Δ! ૉͳςϯϓϨʔτ
post.tt <h1>[% post.title %]</h1> <div class="body">[% post.body %]</div>
ίϯτϩʔϥ $c->render('post.tt', { post => $post });
ʮߘΛҰཡදࣔ͠Α͏ʯ
•ςϯϓϨʔτΛϞδϡʔϧԽ •post.tt → _post.tt •posts/index.tt → _post.tt •posts/show.tt → _post.tt
ʮҰཡͰຊจΛ! লུ͍ͨ͠ʯ
_post.tt [%- SET formatted = with_ellipsis ? (post.body | truncate)
: post.body %] <h1>[% post.title %]</h1> <div class="body">[% formatted %]</div>
िؒޙ……
index.tt [% INCLUDE "_post.tt" WITH with_ellipsis = 1, show_social_buttons =
1, ... ... show_category = 1, %]
INCLUDE ͢Δͱ͖ʹ! ԿΛͤΑ͍ͷ͔! Θ͔Βͳ͍
ʮίϝϯτʹॻ͜͏ʯ
_post.tt [%# post, show_social_buttons, show_category, ... %] ...
ॿ͔ͬͨ
࣌ྲྀΕͨɺ͋Δ
ίϝϯτ่յ •_post.tt Ͱ blog ͕ඞཁʹͳͬͨ •index.tt Ͱ blog ͕ఆٛࡁΈͩͬͨ •_post.tt
ʹίϝϯτ͕ه͞Εͳ͔ͬͨ
_post.tt <div class="post" data-blog-id="[% blog.id %]"> ... </div>
index.tt [%# blog %] ... [% FOR post IN posts
%] [% INCLUDE "_post.tt" WITH blog = blog, ... %] [% END %]
͏μϝͩ
ͦΕ੩తղੳͰ
ͳͥ੩తղੳ •نଇ͕͋ΔͷͰϓϩάϥϜͷಘҙ •Text::Xslate ʹϦονͳ API ͕͋Δ
ొਓ •Text::Xslate::Parser •(Text::Xslate::Syntax::*) •Text::Xslate::Symbol •Text::Xslate::AST::Walker ˑ
Text::Xslate::Parser •࣮ࡍʹ͏ͷαϒΫϥε •e.g. Text::Xslate::Syntax::TTerse •Text::Xslate::Parser#parse •AST ͕ಘΒΕΔ •AST = [
Text::Xslate::Symbol ]
T::X::Parser my $template = "my name is [% name %]";
my $parser = Text::Xslate::Syntax::TTerse->new; my $ast = $parser->parse($template);
Text::Xslate::Symbol •#arity •ࣜͷछྨ(?) •e.g. print, literal
Text::Xslate::Symbol •#id •ࣝผࢠ •e.g. name
Text::Xslate::Symbol •#first #second •Φϖϥϯυ͕ฦΔ •#is_value ͳͲ
มͰ͋ΔτʔΫϯ grep { $_->arity eq 'variable' } @$ast
͏ͪΐͬͱஸೡʹ •ߏͳͷͰ࠶ؼతʹ୳͞ͳ͍ͱ͍͚ͳ ͍ •#first #second
Text::Xslate::AST::Walker https://github.com/aereal/Text-Xslate-AST-Walker
ະఆٛͷม͕ͳ͍ ͜ͱΛظ͢ΔσϞ
՝ •INCLUDE ͢Δ࣌ʹ͢ύϥϝʔλͷ໊લະ ఆٛʹݟ͑ͯ͠·͏ •અ͕ೖ (assign) ͩͬͨΒεΩοϓ͢ΕΑ ͦ͞͏ •#traverse Έ͍ͨͳϝιουΛ࣮͍ͯ͠·
͢ •(inspired by Path::Class::Dir#traverse)
ύϥϝʔλͷ໊લ [% INCLUDE "..." WITH post = post %] ~~~~
՝ •ϦϑΝΫλϦϯάࣗಈԽ͍ͨ͠ •→ நతͰͳ͍ߏจ͕ཉ͍͠