[From Coding Horror: You’re Doing It Wrong]
Jeff Atwood talks about templating engines (for generating html) being wrong… and I agree, with code like this:
<%foreach (var User in Users) { %>
<div class="action-time"><%= ActionSpan(User)%></div>
<% if (User.IsAnonymous) { %>
<div class="gravatar32"><%= RenderGravatar(User)%></div>
<div class="details"><%= RepSpan(User)%><br/><%= Flair(User)%></div>
<% } else { %>
<div class="anon">anonymous</div>
<% } %>
<% } %>
But, and I might be biased here, the WebObjects approach is much more pure, much more clean, from a markup vs. code point of view.
A quick crack at something similar in WebObjects would look like:
<webobject name="UserRepetition"> <div class="action-time"><webobject name="UserActionSpan"/></div> <webobject name="IsAnonymousConditional"> <div class="gravatar32"><webobject name="UserGravatar" /></div>
<div class="details"><webobject name="UserRepSpan" /><br/><webobject name="UserFlair" /></div> </webobject> <webobject name="IsNotAnonymousConditional"> <div class="anon">anonymous</div> </webobject> </webobject>
With a bindings file looking like this:
UserRepetition: WORepetition { list = users; item = currentUser; } UserActionSpan: SomeOtherCustomWOComponentToDisplayThisThingMaybe { } IsAnonymousConditional: WOConditional { condition = currentUser.isAnonymous; } IsNotAnonymousConditional: WOConditional { condition = currentUser.isAnonymous; negate = true; } ...
Not too bad, eh? If you’re looking for separating your presentation from your code, well… I don’t think there’s anything better. But then, like I said, I’m biased.
It’s such a shame, with all the comments, that not a one has brought up the Old Lady…
I hate to say it, but the WO approach is the correct one of the two.