DRY up your views
-
Upload
lachie -
Category
Technology
-
view
1.354 -
download
1
description
Transcript of DRY up your views
![Page 1: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/1.jpg)
smartbomb
o or r
DRY your views!Lachie Cox
![Page 2: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/2.jpg)
smartbombsmartbomb
DRY‣ don’t repeat yourself, mmkay?
![Page 3: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/3.jpg)
smartbombsmartbomb
M V C‣v is for vexing
![Page 4: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/4.jpg)
smartbombsmartbomb
DRYing models is
easy
![Page 5: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/5.jpg)
smartbombsmartbomb
DRYing ctrllrs is
OK
![Page 6: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/6.jpg)
smartbombsmartbomb
DRYing views is
vexatious
![Page 7: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/7.jpg)
smartbombsmartbomb
WET views
sca!olding
![Page 8: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/8.jpg)
smartbombsmartbomb
![Page 9: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/9.jpg)
smartbombsmartbomb
WET views
erb
layouts
partials
![Page 10: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/10.jpg)
smartbombsmartbomb
![Page 11: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/11.jpg)
smartbombsmartbomb
<% labeled_form_for :thing, things_path do |f| %> <%= f.text_field :name %> <%= f.text_field :aspect %> <%= f.check_box :existing %> <%= submit_tag 'Save' %><% end %>
![Page 12: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/12.jpg)
smartbombsmartbomb
<% labeled_form_for :thing, things_path do |f| %>
<%= f.text_field :name %>
<%= f.text_field :aspect %>
<%= f.check_box :existing %>
<%= submit_tag 'Save' %>
<% end %>
WASTE
![Page 13: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/13.jpg)
smartbombsmartbomb
waste baaaad
![Page 14: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/14.jpg)
smartbombsmartbomb
components
engines
![Page 15: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/15.jpg)
smartbombsmartbomb
![Page 16: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/16.jpg)
smartbombsmartbomb
‣ http://codefluency.com/2007/5/18/when-v-is-for-vexing
‣ Bruce Williams
![Page 17: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/17.jpg)
smartbombsmartbomb
Don’t Fear Helpers
Make them APIs
![Page 18: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/18.jpg)
smartbombsmartbomb
Make them semantic
![Page 19: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/19.jpg)
smartbombsmartbomb
helpers as api
<%= describe person,:blank_message => "Nobody" %>
![Page 20: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/20.jpg)
smartbombsmartbomb
helpers as api
<% rounded_box do %> <h2>Products</h2><% end %>
![Page 21: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/21.jpg)
smartbombsmartbomb
stencil
‣ http://stencil.rubyforge.org
![Page 22: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/22.jpg)
smartbombsmartbomb
semantic helpers
as objects
![Page 23: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/23.jpg)
smartbombsmartbomb
a cart stencil
class CartStencil < Stencil ... def to_s render(:partial => 'shared/cart', :object => @cart) end def summary if @cart.blank? "Your cart is empty. Buy stuff!" else "You have #{@cart.size} items in your cart." end endend
![Page 24: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/24.jpg)
smartbombsmartbomb
a cart stencil
<% cart = cart() %> <!-- CartStencil.new -->
<%= cart.summary %>
<%= cart %> <!-- cart.to_s -->
![Page 25: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/25.jpg)
smartbombsmartbomb
more than a stencil
module CartControllerHelper ...end
class UserController include CartControllerHelper load_cart :only => [:show]end
![Page 26: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/26.jpg)
smartbombsmartbomb
Vcart
(stencil)
Ccart
(mixin)
![Page 27: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/27.jpg)
smartbombsmartbomb
Stencil+
Mixin
CSS
+javascript
+
&c.
![Page 28: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/28.jpg)
smartbombsmartbomb
lightweight
widgetty
thingo
![Page 29: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/29.jpg)
smartbombsmartbomb
![Page 30: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/30.jpg)
smartbombsmartbomb
DRY
javascript
![Page 31: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/31.jpg)
smartbombsmartbomb
RJS
![Page 32: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/32.jpg)
smartbombsmartbomb
![Page 33: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/33.jpg)
smartbombsmartbomb
document.getElementById("foobar")
$("foobar")
![Page 34: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/34.jpg)
smartbombsmartbomb
use prototype‣ until optimisation
![Page 35: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/35.jpg)
smartbombsmartbomb
<%= link_to_function 'declaim', 'alert("yay for smarties!")' %>
<%= link_to 'delete', smarty_path(42), :method => :delete %>
![Page 36: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/36.jpg)
smartbombsmartbomb
![Page 37: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/37.jpg)
smartbombsmartbomb
srsly, wtf?
<a onclick="var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'delete'); f.appendChild(m);f.submit();return false;" href="/smarties/1">delete</a>
![Page 38: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/38.jpg)
smartbombsmartbomb
![Page 39: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/39.jpg)
smartbombsmartbomb
be
less
obtrusive
![Page 40: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/40.jpg)
smartbombsmartbomb
use
less
code
![Page 41: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/41.jpg)
smartbombsmartbomb
OMG
javascript
has classes
too!
![Page 42: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/42.jpg)
smartbombsmartbomb
use
&
reuse
![Page 43: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/43.jpg)
smartbombsmartbomb
lowpro‣ http://danwebb.net/lowpro
![Page 44: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/44.jpg)
smartbombsmartbomb
reusable
behaviours
![Page 45: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/45.jpg)
smartbombsmartbomb
![Page 46: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/46.jpg)
smartbombsmartbomb
![Page 47: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/47.jpg)
smartbombsmartbomb
<a class="forgotten" href="/accounts/forgotten">I've forgotten my password.</a>
HTML
![Page 48: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/48.jpg)
smartbombsmartbomb
Event.addBehavior({ '.forgotten': Toggler('forgotten_password','cancel')})
<a class="forgotten" href="/accounts/forgotten">I've forgotten my password.</a>
javascript
HTML
![Page 49: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/49.jpg)
smartbombsmartbomb
![Page 50: DRY up your views](https://reader034.fdocuments.net/reader034/viewer/2022052618/54b6e4f84a7959e8708b45f0/html5/thumbnails/50.jpg)
smartbombsmartbomb
DRY
its good for the soul