SlideShare une entreprise Scribd logo
1  sur  20
Télécharger pour lire hors ligne
RuHL
                          How can I see with all that code in my view?




Wednesday, December 9, 2009
The first thought...
               Haml is so far away from HTML, why not take it further?


               Repeat content instead of having to write loops.


                %ul
                 - @school.special_features.each do |feature|
                   %li= feature.name



                # Maybe something like...
                %ul { repeat: special_features }
                   %li= feature.name




Wednesday, December 9, 2009
The next minute...
               I could actually pull this off with HTML.

                <ul ruby=”_collection: special_features”>
                 <li ruby=”name”/>
                </ul>


                # Which later changed to
                <ul>
                 <li data-ruhl =”_collection: special_features, name”/>
                </ul>

               ...more on this coming up




Wednesday, December 9, 2009
Thoughts from JSP days and beyond
               • Working with designers inefficient after converting views.
                 • Why convert the code from HTML to a language that generates HTML if
                     it is not necessary?


               • Views didn’t garner the same architecture attention ‘code’ did.
                 • Did formulating the Fat Model/Skinny Controller idea take everyone’s
                     energy?


               • Over time, code in views grew as quick fixes were applied.
                 • Little restrictions on what is allowed encourages misuse.

               • Testing views more difficult than models or controllers.
                 • Shouldn’t the multiple attempts to improve view testing be an indicator?



Wednesday, December 9, 2009
The 2 minute RuHL history.
               • The attribute was initially called ruby.
                 • Peter Cooper suggested data-ruby for HTML5 compliance.
                 • Coworker suggested data-ruhl to prevent potential naming collisions.

               • Crystallized the idea of ‘No code in views’
                 • This became the rule that could not be broken.

               • Should not have to code extra HTML tags to make something work.
                 • Exceptions to this arose and _swap was introduced. (details later)

               • Functionality to be driven by real use cases, not imagined scenarios.




Wednesday, December 9, 2009
Standalone RuHL
                   RuHL can be used outside Sinatra and Rails.

                   Ruhl::Engine.new(File.read(ʻhello_world.ruhlʼ)).render(self)


                   Breaking it down:
                      data = File.read(ʻhello_world.ruhlʼ)      # read the html file
                      engine = Ruhl::Engine.new(data)           # instantiate the engine
                      html = engine.render(self)                # render the html
                                                                # using self for context

                   In the Rails helper, self is the @template object available to the
                   controller. This gives you access to all the helpers available within the
                   view.



Wednesday, December 9, 2009
RuHL: _if
                  How would I conditionally display data?


                  #Don’t show the div or it’s contents if there aren’t users.
                    <div data-ruhl="_if: users?">
                       <table>
                         <thead>
                            <tr>
                              <td>First Name</td>
                              <td>Last Name</td>
                              <td>Email</td>
                            </tr>
                         </thead>
                         <tr data-ruhl="_collection: user_list">
                            <td data-ruhl="first_name">Andrew</td>
                            <td data-ruhl="last_name">Stone</td>
                            <td data-ruhl="email">andy@stonean.com</td>
                         </tr>
                       </table>
                     </div>




Wednesday, December 9, 2009
RuHL: _unless
                  How would I conditionally display data?


                  #Show the message if there aren’t users.
                    <p data-ruhl="_unless: users?">No Users were found.</p>


                  If users, the <p> tag would not be included on output.




Wednesday, December 9, 2009
RuHL: _use
               I want to use the user object as the local object within the scope of the div to
               simplify the calls. The methods will first be tried against the local object, then
               the global context.

                    <div data-ruhl="_use: current_user">
                       <h1> Editing <span data-ruhl="_swap: full_name"/></h1>
                       <table>
                         <thead>
                           <tr>
                              <td>First Name</td>
                              <td>Last Name</td>
                              <td>Email</td>
                           </tr>
                         </thead>
                         <tr>
                           <td data-ruhl="first_name">Andrew</td>
                           <td data-ruhl="last_name">Stone</td>
                           <td data-ruhl="email">andy@stonean.com</td>
                         </tr>
                       </table>
                     </div>




