Your SlideShare is downloading. ×
0
Advanced Views with Erector
Advanced Views with Erector
Advanced Views with Erector
Advanced Views with Erector
Advanced Views with Erector
Advanced Views with Erector
Advanced Views with Erector
Advanced Views with Erector
Advanced Views with Erector
Advanced Views with Erector
Advanced Views with Erector
Advanced Views with Erector
Advanced Views with Erector
Advanced Views with Erector
Advanced Views with Erector
Advanced Views with Erector
Advanced Views with Erector
Advanced Views with Erector
Advanced Views with Erector
Advanced Views with Erector
Advanced Views with Erector
Advanced Views with Erector
Advanced Views with Erector
Advanced Views with Erector
Advanced Views with Erector
Advanced Views with Erector
Advanced Views with Erector
Advanced Views with Erector
Advanced Views with Erector
Advanced Views with Erector
Advanced Views with Erector
Advanced Views with Erector
Advanced Views with Erector
Advanced Views with Erector
Advanced Views with Erector
Advanced Views with Erector
Advanced Views with Erector
Advanced Views with Erector
Advanced Views with Erector
Advanced Views with Erector
Advanced Views with Erector
Advanced Views with Erector
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Advanced Views with Erector

1,742

Published 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

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,742
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
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 <ul><li>Modular decomposition </li></ul><ul><li>Inheritance (nested layouts) </li></ul><ul><li>Consistent semantics </li></ul>
  • 13. Views are classes <ul><li>class Views::Articles::Show < Erector::RailsWidget </li></ul><ul><li>def content div :class => &quot;content&quot; do p &quot;Hello <script> World!&quot; , :class => &quot;sidebar&quot; end end </li></ul><ul><li>end </li></ul>
  • 14. Does the right thing
  • 15. Does the right thing <ul><li>Escapes HTML </li></ul><ul><li>Auto-closes tags </li></ul><ul><li>You control indenting and whitespace </li></ul>
  • 16. Does the right thing <ul><li>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> </li></ul>
  • 17. ERB Refactoring
  • 18. <ul><li><!-- app/views/articles/show.html.erb --> <%=h truncate(article.title, :length => 10 ) %> </li></ul>
  • 19. <ul><li><!-- app/views/articles/show.html.erb --> </li></ul><ul><li><%=h display_name(article) %> </li></ul><ul><li># app/helpers/articles.rb def display_name (article) truncate(article.title, :length => 10 ) end </li></ul>
  • 20. <ul><li><!-- app/views/articles/show.html.erb --> </li></ul><ul><li><%=h article_display_name (article) %> </li></ul><ul><li># app/helpers/articles.rb def article_display_name (article) truncate(article.title, :length => 10 ) end </li></ul>
  • 21. <ul><li><!-- app/views/articles/show.html.erb --> </li></ul><ul><li><%=h article_display_name (article) %> </li></ul><ul><li># app/helpers/articles.rb def article_display_name (article) truncate(article.title, :length => 10 ) end </li></ul>
  • 22. <ul><li><!-- app/views/articles/show.html.erb --> <%= article_display_name(article) %> </li></ul><ul><li># app/helpers/articles.rb def article_display_name (article) content_tag :span , h(truncate(article.title, :length => 10 )), :title => article.title end </li></ul>
  • 23. <ul><li><!-- app/views/articles/show.html.erb --> <%= article_display_name(article) %> </li></ul><ul><li># app/helpers/articles.rb def article_display_name (article) content_tag :span , h(truncate(article.title, :length => 10 )), :title => article.title end </li></ul>
  • 24. <ul><li><!-- app/views/articles/show.html.erb --> <%= article_display_name(article) %> </li></ul><ul><li># app/helpers/articles.rb def article_display_name (article) content_tag :span , h(truncate(article.title, :length => 10 )), :title => article.title end </li></ul>
  • 25. <ul><li><!-- app/views/articles/show.html.erb --> <%= render ( &quot;articles/title&quot; , :title => article) %> </li></ul><ul><li><!-- app/views/_title.html.erb --> <span title= &quot;<%= article.title %>&quot; > <%=h truncate(article.title, :length => 10 ) %> </span> </li></ul>
  • 26. <ul><li><!-- app/views/articles/show.html.erb --> <%= render ( &quot;articles/title&quot; , :title => article) %> </li></ul><ul><li><!-- app/views/_title.html.erb --> <span title= &quot;<%= article.title %>&quot; > <%=h truncate(article.title, :length => 10 ) %> </span> </li></ul>
  • 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. <ul><li># 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 </li></ul>
  • 30. <ul><li># 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 </li></ul>
  • 31. <ul><li># 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 </li></ul>
  • 32. <ul><li># 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 </li></ul>
  • 33. <ul><li># app/views/articles/show.rb class Views::Articles::Show < Views::Articles::Base def content display_name end end </li></ul><ul><li># 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 </li></ul>
  • 34. <ul><li># app/views/articles/show.rb class Views::Articles::Show < Views::Articles::Base def content display_name end end </li></ul><ul><li># 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 </li></ul>
  • 35. <ul><li># app/views/articles/show.rb class Views::Articles::Show < Views::Articles::Base def content display_name end end </li></ul><ul><li># 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 </li></ul>
  • 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

×