Advanced Views with Erector

  • 1,636 views
Uploaded on

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, …

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

More in: Technology , Education
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
1,636
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
11
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Advanced Views with Erector A builder view framework Jeff Dean
  • 2. ERB views kinda suck
  • 3. Make you work more
  • 4. No encapsulation
  • 5. Refactoring can be hard
  • 6. We need something that...
  • 7. Requires less work
  • 8. Respects encapsulation
  • 9. Is testable in isolation
  • 10. Erector to the rescue!
  • 11. Views are classes
  • 12. Views are classes
    • Modular decomposition
    • Inheritance (nested layouts)
    • Consistent semantics
  • 13. 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
  • 14. Does the right thing
  • 15. Does the right thing
    • Escapes HTML
    • Auto-closes tags
    • You control indenting and whitespace
  • 16. 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>
  • 17. ERB Refactoring
  • 18.
    • <!-- app/views/articles/show.html.erb --> <%=h truncate(article.title, :length => 10 ) %>
  • 19.
    • <!-- app/views/articles/show.html.erb -->
    • <%=h display_name(article) %>
    • # app/helpers/articles.rb def display_name (article) truncate(article.title, :length => 10 ) end
  • 20.
    • <!-- 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
  • 21.
    • <!-- 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
  • 22.
    • <!-- 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
  • 23.
    • <!-- 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
  • 24.
    • <!-- 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
  • 25.
    • <!-- 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>
  • 26.
    • <!-- 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>
  • 27. Erector Refactoring
  • 28. # app/views/articles/show.rb class Views::Articles::Show < Erector::RailsWidget def content text do truncate @article .title, :length => 10 end end end
  • 29.
    • # 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
  • 30.
    • # 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
  • 31.
    • # 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
  • 32.
    • # 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
  • 33.
    • # 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
  • 34.
    • # 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
  • 35.
    • # 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
  • 36.  
  • 37. What’s next?
  • 38. more rails helpers
  • 39. better performance
  • 40. rails 3
  • 41. erector.rubyforge.org github.com/pivotal/erector [email_address] [email_address] [email_address] [email_address] [email_address]
  • 42. Questions