Taming The View for Rubyconf India

89
Taming of the View RubyConf India 2010 Sarah Taraporewalla ThoughtWorks Sunday, 21 March 2010

Transcript of Taming The View for Rubyconf India

Page 1: Taming The View for Rubyconf India

Taming of the ViewRubyConf India 2010Sarah Taraporewalla

ThoughtWorks

Sunday, 21 March 2010

Page 2: Taming The View for Rubyconf India

I will attempt to convince you that:

Sunday, 21 March 2010

Page 3: Taming The View for Rubyconf India

I will attempt to convince you that:

- the current template engines are too permissive

Sunday, 21 March 2010

Page 4: Taming The View for Rubyconf India

I will attempt to convince you that:

- the current template engines are too permissive

- there are loads of code bases that break fundamental principles

Sunday, 21 March 2010

Page 5: Taming The View for Rubyconf India

I will attempt to convince you that:

- the current template engines are too permissive

- there are loads of code bases that break fundamental principles

- you should turn to other template engines

Sunday, 21 March 2010

Page 6: Taming The View for Rubyconf India

What is wrong with these examples?

Sunday, 21 March 2010

Page 7: Taming The View for Rubyconf India

Example 1

Sunday, 21 March 2010

Page 8: Taming The View for Rubyconf India

Example 2

Sunday, 21 March 2010

Page 9: Taming The View for Rubyconf India

Example 3

Sunday, 21 March 2010

Page 10: Taming The View for Rubyconf India

Example 4

Sunday, 21 March 2010

Page 11: Taming The View for Rubyconf India

AGH! This is really really easy in Rails3

now!

Sunday, 21 March 2010

Page 12: Taming The View for Rubyconf India

Controller

Sunday, 21 March 2010

Page 13: Taming The View for Rubyconf India

Why do we get thin controllers?

Sunday, 21 March 2010

Page 14: Taming The View for Rubyconf India

Push vs Pull

http://www.flickr.com/photos/91772025@N00/444044158/ CC BY-ND 2.0

Sunday, 21 March 2010

Page 15: Taming The View for Rubyconf India

Pull

View Controllers Model

Sunday, 21 March 2010

Page 16: Taming The View for Rubyconf India

Pull - over time

ViewControllers Model

Sunday, 21 March 2010

Page 17: Taming The View for Rubyconf India

Push

View Controllers Model

Sunday, 21 March 2010

Page 18: Taming The View for Rubyconf India

Push - over time

View

Controllers

Model

Sunday, 21 March 2010

Page 19: Taming The View for Rubyconf India

Push - and then

View Controllers Services

Sunday, 21 March 2010

Page 20: Taming The View for Rubyconf India

What do we want?

Sunday, 21 March 2010

Page 21: Taming The View for Rubyconf India

Testable Code

Sunday, 21 March 2010

Page 22: Taming The View for Rubyconf India

Testable Code

Don’t put logic in your views

Sunday, 21 March 2010

Page 23: Taming The View for Rubyconf India

Single Responsibility

Sunday, 21 March 2010

Page 24: Taming The View for Rubyconf India

Single Responsibility

Don’t let changes in column names effect your views

Sunday, 21 March 2010

Page 25: Taming The View for Rubyconf India

Separation of Concerns

Sunday, 21 March 2010

Page 26: Taming The View for Rubyconf India

Separation of Concerns

Enforce strict separation of model and view

Sunday, 21 March 2010

Page 27: Taming The View for Rubyconf India

How do we improve on the examples?

Sunday, 21 March 2010

Page 28: Taming The View for Rubyconf India

Example 1 - Before

Sunday, 21 March 2010

Page 29: Taming The View for Rubyconf India

Example 1 - Improved

Sunday, 21 March 2010

Page 30: Taming The View for Rubyconf India

Example 2 - Before

Sunday, 21 March 2010

Page 31: Taming The View for Rubyconf India

Example 2 - Improved

Sunday, 21 March 2010

Page 32: Taming The View for Rubyconf India

Example 2 - Improved

Sunday, 21 March 2010

Page 33: Taming The View for Rubyconf India

Example 3 - Before

Sunday, 21 March 2010

Page 34: Taming The View for Rubyconf India

Example 4 - Before

Sunday, 21 March 2010

Page 35: Taming The View for Rubyconf India

Example 3,4 - Improved

Sunday, 21 March 2010

Page 36: Taming The View for Rubyconf India

Example 3,4 - Improved

Sunday, 21 March 2010

Page 37: Taming The View for Rubyconf India

If you make it easy to do the wrong thing, the

wrong thing will be done

Sunday, 21 March 2010

Page 38: Taming The View for Rubyconf India

If you make it easy to do the right thing, the

right thing will be done

Sunday, 21 March 2010

Page 39: Taming The View for Rubyconf India

Haml

Sunday, 21 March 2010

Page 40: Taming The View for Rubyconf India

Haml

Sunday, 21 March 2010

Page 41: Taming The View for Rubyconf India

Haml

Good

Sunday, 21 March 2010

Page 42: Taming The View for Rubyconf India

Haml

Good

But ...

Sunday, 21 March 2010

Page 43: Taming The View for Rubyconf India

Haml

Good

But ...

still allows ruby

Sunday, 21 March 2010

Page 44: Taming The View for Rubyconf India

String Template

Sunday, 21 March 2010

Page 45: Taming The View for Rubyconf India

StringTemplate

- Strict separation of model & view

- Fits well with functional programming

