Advanced Views with Erector
Upcoming SlideShare
Loading in...5
×
 

Advanced Views with Erector

on

  • 2,801 views

Erector is a Ruby gem that implements the “builder” pattern for HTML generation. It can save developers time by encouraging more advanced reuse in views via composition and inheritance, terse ...

Erector is a Ruby gem that implements the “builder” pattern for HTML generation. It can save developers time by encouraging more advanced reuse in views via composition and inheritance, terse syntax, auto-closing tags and default HTML-escaping of all output. It can serve as an alternate view technology in Rails.

Using Erector makes it easier to refactor complex views by using standard refactoring techniques such as extracting methods. In ERB you can only accomplish this by helpers or partials, each of which lives in separate files. Since every Erector widget is a class, you can factor out duplication more easily than you can with helpers or partials.

Because views are just Ruby classes, Erector allows for inherited (nested) layouts by default. As a security measure, all output is HTML-escaped by default and all HTML elements are closed automatically.

This talk was given by Jeff Dean at RailsConf 2009. http://en.oreilly.com/rails2009/public/schedule/detail/8587

Statistics

Views

Total Views
2,801
Views on SlideShare
2,793
Embed Views
8

Actions

Likes
0
Downloads
10
Comments
0

1 Embed 8

http://www.slideshare.net 8

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Advanced Views with Erector Advanced Views with Erector Presentation Transcript

  • Advanced Views with Erector A builder view framework Jeff Dean
  • ERB views kinda suck
  • Make you work more
  • No encapsulation
  • Refactoring can be hard
  • We need something that...
  • Requires less work
  • Respects encapsulation
  • Is testable in isolation
  • Erector to the rescue!
  • Views are classes
  • Views are classes
    • Modular decomposition
    • Inheritance (nested layouts)
    • Consistent semantics
  • Views are classes
    • class Views::Articles::Show < Erector::RailsWidget
    • def content div :class => &quot;content&quot; do p &quot;Hello <script> World!&quot; , :class => &quot;sidebar&quot; end end
    • end
  • Does the right thing
  • Does the right thing
    • Escapes HTML
    • Auto-closes tags
    • You control indenting and whitespace
  • Does the right thing
    • class Views::Articles::Show < Erector::RailsWidget def content div :class => &quot;content&quot; do p &quot;Hello <script> World!&quot; , :class => &quot;sidebar&quot; end end end # <div class=&quot;content&quot;> # <p class=&quot;sidebar&quot;>Hello &lt;script&gt; World!</p> # </div>
  • ERB Refactoring
    • <!-- app/views/articles/show.html.erb --> <%=h truncate(article.title, :length => 10 ) %>
    • <!-- app/views/articles/show.html.erb -->
    • <%=h display_name(article) %>
    • # app/helpers/articles.rb def display_name (article) truncate(article.title, :length => 10 ) end
    • <!-- app/views/articles/show.html.erb -->
    • <%=h article_display_name (article) %>
    • # app/helpers/articles.rb def article_display_name (article) truncate(article.title, :length => 10 ) end
    • <!-- app/views/articles/show.html.erb -->
    • <%=h article_display_name (article) %>
    • # app/helpers/articles.rb def article_display_name (article) truncate(article.title, :length => 10 ) end
    • <!-- app/views/articles/show.html.erb --> <%= article_display_name(article) %>
    • # app/helpers/articles.rb def article_display_name (article) content_tag :span , h(truncate(article.title, :length => 10 )), :title => article.title end
    • <!-- app/views/articles/show.html.erb --> <%= article_display_name(article) %>
    • # app/helpers/articles.rb def article_display_name (article) content_tag :span , h(truncate(article.title, :length => 10 )), :title => article.title end
    • <!-- app/views/articles/show.html.erb --> <%= article_display_name(article) %>
    • # app/helpers/articles.rb def article_display_name (article) content_tag :span , h(truncate(article.title, :length => 10 )), :title => article.title end
    • <!-- app/views/articles/show.html.erb --> <%= render ( &quot;articles/title&quot; , :title => article) %>
    • <!-- app/views/_title.html.erb --> <span title= &quot;<%= article.title %>&quot; > <%=h truncate(article.title, :length => 10 ) %> </span>
    • <!-- app/views/articles/show.html.erb --> <%= render ( &quot;articles/title&quot; , :title => article) %>
    • <!-- app/views/_title.html.erb --> <span title= &quot;<%= article.title %>&quot; > <%=h truncate(article.title, :length => 10 ) %> </span>
  • Erector Refactoring
  • # app/views/articles/show.rb class Views::Articles::Show < Erector::RailsWidget def content text do truncate @article .title, :length => 10 end end end
    • # app/views/articles/show.rb class Views::Articles::Show < Erector::RailsWidget def content display_name end private def display_name text do truncate @article .title, :length => 10 end end end
    • # app/views/articles/show.rb class Views::Articles::Show < Erector::RailsWidget def content display_name end private def display_name text do truncate @article .title, :length => 10 end end end
    • # app/views/articles/show.rb class Views::Articles::Show < Erector::RailsWidget def content display_name end private def display_name span :title => @article .title do truncate @article .title, :length => 10 end end end
    • # app/views/articles/show.rb class Views::Articles::Show < Erector::RailsWidget def content display_name end private def display_name span :title => @article .title do truncate @article .title, :length => 10 end end end
    • # app/views/articles/show.rb class Views::Articles::Show < Views::Articles::Base def content display_name end end
    • # app/views/articles/base.rb class Views::Articles::Base < Erector::RailsWidget def display_name span :title => @article .title do truncate @article .title, :length => 10 end end end
    • # app/views/articles/show.rb class Views::Articles::Show < Views::Articles::Base def content display_name end end
    • # app/views/articles/base.rb class Views::Articles::Base < Erector::RailsWidget def display_name span :title => @article .title do truncate @article .title, :length => 10 end end end
    • # app/views/articles/show.rb class Views::Articles::Show < Views::Articles::Base def content display_name end end
    • # app/views/articles/base.rb class Views::Articles::Base < Erector::RailsWidget def display_name span :title => @article .title do truncate @article .title, :length => 10 end end end
  •  
  • What’s next?
  • more rails helpers
  • better performance
  • rails 3
  • erector.rubyforge.org github.com/pivotal/erector [email_address] [email_address] [email_address] [email_address] [email_address]
  • Questions