I've been reading this paper: Enforcing Strict Model-View Separation in Template Engines (PDF).
It contends that you only need four constructs within the views:
- attribute reference
- conditional template inclusion based upon presence/absence of an attribute
- recursive template references
- template application to a multi-valued attribute similar to lambda functions and LISP’s map operator
No other logic is allowed within the view - for example if (attr < 5)
would not be allowed, only if (valueLessThanFive)
Is this reasonable? Most MVC frameworks allow any logic within the views, which can lead to business logic creeping into the view. But is it really possible to get by with only those four constructs?
For "zebra striped" tables, the paper suggests mapping a list of templates onto the list of data - for example:
map(myList, [oddRowTemplate, evenRowTemplate])
But what if you want to make the first and last rows different styles? What if the 3rd row should be purple on Tuesdays because your graphic designer is mad? These are view-specific (for example if I was outputting the same data as XML I wouldn't use them) so don't seem to belong in the model or controller.
In summary:
- Do you need logic above the four constructs listed above within views?
- If so, how do you restrict business logic creeping in?
- If not, how would you make the 3rd row purple on Tuesdays?