So at Esper we made this tool to make our lives easier.
var s = ' <div> <span class="example">Welcome!</span> </div> ';
var s = '<div>' +' <span class="example">Welcome!</span>' +'</div>';
This required more edits than we wanted, but why not. Now something we often do is modify child nodes based one some conditions. We use jQuery to produce a dom node from a string, maybe as follows:
var view = $( '<div>' +' <span class="example" id="msg"></span>' +'</div>'; ); var text = isNewUser(user) ? "Welcome!" : "Hello!"; $("#msg").text(text);
Huh. This assumes that no other element in the whole document may be identified by
id="msg". If not, the selection
var view = $('<div/>'); var msg = $('<span class="example"/>'); msg.appendTo(view); var text = isNewUser(user) ? "Welcome!" : "Hello!"; msg.text(text);
or maybe we will push jQuery to its limits and write the following:
var view = $('<div/>'); $('<span class="example"/>') .appendTo(view) .text(isNewUser(user) ? "Welcome!" : "Hello!");
Nice. No more global identifier. Does it look like HTML? No. Do you need to create something like a modal, with a complex structure and several placeholders for text or HTML elements? Still without using global
This is where we figured something should be done. We came up with
Our example written using oblivion syntax is:
Additionally, Oblivion automatically packs our variables (
msg) into a
_view object that can be easily passed to other functions. This means that in the example above,
msg are also available as
Oblivion parses the HTML template statically so if your HTML code contains a syntax error,
oblivion will indicate its exact position in the source file.
And really, all
oblivion does is produce this:
Note how the line count is the same in both the input and the output. If a runtime error is reported on line 8, we know that the error is on line 8 in the source code as well.
Oblivion is free software distributed under the terms of a BSD license. Enjoy!