Skip to content

Instantly share code, notes, and snippets.

@subtleGradient
Created January 15, 2010 12:34
Show Gist options
  • Save subtleGradient/278016 to your computer and use it in GitHub Desktop.
Save subtleGradient/278016 to your computer and use it in GitHub Desktop.
BuildSugar—Provides a bizarrely clean & simple syntax sugar for building HTML/XML strings
/*
---
provides : BuildSugar
source : http://gist.github.com/278016
copyright : 2010 Thomas Aylott
license : MIT
...
*/
var BS=function(c){function f(a,b){c.push([]);return d(a,b)}function d(a,b){b=""+(b||"");a&&e("<",a,">");b&&e(b);a&&e("</",a.split(" ")[0],">");return d}d.toString=function(){return c.pop().join("")};function e(){c[c.length-1].push(c.join.call(arguments,""))}return f}([]);
/*
---
provides : BuildSugar
version : 0.1alpha1
description : Provides a bizarrely clean & simple syntax sugar for building HTML/XML strings
source : http://gist.github.com/278016
git : git://gist.github.com/278016.git
demo : http://jsfiddle.net/SubtleGradient/4W3RR/
author : Thomas Aylott
site : subtlegradient.com
copyright : 2010 Thomas Aylott
license : MIT
...
*/
var BS = (function (bs) {
function BS ( selector,content ) {
bs .push ([])
return BS2 ( selector,content )
}
function BS2 ( selector,content ) {
content = ''+ ( content||'' )
selector && write ( '<',selector,'>' )
content && write ( content )
selector && write ( '</',selector .split (' ')[0],'>' )
return BS2
}
BS2 .toString = function () { return bs .pop () .join ('') }
function write () { bs[bs.length-1] .push (bs .join .call ( arguments,'' )) }
return BS
}) ([])
document
.write
(BS
('H1' ,"Battlestar Galactica DVDs")
('P' ,"Best. Show. Evar. ")
('A href=#' ,BS ('IMG src="http://bit.ly/WinAylott"') ('BR')
('' ,"View larger image") )
('FORM method=GET' ,BS
('LABEL for=quantity' ,"Quantity")
('INPUT type=text name=quantity id=quantity value=1')
('INPUT type=submit value="Add to Cart"')
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment