SlideShare une entreprise Scribd logo
1  sur  42
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 => "content" do
      p "Hello <script> World!", :class => "sidebar"
    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 => "content" do
      p "Hello <script> World!", :class => "sidebar"
    end
  end
end

# <div class="content">
#   <p class="sidebar">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("articles/title", :title => article) %>




<!-- app/views/_title.html.erb -->

<span title="<%= article.title %>">
  <%=h truncate(article.title, :length => 10) %>
</span>
<!-- 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>
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

 jdean@pivotallabs.com
Questions

Contenu connexe

Tendances

5 Reasons To Love CodeIgniter
5 Reasons To Love CodeIgniter5 Reasons To Love CodeIgniter
5 Reasons To Love CodeIgniter
nicdev
 

Tendances (19)

Jstl
JstlJstl
Jstl
 
Single Page Web Apps with Backbone.js and Rails
Single Page Web Apps with Backbone.js and RailsSingle Page Web Apps with Backbone.js and Rails
Single Page Web Apps with Backbone.js and Rails
 
Dash of ajax
Dash of ajaxDash of ajax
Dash of ajax
 
5 Reasons To Love CodeIgniter
5 Reasons To Love CodeIgniter5 Reasons To Love CodeIgniter
5 Reasons To Love CodeIgniter
 
Jsp Presentation +Mufix "3"
Jsp Presentation +Mufix "3"Jsp Presentation +Mufix "3"
Jsp Presentation +Mufix "3"
 
jQuery - Doing it right
jQuery - Doing it rightjQuery - Doing it right
jQuery - Doing it right
 
merb.intro
merb.intromerb.intro
merb.intro
 
Consume RESTful APIs with $resource and Restangular
Consume RESTful APIs with $resource and RestangularConsume RESTful APIs with $resource and Restangular
Consume RESTful APIs with $resource and Restangular
 
Cucumber testing
Cucumber testingCucumber testing
Cucumber testing
 
21.search in laravel
21.search in laravel21.search in laravel
21.search in laravel
 
A Brief Introduction to JQuery Mobile
A Brief Introduction to JQuery MobileA Brief Introduction to JQuery Mobile
A Brief Introduction to JQuery Mobile
 
Mojolicious - A new hope
Mojolicious - A new hopeMojolicious - A new hope
Mojolicious - A new hope
 
Story Driven Web Development
Story Driven Web DevelopmentStory Driven Web Development
Story Driven Web Development
 
Intro To Sammy
Intro To SammyIntro To Sammy
Intro To Sammy
 
Build REST APIs like a Jedi with Symfony2
Build REST APIs like a Jedi with Symfony2Build REST APIs like a Jedi with Symfony2
Build REST APIs like a Jedi with Symfony2
 
Mojolicious on Steroids
Mojolicious on SteroidsMojolicious on Steroids
Mojolicious on Steroids
 
Modularizing Rails Apps with Cells
Modularizing Rails Apps with CellsModularizing Rails Apps with Cells
Modularizing Rails Apps with Cells
 
Telling Stories With RSpec
Telling Stories With RSpecTelling Stories With RSpec
Telling Stories With RSpec
 
WordPress Theme Development for Designers
WordPress Theme Development for DesignersWordPress Theme Development for Designers
WordPress Theme Development for Designers
 

En vedette

Motocross race
Motocross raceMotocross race
Motocross race
jumper002
 
Martinlutherking
MartinlutherkingMartinlutherking
Martinlutherking
jumper002
 
Initial Ideas Presentation
Initial Ideas PresentationInitial Ideas Presentation
Initial Ideas Presentation
leahmarie
 
Колчак – правитель России
Колчак – правитель РоссииКолчак – правитель России
Колчак – правитель России
klio25omsk
 
Speeding Up .Shristi
Speeding Up .ShristiSpeeding Up .Shristi
Speeding Up .Shristi
shristisaboo
 
St. Petersburg 2012 report
St. Petersburg 2012 reportSt. Petersburg 2012 report
St. Petersburg 2012 report
FST Biometrics
 

En vedette (18)

Motocross race
Motocross raceMotocross race
Motocross race
 
Martinlutherking
MartinlutherkingMartinlutherking
Martinlutherking
 
Initial Ideas Presentation
Initial Ideas PresentationInitial Ideas Presentation
Initial Ideas Presentation
 
Farmacologia usmle
Farmacologia usmleFarmacologia usmle
Farmacologia usmle
 
Motocross race
Motocross raceMotocross race
Motocross race
 
Колчак – правитель России
Колчак – правитель РоссииКолчак – правитель России
Колчак – правитель России
 
Emerging Futures Lab: One year later
Emerging Futures Lab: One year laterEmerging Futures Lab: One year later
Emerging Futures Lab: One year later
 
Von neumann workers
Von neumann workersVon neumann workers
Von neumann workers
 
Automated card recharge android application
Automated card recharge android applicationAutomated card recharge android application
Automated card recharge android application
 
Automated card recharge android application
Automated card recharge android applicationAutomated card recharge android application
Automated card recharge android application
 
Automated card recharge android application
Automated card recharge android applicationAutomated card recharge android application
Automated card recharge android application
 
Speeding Up .Shristi
Speeding Up .ShristiSpeeding Up .Shristi
Speeding Up .Shristi
 
Career Opportunities in Information Security Industry
Career Opportunities in Information Security IndustryCareer Opportunities in Information Security Industry
Career Opportunities in Information Security Industry
 
Information Security Awareness
Information Security AwarenessInformation Security Awareness
Information Security Awareness
 