Wednesday, December 9, 2009
RuHL: _use
               So what about iterating over a collection?
                     <select id="states">
                       <option data-ruhl="_use: state_options">
                     </select>

               The option tag will be repeated for each item returned.
               state_options can return an array of hashes:
                  def state_options
                    [ {:value => 'AL', :inner_html => 'Alabama'},
                      {:value => 'AK', :inner_html => 'Alaska'},
                      {:value => 'AZ', :inner_html => 'Arizona'},
                      ...
                    ]
                  end

               :inner_html will set the contents of the tag. all other keys are treated as
               attributes for the tag.




Wednesday, December 9, 2009
RuHL: _swap
                  I have some template text, but want to replace content within a paragraph.


                  #Replace the span tag with the results of language
                    <p>The programming language, <span data-ruhl='_swap:language'/>, is awesome.</p>




Wednesday, December 9, 2009
RuHL: partials, layouts and params
                  <div id="page">
                    <div data-ruhl="_partial:file|header_search"/>
                    <div data-ruhl="_render_" class="metro"/>
                    <div data-ruhl="_partial:file|footer"/>
                  </div>


                  Can pass parameters to methods with a pipe (‘|’).


                  Use _render_ to get the =yield result in Rails. Defining the layout works as
                  expected within Rails. Outside of Rails, you pass a :layout option to the
                  engine constructor. The value of the :layout option is the path to the file.
                  Use :layout_source if you’ve cached the reading of the file (as Rails does).


                  Use _partial to pull in page partials. The method, in this case :file, must
                  return a path to the partial.




Wednesday, December 9, 2009
More information on keywords:


                        http://stonean.com/page/ruhl



                              Moving on. A lot to cover...


Wednesday, December 9, 2009
If the code isn’t in the view, where
                                          does it go?
               • RuHL encourages the use of the presenter pattern.
               • It’s easy with the helpers if you follow the convention in a
                  Rails app:
                  • Have presenter in app/presenters
                  • Name presenter similar to controller conventions (just
                     singular):
                              class UserPresenter < Ruhl::Rails::Presenter
                              end
                     (file name: user_presenter.rb)


Wednesday, December 9, 2009
What does my controller look like?
                  Controller methods look like:
                          def new
                            @post = Post.new

                            present
                          end



                         def create
                           @post = Post.new(params[:post])

                           if @post.save
                             flash[:notice] = 'Post was successfully created.'
                             redirect_to(@post)
                           else
                             present :new
                           end
                         end




Wednesday, December 9, 2009
And this works how?
                  For this example, let’s say we are working in the UsersController.


               • Calling present in the controller method creates a new instance of the
                  UserPresenter.
               • The UserPresenter instance is passed to the view as a local object.
               • RuHL will first apply all methods to the local object. If the local object does
                  not respond to the method, RuHL then tries to apply the method to the
                  context.
               • The context is everything you are accustomed to in a Rails view:
                  • Controller methods exposed with helper_method
                  • Helper methods (ApplicationHelper, UsersHelper)




Wednesday, December 9, 2009
And my model?
                  For this example, let’s say we are working with the UserPresenter.


                     • When the presenter is created, it has a local object called presentee
                        which holds the instance of the model you are presenting. (@user)
                     • If you have this call in your view:
                                              data-ruhl=”first_name”
                        the order is:
                        1. Does the presenter respond_to?(:first_name)
                              1. Does the presenter have first_name defined?
                              2.Does the presentee (@user) have first_name defined?
                        2.Try the method against the context
                              1. Raises exception if method not found.




