Presentation.Key

641 views

Published on

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

  • Be the first to like this

No Downloads
Views
Total views
641
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • You have to manage indenting and whitespace yourself
    You have to close the tags yourself
    You have to decide when to html escape
    Poor IDE support
    Proper factoring makes code slow - render partial
  • Helpers are typically available in all views
    No namespacing
    Instance variables are psuedo-global
    Yield has inconsistent semantics
    Concat binding fuckupedness
    The way variables are bound affects refactoring
  • Code lives in different files - view, layout, partial and the helpers module
    You have to know when to use auto-naming, instance variables, locals
    Inconsistent calling conventions for helpers
    View specs can be hard



    # helpers returning raw text
    [
    :image_tag,
    :javascript_include_tag,
    :stylesheet_link_tag,
    :sortable_element,
    :sortable_element_js,
    :text_field_with_auto_complete,



    # helpers returning raw text whose first parameter is HTML escaped
    [
    :link_to,
    :link_to_remote,
    :mail_to,
    :button_to,
    :submit_tag,



    # return text, take block
    [
    :link_to,
    :link_to_remote,
    :link_to_function,
    :text_field_tag,
    :password_field_tag,
    :check_box_tag



    # render text, take block
    [
    :error_messages_for,
    :form_tag,
    :form_for,
  • Auto-escape html
    Manage whitespace and indentation for you
    Auto-close your tags
    Good IDE support
    Consistent semantics
    Little magic
  • Methods, variables etc...
    How many of you are object oriented programmers? Functional gangbang
  • Unit testability proves/enables good design
    Enables refactoring
    Enables you to test-drive
    Dependency injection
  • "Dependency injection" via constructor params (aka "Complete Construction")



    Well-defined semantics for variables, loops, blocks
  • "Dependency injection" via constructor params (aka "Complete Construction")



    Well-defined semantics for variables, loops, blocks
  • "Dependency injection" via constructor params (aka "Complete Construction")



    Well-defined semantics for variables, loops, blocks
  • pretty printing
  • pretty printing
  • pretty printing
  • Rename has to go into both files
    Probably have to rename this since you probably have all helpers included by default
  • You have to decide where to h (this might change in rails 3)
  • Different semantics
  • Presentation.Key

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

    ×