- Written in Java, .Net, Python

is NOT Turing complete

Sunday, 21 March 2010

Page 46: Taming The View for Rubyconf India

1. Create a StringTemplate Object with the template

Sunday, 21 March 2010

Page 47: Taming The View for Rubyconf India

1. Create a StringTemplate Object with the template

string or file

Sunday, 21 March 2010

Page 48: Taming The View for Rubyconf India

2. Set attributes and their values

Sunday, 21 March 2010

Page 49: Taming The View for Rubyconf India

3. Call toString() to mash attributes onto the template

Sunday, 21 March 2010

Page 50: Taming The View for Rubyconf India

Testable?

Sunday, 21 March 2010

Page 51: Taming The View for Rubyconf India

Testable? Yes!

Sunday, 21 March 2010

Page 52: Taming The View for Rubyconf India

Single Responsibility?

Sunday, 21 March 2010

Page 53: Taming The View for Rubyconf India

Single Responsibility? Yes!

Sunday, 21 March 2010

Page 54: Taming The View for Rubyconf India

Separation of Concerns?

Sunday, 21 March 2010

Page 55: Taming The View for Rubyconf India

Separation of Concerns? Yes!

It is the essence of its philosophy

Sunday, 21 March 2010

Page 56: Taming The View for Rubyconf India

Other cool things...

Sunday, 21 March 2010

Page 57: Taming The View for Rubyconf India

Templates (Partials)

Sunday, 21 March 2010

Page 58: Taming The View for Rubyconf India

Iterators

Sunday, 21 March 2010

Page 59: Taming The View for Rubyconf India

Iterators

Sunday, 21 March 2010

Page 60: Taming The View for Rubyconf India

Renderers

Sunday, 21 March 2010

Page 61: Taming The View for Rubyconf India

Localisation

Sunday, 21 March 2010

Page 62: Taming The View for Rubyconf India

But, we do Ruby

Sunday, 21 March 2010

Page 63: Taming The View for Rubyconf India

Slippers

Sunday, 21 March 2010

Page 64: Taming The View for Rubyconf India

Slippers

Sunday, 21 March 2010

Page 65: Taming The View for Rubyconf India

Slippers & Ramaze

Sunday, 21 March 2010

Page 66: Taming The View for Rubyconf India

Slippers - Example

Sunday, 21 March 2010

Page 67: Taming The View for Rubyconf India

Slippers - Example

Sunday, 21 March 2010

Page 68: Taming The View for Rubyconf India

Slippers - Example

Sunday, 21 March 2010

Page 69: Taming The View for Rubyconf India

Slippers - Exampleindex.st

person/index.st

person/age.stSunday, 21 March 2010

Page 70: Taming The View for Rubyconf India

Slippers - Exampleperson/age_renderer.rb

controller/role_renderer.rb

Sunday, 21 March 2010

Page 71: Taming The View for Rubyconf India

Slippers - Example

Sunday, 21 March 2010

Page 72: Taming The View for Rubyconf India

Slippers - Testable?

• Holes are correctly plugged

• Correct instance variables set

• Renderers

Sunday, 21 March 2010

Page 73: Taming The View for Rubyconf India

• Holes are correctly plugged

• Correct instance variables set

• Renderers

Slippers - Testable?

Sunday, 21 March 2010

Page 74: Taming The View for Rubyconf India

Slippers - Testable?

Sunday, 21 March 2010

Page 75: Taming The View for Rubyconf India

Slippers - Testable?

Sunday, 21 March 2010

Page 76: Taming The View for Rubyconf India

Slippers - Testable?

Sunday, 21 March 2010

Page 77: Taming The View for Rubyconf India

• Holes are correctly plugged

• Correct instance variables set

• Renderers

Slippers - Testable?

Sunday, 21 March 2010

Page 78: Taming The View for Rubyconf India

Slippers - Testable?

Sunday, 21 March 2010

Page 79: Taming The View for Rubyconf India

Slippers - Testable?

Sunday, 21 March 2010

Page 80: Taming The View for Rubyconf India

Slippers - Testable?

Sunday, 21 March 2010

Page 81: Taming The View for Rubyconf India

• Holes are correctly plugged

• Correct instance variables set

• Renderers

Slippers - Testable?

Sunday, 21 March 2010

Page 82: Taming The View for Rubyconf India

Slippers - Testable?

Sunday, 21 March 2010

Page 83: Taming The View for Rubyconf India

Slippers

Sunday, 21 March 2010

Page 84: Taming The View for Rubyconf India

Slippers

• Can be found at:

‣ http://slippersRb.com

‣ http://github.com/starapor/slippers

‣ http://gemcutter.org/gems/slippers

• Ramaze view template

‣ http://ramaze.net

Sunday, 21 March 2010

Page 85: Taming The View for Rubyconf India

Have I convinced you that:

Sunday, 21 March 2010

Page 86: Taming The View for Rubyconf India

Have I convinced you that:

- the current template engines are too permissive

Sunday, 21 March 2010

Page 87: Taming The View for Rubyconf India

Have I convinced you that:

- the current template engines are too permissive

- there are loads of code bases that break fundamental principles

Sunday, 21 March 2010

Page 88: Taming The View for Rubyconf India

Have I convinced you that:

- the current template engines are too permissive

- there are loads of code bases that break fundamental principles

- we should turn to other template engines

Sunday, 21 March 2010

Page 89: Taming The View for Rubyconf India

questions?

Sunday, 21 March 2010