Wednesday, December 9, 2009
So what about testing?
               • With RuHL, rendering your view is not required to test.
                 • Only interested in the correct placement and content of data-ruhl
                     attributes.
                  • Helper provided:
                <div class="single_box">
                  <h2>Topic: <span data-ruhl="_swap: name"/></h2>

                  <div id="chart">
                    <img data-ruhl="src: chart_period"/>
                  </div>
                </div>

                # To test above:

                it "calls to replace name" do
                  data_ruhl('div.single_box > h2 > span').should == "_swap: name"
                end




Wednesday, December 9, 2009
But wait, there’s more...
               Since RuHL encourages presenters, test your presenters as you would your
               models.


                                 This simplifies your testing!

               • Don’t have to go through your controller to trigger conditions that decide
                  the output in your view.
               • This decreases the amount of time required to run your tests.




Wednesday, December 9, 2009
Summary
               • Allowing code in your view is a bad practice.
                 • This simple requirement ensures a holistic approach.
               • Utilize the presenter pattern to hold the code you would otherwise have
                  placed in your view.
               • Using RuHL encourages continued designer involvement with the
                  maintenance of views.




Wednesday, December 9, 2009

Contenu connexe

Tendances

Learning PHP for Drupal Theming, DC Chicago 2009
Learning PHP for Drupal Theming, DC Chicago 2009Learning PHP for Drupal Theming, DC Chicago 2009
Learning PHP for Drupal Theming, DC Chicago 2009
Emma Jane Hogbin Westby
 
Introducing Modern Perl
Introducing Modern PerlIntroducing Modern Perl
Introducing Modern Perl
Dave Cross
 

Tendances (20)

Introduction to Drupal (7) Theming
Introduction to Drupal (7) ThemingIntroduction to Drupal (7) Theming
Introduction to Drupal (7) Theming
 
PSD to a Drupal Theme (using a base theme)
PSD to a Drupal Theme (using a base theme)PSD to a Drupal Theme (using a base theme)
PSD to a Drupal Theme (using a base theme)
 
Drupal 7 Theme System
Drupal 7 Theme SystemDrupal 7 Theme System
Drupal 7 Theme System
 
Learning PHP for Drupal Theming, DC Chicago 2009
Learning PHP for Drupal Theming, DC Chicago 2009Learning PHP for Drupal Theming, DC Chicago 2009
Learning PHP for Drupal Theming, DC Chicago 2009
 
Drupal theming - a practical approach (European Drupal Days 2015)
Drupal theming - a practical approach (European Drupal Days 2015)Drupal theming - a practical approach (European Drupal Days 2015)
Drupal theming - a practical approach (European Drupal Days 2015)
 
A look at Drupal 7 Theming
A look at Drupal 7 ThemingA look at Drupal 7 Theming
A look at Drupal 7 Theming
 
Drupal Front End PHP
Drupal Front End PHPDrupal Front End PHP
Drupal Front End PHP
 
Drupal 8: Entities
Drupal 8: EntitiesDrupal 8: Entities
Drupal 8: Entities
 
Absolute Beginners Guide to Drupal
Absolute Beginners Guide to DrupalAbsolute Beginners Guide to Drupal
Absolute Beginners Guide to Drupal
 
Design to Theme @ CMSExpo
Design to Theme @ CMSExpoDesign to Theme @ CMSExpo
Design to Theme @ CMSExpo
 
ePUB 3 and Publishing e-books
ePUB 3 and Publishing e-booksePUB 3 and Publishing e-books
ePUB 3 and Publishing e-books
 
Drupal Module Development
Drupal Module DevelopmentDrupal Module Development
Drupal Module Development
 
Mongo-Drupal
Mongo-DrupalMongo-Drupal
Mongo-Drupal
 
DrupalCon Chicago Practical MongoDB and Drupal
DrupalCon Chicago Practical MongoDB and DrupalDrupalCon Chicago Practical MongoDB and Drupal
DrupalCon Chicago Practical MongoDB and Drupal
 
Creating Custom Drupal Modules
Creating Custom Drupal ModulesCreating Custom Drupal Modules
Creating Custom Drupal Modules
 
Intro to-html-backbone-angular
Intro to-html-backbone-angularIntro to-html-backbone-angular
Intro to-html-backbone-angular
 
MongoDB - Ruby document store that doesn't rhyme with ouch
MongoDB - Ruby document store that doesn't rhyme with ouchMongoDB - Ruby document store that doesn't rhyme with ouch
MongoDB - Ruby document store that doesn't rhyme with ouch
 
Introducing Modern Perl
Introducing Modern PerlIntroducing Modern Perl
Introducing Modern Perl
 
Customizing the look and-feel of DSpace
Customizing the look and-feel of DSpaceCustomizing the look and-feel of DSpace
Customizing the look and-feel of DSpace
 
Puppet Design Patterns - PuppetConf
Puppet Design Patterns - PuppetConfPuppet Design Patterns - PuppetConf
Puppet Design Patterns - PuppetConf
 

En vedette

How to give a kick ass demo
How to give a kick ass demoHow to give a kick ass demo
How to give a kick ass demo
stephenlead
 
Power Point 09 10 Formigues
Power Point 09 10 FormiguesPower Point 09 10 Formigues
Power Point 09 10 Formigues
Cucaferatona
 
HPM seminar5 ethics
HPM seminar5 ethicsHPM seminar5 ethics
HPM seminar5 ethics
Sean Cubitt
 
Twitter for Real Estate Agents
Twitter for Real Estate AgentsTwitter for Real Estate Agents
Twitter for Real Estate Agents
Brad Sage
 
Hardware i Software
Hardware i SoftwareHardware i Software
Hardware i Software
kaiser1616
 
Design Filter FIR
Design Filter FIRDesign Filter FIR
Design Filter FIR
Ibnu Fajar
 
Magazine Cover Evaluation
Magazine Cover EvaluationMagazine Cover Evaluation
Magazine Cover Evaluation
guest6d3c71
 
Ssfootball
SsfootballSsfootball
Ssfootball
jaenvit
 

En vedette (20)

130802 デザインマンホール蓋の世界とその楽しみ方
130802 デザインマンホール蓋の世界とその楽しみ方130802 デザインマンホール蓋の世界とその楽しみ方
130802 デザインマンホール蓋の世界とその楽しみ方
 
How to give a kick ass demo
How to give a kick ass demoHow to give a kick ass demo
How to give a kick ass demo
 
Atlas of NSW
Atlas of NSWAtlas of NSW
Atlas of NSW
 
Power Point 09 10 Formigues
Power Point 09 10 FormiguesPower Point 09 10 Formigues
Power Point 09 10 Formigues
 
Hpm8technology
Hpm8technologyHpm8technology
Hpm8technology
 
Performance Testing And Beyond
Performance Testing And BeyondPerformance Testing And Beyond
Performance Testing And Beyond
 
Evening in A Minor: Digital artwork by Shelley M. House
Evening in A Minor: Digital artwork by Shelley M. HouseEvening in A Minor: Digital artwork by Shelley M. House
Evening in A Minor: Digital artwork by Shelley M. House
 
HPM seminar5 ethics
HPM seminar5 ethicsHPM seminar5 ethics
HPM seminar5 ethics
 
Twitter for Real Estate Agents
Twitter for Real Estate AgentsTwitter for Real Estate Agents
Twitter for Real Estate Agents
 
Hardware i Software
Hardware i SoftwareHardware i Software
Hardware i Software
 
Design Filter FIR
Design Filter FIRDesign Filter FIR
Design Filter FIR
 
Ethics pres
Ethics presEthics pres
Ethics pres
 
Magazine Cover Evaluation
Magazine Cover EvaluationMagazine Cover Evaluation
Magazine Cover Evaluation
 
privations / secretions
privations / secretionsprivations / secretions
privations / secretions
 
Nfib ny minimum wage study - 2012
Nfib ny minimum wage study - 2012Nfib ny minimum wage study - 2012
Nfib ny minimum wage study - 2012
 
