You're Doing It Wrong
codinghorror.com
codinghorror.com
I've worked on plenty of sites of varying complexity. If your templates have anything more than trivial logic in them, You're Doing It Wrong!
Adding complex programming constructs back into the templating language is NOT the way forward. It's the way BACKWARDS. It's where we came from. Templating languages are intentionally simple because you're NOT supposed to have lots of complex logic in your template. Sure, you might have a little bit of if/then/else and a bit of looping, but beyond that, if you need anything more than that, you're doing it Very Wrong.
As for ending up with huge templates - again, Jeff is Doing It Wrong. To avoid having monster spaghetti templates, you're supposed to break down your templates into their elements, much like you break down your large pieces of functionality into classes and methods.
templates with logic/inline JS -> templates without logic/ external js -> almost no templates/ JS + Ajax to draw the page on the fly
Well of course code and markup are two different things, but incompatible? Not really.
We use HTML templates simply to let the designers play with the templates without our interference. It also makes changes easier to integrate. Otherwise we could use a DSL and transform the syntax tree in valid xhtml (which many people are already doing).
> Rather than poking holes in HTML to insert code, we should simply treat HTML as code.
Actually that means poking holes in your code to insert HTML. And it's worse IMO.
While I like some of the articles Jeff has published on his blog, but I've long since deleted the blog's feed from my list because the signal to noise ration is really awful.
The most common one is tag libraries in place of scripting. The Java world is probably the biggest offender here. The JSTL started out as a way to create cleaner looking pages - and maybe it has - but the spec got to the point where they were recreating the entire Java language as a separate set of tags. Supposedly, these tags are easier for "non-programmers" to use, but by the time you're setting properties for objects and using conditionals and loops, you may as well admit you're a programmer.
I'm no big fan of long jsp pages, but I prefer Rails's approach to scripting: if a task requires programming logic, use a programming language.
Ultimately, I found the "you're doing it wrong if you include scripting in a view" to be very unconvincing, especially if the tags you replace it with end up recreating the programming language you're not supposed to be using in the first place.
html do
head { title "Boats.com" }
body do
h1 "Boats.com has great deals"
ul do
li "$49 for a canoe"
li "$39 for a raft"
li "$29 for a huge boot that floats and can fit 5 people"
end
end
endMore examples, please! Here's some haml, from the haml site. (I don't actually know haml, but perhaps I soon will:)
#content
.left.column
%h2 Welcome to our site!
%p= print_information
.right.column= render :partial => "sidebar"
Grading these competitions is hard, since it depends on taste and you can't always tell from a short example how something will perform in the field. haml may be a bit too terse (though I say that without being convinced that it's true; terse is generally good), and on first glance it will confuse the daylights out of an otherwise HTML-savvy person who has never seen it before. But I certainly like it compared to tag soup. (html
[:head {:title "Boats.com" }]
[:body
[:h1 "Boats.com has great deals"]
[:ul
[:li "$49 for a canoe"]]
(sidebar)])
render(:partial, sidebar) is accomplished by just calling another function that generates html.PERL has packages that allow you to put your HTML inside your code, or allow you to put your code inside templates.
It soon became clear to me that neither is the win - the only real difference is "if I don't escape (via a quote character or a <? or a <%), what is a character considered - HTML or code?".
The problem is that code is there are two different conceptual trees - the HTML tree with nested elements, and the code tree with nested logic, and these trees do not have a 1:1 correspondence.
This is not a simple problem, and there are not simple answers.
Programming in Ruby and Rails for the last few years, I've become a fan of partials, but the tradeoff that you get is that for greater ability to read one file, you lose locality of reference ( http://en.wikipedia.org/wiki/Locality_of_reference ) - you might have a table row in one file, and the data data in another file entirely.
In general, I am a huge fan of straight text based editing, and always raise a dubious eyebrow at proposals for graphical editors and strange new editing tools.
...but I seriously wonder if there's some solution off in the space of a different kind of editor that lets you switch back and forth between viewing the code tree and the presentation tree...
I'm dumbfounded that he does not notice the circular nature of his proposal.
Pretty much every Lisp has a way to express markup directly as S-expressions.