News for hackers. Stuff that matters.
Re:Simplify templating with node.js, jsdom 0.2.0 and weld by simonw on Saturday February 19, 2011 @01:00AM (#2238548)
DOM-based templating is a horrible idea, at least for the majority of projects I've ever worked on.

The whole point of using templates is to separate your presentation logic from the rest of your application - ideally, you can hand the templates over to a client-side developer/designer and they can re-jiggle everything on their own.

If your templating code works by performing manipulations against a DOM, even minor changes to your page structure require to to completely rewrite both the templates and the code that manipulates them. I'd much rather leave my "view" code alone and just alter the template.

Re:Simplify templating with node.js, jsdom 0.2.0 and weld by piranha on Saturday February 19, 2011 @05:26AM (#2238837)
Also it's probably worth to mention that manipulation of DOM is not that fast. Every performance optimization article starts with "don't manipulate DOM directly, make a batch of your changes and then inject them in one step".
Re:Simplify templating with node.js, jsdom 0.2.0 and weld by prodigal_erik on Saturday February 19, 2011 @06:18AM (#2238873)
I don't see any reason for JSDOM in the server to be slow, the object tree being manipulated isn't all that complicated. They aren't suggesting the typical mess of putting useless markup on the wire and then conducting massive repairs to it in the browser's DOM, all while a rendering engine tries to repaint each change.
Re:Simplify templating with node.js, jsdom 0.2.0 and weld by dmethvin on Saturday February 19, 2011 @11:00AM (#2239218)
Seems like the complexity of the client-side code comes from rendering the DOM tree based on the CSS and constraints of the drawing area, hardware, etc. So yeah, just manipulating the tree would seem to be pretty fast.

Of course, it's being compared to spitting out an arbitrary stream of characters to the client without the need to parse HTML on the server side, which is probably faster. Years of experience with the string rendering approach tells us it's definitely more error prone. For example it's impossible to tell whether the character stream is well formed HTML without traversing all the code paths.

Re:Simplify templating with node.js, jsdom 0.2.0 and weld by hij1nx on Saturday February 19, 2011 @06:36PM (#2240291)
that's subjective (unless you can post tests). for example, one of the primary deficiencies of the dom is live node lists, which is an optimization option in jsdom. (also, we do create a fragment and then attach, as perf opts state)
Re:Simplify templating with node.js, jsdom 0.2.0 and weld by boucher on Saturday February 19, 2011 @01:17AM (#2238582)
I'd argue that this isn't really that dissimilar from most simple template engines. It's basically substituting class names for placeholder syntax, though with a bit less flexibility for things like repetitions or conditionals. Though I'd agree that I'd rather just use something like Mustache than this.
Re:Simplify templating with node.js, jsdom 0.2.0 and weld by webXL on Saturday February 19, 2011 @01:29AM (#2238603)
I think he just wants to separate the presentation from the logic, which might be good when you don't trust designers re-jiggling things, and you want clean code. Plus, if you're working with a lot of AJAX, it's nice to be able to reproduce the presentation on the server the same way you do it on the client. I suppose keeping the two in sync can be difficult, which is why you probably would only want to render on the server on the initial page request.
Re:Simplify templating with node.js, jsdom 0.2.0 and weld by tmpvar on Saturday February 19, 2011 @01:26AM (#2238594)
How do you rationalize including logic in the markup?
Re:Simplify templating with node.js, jsdom 0.2.0 and weld by simonw on Saturday February 19, 2011 @03:04AM (#2238710)
Presentation logic requires logic. If a designer decides to display a bunch of items as three columns rather than two columns, or to switch the display for a single item vs a number of different items, that's presentation logic. Ideally it should go in the template, if that's where you've chosen to keep it.

With Django, I often find myself writing view logic to support specific presentation tasks (regrouping items in a manner more complicated than Django's {% regroup %} template tag allows, for example). This used to bother me, but then I decided that pragmatism beats purity if you actually want to ship anything.

Re:Simplify templating with node.js, jsdom 0.2.0 and weld by reddittor on Saturday February 19, 2011 @04:45AM (#2238804)
I'd have to agree with you, for most traditional web applications.

When you start to build single page applications with lots and lots of states ( that are bound to a DOM ), this approach actually can become favorable since your "presentation logic" is browser-side JavaScript.

To my understanding, the server-side rendering aspect of this approach is really just an after-thought. You have your single page application, but you can still render a server-side snapshot of various UI states without requiring a browser.

While I wouldn't necessarily dive straight into DOM-based templating, I think there is a lot of room for interesting improvements in this area. I'm glad to see JSDOM maturing.