Book Talk
Book TalkBook Talk
Book Talk
 
03072010 Web
03072010 Web03072010 Web
03072010 Web
 
CBC: New York State Budget & Jobs Summit
CBC: New York State Budget & Jobs SummitCBC: New York State Budget & Jobs Summit
CBC: New York State Budget & Jobs Summit
 
Resisitng Assimilation
Resisitng AssimilationResisitng Assimilation
Resisitng Assimilation
 
Ssfootball
SsfootballSsfootball
Ssfootball
 

Similaire à RuHL

Drupal module development training delhi
Drupal module development training delhiDrupal module development training delhi
Drupal module development training delhi
unitedwebsoft
 

Similaire à RuHL (20)

Drupal theming
Drupal themingDrupal theming
Drupal theming
 
Decoupled drupal
Decoupled drupal Decoupled drupal
Decoupled drupal
 
Basics of XML
Basics of XMLBasics of XML
Basics of XML
 
Building Custom PHP Extensions
Building Custom PHP ExtensionsBuilding Custom PHP Extensions
Building Custom PHP Extensions
 
XML-Javascript
XML-JavascriptXML-Javascript
XML-Javascript
 
XML-Javascript
XML-JavascriptXML-Javascript
XML-Javascript
 
Drupal7 Theming session on the occassion of Drupal7 release party in Delhi NCR
Drupal7 Theming session on the occassion of  Drupal7 release party in Delhi NCRDrupal7 Theming session on the occassion of  Drupal7 release party in Delhi NCR
Drupal7 Theming session on the occassion of Drupal7 release party in Delhi NCR
 
Rails ORM De-mystifying Active Record has_many
Rails ORM De-mystifying Active Record has_manyRails ORM De-mystifying Active Record has_many
Rails ORM De-mystifying Active Record has_many
 
EdTechJoker Spring 2020 - Lecture 7 Drupal intro
EdTechJoker Spring 2020 - Lecture 7 Drupal introEdTechJoker Spring 2020 - Lecture 7 Drupal intro
EdTechJoker Spring 2020 - Lecture 7 Drupal intro
 
Introduction into Drupal site building
Introduction into Drupal site buildingIntroduction into Drupal site building
Introduction into Drupal site building
 
Decoupling Drupal mit dem Lupus Nuxt.js Drupal Stack
Decoupling Drupal mit dem Lupus Nuxt.js Drupal StackDecoupling Drupal mit dem Lupus Nuxt.js Drupal Stack
Decoupling Drupal mit dem Lupus Nuxt.js Drupal Stack
 
Drupal 8 - Corso frontend development
Drupal 8 - Corso frontend developmentDrupal 8 - Corso frontend development
Drupal 8 - Corso frontend development
 
drush_multi @ DrupalDevDays 2010
drush_multi @ DrupalDevDays 2010drush_multi @ DrupalDevDays 2010
drush_multi @ DrupalDevDays 2010
 
Decoupled drupal DcRuhr
Decoupled drupal DcRuhrDecoupled drupal DcRuhr
Decoupled drupal DcRuhr
 
Making The Drupal Pill Easier To Swallow
Making The Drupal Pill Easier To SwallowMaking The Drupal Pill Easier To Swallow
Making The Drupal Pill Easier To Swallow
 
Best Practices For Drupal Developers By Mir Nazim @ Drupal Camp India 2008
Best Practices For Drupal Developers By Mir Nazim @ Drupal Camp India 2008Best Practices For Drupal Developers By Mir Nazim @ Drupal Camp India 2008
Best Practices For Drupal Developers By Mir Nazim @ Drupal Camp India 2008
 
Migrating data into Drupal using the migrate module
Migrating data into Drupal using the migrate moduleMigrating data into Drupal using the migrate module
Migrating data into Drupal using the migrate module
 
Drupal
DrupalDrupal
Drupal
 
Drupal module development training delhi
Drupal module development training delhiDrupal module development training delhi
Drupal module development training delhi
 
