Taming of the View
                            RubyConf India 2010
                            Sarah Taraporewalla
                              ThoughtWorks




Sunday, 21 March 2010
I will attempt to
                        convince you that:




Sunday, 21 March 2010
I will attempt to
                         convince you that:
                   -    the current template engines are too
                        permissive




Sunday, 21 March 2010
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
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
What is wrong with
                         these examples?


Sunday, 21 March 2010
Example 1




Sunday, 21 March 2010
Example 2




Sunday, 21 March 2010
Example 3




Sunday, 21 March 2010
Example 4




Sunday, 21 March 2010
AGH! This is really
                        really easy in Rails3
                                now!


Sunday, 21 March 2010
Controller




Sunday, 21 March 2010
Why do we get thin
                          controllers?


Sunday, 21 March 2010
Push vs Pull




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

Sunday, 21 March 2010
Pull

                        View   Controllers   Model




Sunday, 21 March 2010
Pull - over time

                             Controllers   Model
              View




Sunday, 21 March 2010
Push

                        View   Controllers   Model




Sunday, 21 March 2010
Push - over time

                        View                      Model

                                    Controllers




Sunday, 21 March 2010
Push - and then

                        View        Controllers   Services




Sunday, 21 March 2010
What do we want?




Sunday, 21 March 2010
Testable Code




Sunday, 21 March 2010
Testable Code


                        Don’t put logic in your views




Sunday, 21 March 2010
Single Responsibility




Sunday, 21 March 2010
Single Responsibility


                        Don’t let changes in column
                         names effect your views



Sunday, 21 March 2010
Separation of Concerns




Sunday, 21 March 2010
Separation of Concerns


                        Enforce strict separation of
                             model and view



Sunday, 21 March 2010
How do we improve on
                  the examples?


Sunday, 21 March 2010
Example 1 - Before




Sunday, 21 March 2010
Example 1 - Improved




Sunday, 21 March 2010
Example 2 - Before




Sunday, 21 March 2010
Example 2 - Improved




Sunday, 21 March 2010
Example 2 - Improved




Sunday, 21 March 2010
Example 3 - Before




Sunday, 21 March 2010
Example 4 - Before




Sunday, 21 March 2010
Example 3,4 - Improved




Sunday, 21 March 2010
Example 3,4 - Improved




Sunday, 21 March 2010
If you make it easy to
                 do the wrong thing, the
                    wrong thing will be
                          done

Sunday, 21 March 2010
If you make it easy to
                   do the right thing, the
                  right thing will be done


Sunday, 21 March 2010
Haml



Sunday, 21 March 2010
Haml




Sunday, 21 March 2010
Haml

                        Good




Sunday, 21 March 2010
Haml

                        Good
                           But ...



Sunday, 21 March 2010
Haml

                        Good
                           But ...
                                still allows ruby


Sunday, 21 March 2010
String Template



Sunday, 21 March 2010
StringTemplate

               - Strict separation of model & view
               - Fits well with functional programming
               - Written in Java, .Net, Python
                        is NOT Turing complete

Sunday, 21 March 2010
1. Create a StringTemplate Object with
                                  the template




Sunday, 21 March 2010
1. Create a StringTemplate Object with
                                  the template




                                                 string or file




Sunday, 21 March 2010
2. Set attributes and their values




Sunday, 21 March 2010
3. Call toString() to mash attributes onto
                               the template




Sunday, 21 March 2010
Testable?




Sunday, 21 March 2010
Testable? Yes!




Sunday, 21 March 2010
Single Responsibility?




Sunday, 21 March 2010
Single Responsibility?
                                 Yes!




Sunday, 21 March 2010
Separation of
                         Concerns?




Sunday, 21 March 2010
Separation of
                             Concerns? Yes!


                        It is the essence of its philosophy




Sunday, 21 March 2010
Other cool things...




Sunday, 21 March 2010
Templates (Partials)




Sunday, 21 March 2010
Iterators




Sunday, 21 March 2010
Iterators




Sunday, 21 March 2010
Renderers




Sunday, 21 March 2010
Localisation




Sunday, 21 March 2010
But, we do Ruby



Sunday, 21 March 2010
Slippers



Sunday, 21 March 2010
Slippers




Sunday, 21 March 2010
Slippers & Ramaze




Sunday, 21 March 2010
Slippers - Example




Sunday, 21 March 2010
Slippers - Example




Sunday, 21 March 2010
Slippers - Example




Sunday, 21 March 2010
Slippers - Example
                                                      index.st




          person/index.st


                                      person/age.st
Sunday, 21 March 2010
Slippers - Example
                                      person/age_renderer.rb




    controller/role_renderer.rb




Sunday, 21 March 2010
Slippers - Example




Sunday, 21 March 2010
Slippers - Testable?

                   • Holes are correctly plugged
                   • Correct instance variables set
                   • Renderers


Sunday, 21 March 2010
Slippers - Testable?

                   • Holes are correctly plugged
                   • Correct instance variables set
                   • Renderers


Sunday, 21 March 2010
Slippers - Testable?




Sunday, 21 March 2010
Slippers - Testable?




Sunday, 21 March 2010
Slippers - Testable?




Sunday, 21 March 2010
Slippers - Testable?

                   • Holes are correctly plugged
                   • Correct instance variables set
                   • Renderers


Sunday, 21 March 2010
Slippers - Testable?




Sunday, 21 March 2010
Slippers - Testable?




Sunday, 21 March 2010
Slippers - Testable?




Sunday, 21 March 2010
Slippers - Testable?

                   • Holes are correctly plugged
                   • Correct instance variables set
                   • Renderers


Sunday, 21 March 2010
Slippers - Testable?




Sunday, 21 March 2010
Slippers




Sunday, 21 March 2010
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
Have I convinced you
                                that:




Sunday, 21 March 2010
Have I convinced you
                                that:
                   -    the current template engines are too
                        permissive




Sunday, 21 March 2010
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
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
questions?



Sunday, 21 March 2010

Taming The View for Rubyconf India