Setup Your Personal Malware Lab
Setup Your Personal Malware LabSetup Your Personal Malware Lab
Setup Your Personal Malware Lab
 
St. Petersburg 2012 report
St. Petersburg 2012 reportSt. Petersburg 2012 report
St. Petersburg 2012 report
 
Seminar and Workshop Computer Security, BPPTIK Kominfo
Seminar and Workshop Computer Security, BPPTIK KominfoSeminar and Workshop Computer Security, BPPTIK Kominfo
Seminar and Workshop Computer Security, BPPTIK Kominfo
 
Malware Analysis
Malware AnalysisMalware Analysis
Malware Analysis
 

Similaire à Presentation.Key

Rails 3: Dashing to the Finish
Rails 3: Dashing to the FinishRails 3: Dashing to the Finish
Rails 3: Dashing to the Finish
Yehuda Katz
 
Rails 3 overview
Rails 3 overviewRails 3 overview
Rails 3 overview
Yehuda Katz
 
Resource and view
Resource and viewResource and view
Resource and view
Papp Laszlo
 
Documenting from the Trenches
Documenting from the TrenchesDocumenting from the Trenches
Documenting from the Trenches
Xavier Noria
 
WebcampZG - Rails 4
WebcampZG - Rails 4WebcampZG - Rails 4
WebcampZG - Rails 4
shnikola
 
Desenvolvimento web com Ruby on Rails (parte 2)
Desenvolvimento web com Ruby on Rails (parte 2)Desenvolvimento web com Ruby on Rails (parte 2)
Desenvolvimento web com Ruby on Rails (parte 2)
Joao Lucas Santana
 
OSDC 2009 Rails Turtorial
OSDC 2009 Rails TurtorialOSDC 2009 Rails Turtorial
OSDC 2009 Rails Turtorial
Yi-Ting Cheng
 

Similaire à Presentation.Key (20)

Advanced Views with Erector
Advanced Views with ErectorAdvanced Views with Erector
Advanced Views with Erector
 
Introduction à Ruby
Introduction à RubyIntroduction à Ruby
Introduction à Ruby
 
Ruby on Rails at PROMPT ISEL '11
Ruby on Rails at PROMPT ISEL '11Ruby on Rails at PROMPT ISEL '11
Ruby on Rails at PROMPT ISEL '11
 
The Rails Way
The Rails WayThe Rails Way
The Rails Way
 
Rails 3: Dashing to the Finish
Rails 3: Dashing to the FinishRails 3: Dashing to the Finish
Rails 3: Dashing to the Finish
 
Rails 3 overview
Rails 3 overviewRails 3 overview
Rails 3 overview
 
Resource and view
Resource and viewResource and view
Resource and view
 
Pourquoi ruby et rails déchirent
Pourquoi ruby et rails déchirentPourquoi ruby et rails déchirent
Pourquoi ruby et rails déchirent
 
Documenting from the Trenches
Documenting from the TrenchesDocumenting from the Trenches
Documenting from the Trenches
 
WebcampZG - Rails 4
WebcampZG - Rails 4WebcampZG - Rails 4
WebcampZG - Rails 4
 
Ruby/Rails
Ruby/RailsRuby/Rails
Ruby/Rails
 
RoR 101: Session 2
RoR 101: Session 2RoR 101: Session 2
RoR 101: Session 2
 
Rails 4.0
Rails 4.0Rails 4.0
Rails 4.0
 
Como programar un blog REST
Como programar un blog RESTComo programar un blog REST
Como programar un blog REST
 
Desenvolvimento web com Ruby on Rails (parte 2)
Desenvolvimento web com Ruby on Rails (parte 2)Desenvolvimento web com Ruby on Rails (parte 2)
Desenvolvimento web com Ruby on Rails (parte 2)
 
Rails Antipatterns | Open Session with Chad Pytel
Rails Antipatterns | Open Session with Chad Pytel Rails Antipatterns | Open Session with Chad Pytel
Rails Antipatterns | Open Session with Chad Pytel
 
Ruby on Rails - Introduction
Ruby on Rails - IntroductionRuby on Rails - Introduction
Ruby on Rails - Introduction
 
OSDC 2009 Rails Turtorial
OSDC 2009 Rails TurtorialOSDC 2009 Rails Turtorial
OSDC 2009 Rails Turtorial
 
Rails vs Web2py
Rails vs Web2pyRails vs Web2py
Rails vs Web2py
 
Template rendering in rails
Template rendering in rails Template rendering in rails
Template rendering in rails
 

Dernier

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Dernier (20)

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 

Presentation.Key

Notes de l'éditeur

  1. 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
  2. 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
  3. 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,
  4. Auto-escape html Manage whitespace and indentation for you Auto-close your tags Good IDE support Consistent semantics Little magic
  5. Methods, variables etc... How many of you are object oriented programmers? Functional gangbang
  6. Unit testability proves/enables good design Enables refactoring Enables you to test-drive Dependency injection
  7. &quot;Dependency injection&quot; via constructor params (aka &quot;Complete Construction&quot;) Well-defined semantics for variables, loops, blocks
  8. &quot;Dependency injection&quot; via constructor params (aka &quot;Complete Construction&quot;) Well-defined semantics for variables, loops, blocks
  9. &quot;Dependency injection&quot; via constructor params (aka &quot;Complete Construction&quot;) Well-defined semantics for variables, loops, blocks
  10. pretty printing
  11. pretty printing
  12. pretty printing
  13. Rename has to go into both files Probably have to rename this since you probably have all helpers included by default
  14. You have to decide where to h (this might change in rails 3)
  15. Different semantics