Xml 215-presentation
Xml 215-presentationXml 215-presentation
Xml 215-presentation
 

Dernier

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Dernier (20)

🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
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
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
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...
 
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...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
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...
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
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
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 

RuHL

  • 1. RuHL How can I see with all that code in my view? Wednesday, December 9, 2009
  • 2. The first thought... Haml is so far away from HTML, why not take it further? Repeat content instead of having to write loops. %ul - @school.special_features.each do |feature| %li= feature.name # Maybe something like... %ul { repeat: special_features } %li= feature.name Wednesday, December 9, 2009
  • 3. The next minute... I could actually pull this off with HTML. <ul ruby=”_collection: special_features”> <li ruby=”name”/> </ul> # Which later changed to <ul> <li data-ruhl =”_collection: special_features, name”/> </ul> ...more on this coming up Wednesday, December 9, 2009
  • 4. Thoughts from JSP days and beyond • Working with designers inefficient after converting views. • Why convert the code from HTML to a language that generates HTML if it is not necessary? • Views didn’t garner the same architecture attention ‘code’ did. • Did formulating the Fat Model/Skinny Controller idea take everyone’s energy? • Over time, code in views grew as quick fixes were applied. • Little restrictions on what is allowed encourages misuse. • Testing views more difficult than models or controllers. • Shouldn’t the multiple attempts to improve view testing be an indicator? Wednesday, December 9, 2009
  • 5. The 2 minute RuHL history. • The attribute was initially called ruby. • Peter Cooper suggested data-ruby for HTML5 compliance. • Coworker suggested data-ruhl to prevent potential naming collisions. • Crystallized the idea of ‘No code in views’ • This became the rule that could not be broken. • Should not have to code extra HTML tags to make something work. • Exceptions to this arose and _swap was introduced. (details later) • Functionality to be driven by real use cases, not imagined scenarios. Wednesday, December 9, 2009
  • 6. Standalone RuHL RuHL can be used outside Sinatra and Rails. Ruhl::Engine.new(File.read(ʻhello_world.ruhlʼ)).render(self) Breaking it down: data = File.read(ʻhello_world.ruhlʼ) # read the html file engine = Ruhl::Engine.new(data) # instantiate the engine html = engine.render(self) # render the html # using self for context In the Rails helper, self is the @template object available to the controller. This gives you access to all the helpers available within the view. Wednesday, December 9, 2009
  • 7. RuHL: _if How would I conditionally display data? #Don’t show the div or it’s contents if there aren’t users. <div data-ruhl="_if: users?"> <table> <thead> <tr> <td>First Name</td> <td>Last Name</td> <td>Email</td> </tr> </thead> <tr data-ruhl="_collection: user_list"> <td data-ruhl="first_name">Andrew</td> <td data-ruhl="last_name">Stone</td> <td data-ruhl="email">andy@stonean.com</td> </tr> </table> </div> Wednesday, December 9, 2009
  • 8. RuHL: _unless How would I conditionally display data? #Show the message if there aren’t users. <p data-ruhl="_unless: users?">No Users were found.</p> If users, the <p> tag would not be included on output. Wednesday, December 9, 2009
  • 9. RuHL: _use I want to use the user object as the local object within the scope of the div to simplify the calls. The methods will first be tried against the local object, then the global context. <div data-ruhl="_use: current_user"> <h1> Editing <span data-ruhl="_swap: full_name"/></h1> <table> <thead> <tr> <td>First Name</td> <td>Last Name</td> <td>Email</td> </tr> </thead> <tr> <td data-ruhl="first_name">Andrew</td> <td data-ruhl="last_name">Stone</td> <td data-ruhl="email">andy@stonean.com</td> </tr> </table> </div> Wednesday, December 9, 2009
  • 10. RuHL: _use So what about iterating over a collection? <select id="states"> <option data-ruhl="_use: state_options"> </select> The option tag will be repeated for each item returned. state_options can return an array of hashes: def state_options [ {:value => 'AL', :inner_html => 'Alabama'}, {:value => 'AK', :inner_html => 'Alaska'}, {:value => 'AZ', :inner_html => 'Arizona'}, ... ] end :inner_html will set the contents of the tag. all other keys are treated as attributes for the tag. Wednesday, December 9, 2009
  • 11. RuHL: _swap I have some template text, but want to replace content within a paragraph. #Replace the span tag with the results of language <p>The programming language, <span data-ruhl='_swap:language'/>, is awesome.</p> Wednesday, December 9, 2009
  • 12. RuHL: partials, layouts and params <div id="page"> <div data-ruhl="_partial:file|header_search"/> <div data-ruhl="_render_" class="metro"/> <div data-ruhl="_partial:file|footer"/> </div> Can pass parameters to methods with a pipe (‘|’). Use _render_ to get the =yield result in Rails. Defining the layout works as expected within Rails. Outside of Rails, you pass a :layout option to the engine constructor. The value of the :layout option is the path to the file. Use :layout_source if you’ve cached the reading of the file (as Rails does). Use _partial to pull in page partials. The method, in this case :file, must return a path to the partial. Wednesday, December 9, 2009
  • 13. More information on keywords: http://stonean.com/page/ruhl Moving on. A lot to cover... Wednesday, December 9, 2009
  • 14. If the code isn’t in the view, where does it go? • RuHL encourages the use of the presenter pattern. • It’s easy with the helpers if you follow the convention in a Rails app: • Have presenter in app/presenters • Name presenter similar to controller conventions (just singular): class UserPresenter < Ruhl::Rails::Presenter end (file name: user_presenter.rb) Wednesday, December 9, 2009
  • 15. What does my controller look like? Controller methods look like: def new @post = Post.new present end def create @post = Post.new(params[:post]) if @post.save flash[:notice] = 'Post was successfully created.' redirect_to(@post) else present :new end end Wednesday, December 9, 2009
  • 16. And this works how? For this example, let’s say we are working in the UsersController. • Calling present in the controller method creates a new instance of the UserPresenter. • The UserPresenter instance is passed to the view as a local object. • RuHL will first apply all methods to the local object. If the local object does not respond to the method, RuHL then tries to apply the method to the context. • The context is everything you are accustomed to in a Rails view: • Controller methods exposed with helper_method • Helper methods (ApplicationHelper, UsersHelper) Wednesday, December 9, 2009
  • 17. And my model? For this example, let’s say we are working with the UserPresenter. • When the presenter is created, it has a local object called presentee which holds the instance of the model you are presenting. (@user) • If you have this call in your view: data-ruhl=”first_name” the order is: 1. Does the presenter respond_to?(:first_name) 1. Does the presenter have first_name defined? 2.Does the presentee (@user) have first_name defined? 2.Try the method against the context 1. Raises exception if method not found. Wednesday, December 9, 2009
  • 18. So what about testing? • With RuHL, rendering your view is not required to test. • Only interested in the correct placement and content of data-ruhl attributes. • Helper provided: <div class="single_box"> <h2>Topic: <span data-ruhl="_swap: name"/></h2> <div id="chart"> <img data-ruhl="src: chart_period"/> </div> </div> # To test above: it "calls to replace name" do data_ruhl('div.single_box > h2 > span').should == "_swap: name" end Wednesday, December 9, 2009
  • 19. But wait, there’s more... Since RuHL encourages presenters, test your presenters as you would your models. This simplifies your testing! • Don’t have to go through your controller to trigger conditions that decide the output in your view. • This decreases the amount of time required to run your tests. Wednesday, December 9, 2009
  • 20. Summary • Allowing code in your view is a bad practice. • This simple requirement ensures a holistic approach. • Utilize the presenter pattern to hold the code you would otherwise have placed in your view. • Using RuHL encourages continued designer involvement with the maintenance of views. Wednesday, December 9, 2009