Skip to content

Instantly share code, notes, and snippets.

@killercup
Last active November 4, 2016 10:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save killercup/7c53829d216c276ccf1a886cd07d9330 to your computer and use it in GitHub Desktop.
Save killercup/7c53829d216c276ccf1a886cd07d9330 to your computer and use it in GitHub Desktop.
/// PAascal's LIttle TEmplate THingy
///
/// a.k.a. that thing I threw together in 10min
define("paliteth", [], function () {
function notNull(val) {
return !!val;
}
function isArray(vArg) {
return Object.prototype.toString.call(vArg) === "[object Array]";
};
/**
* Create dom element representation
*
* @param {string} name
* @param {Object} attrs
* @param {Object[]} children
* @returns {Object}
*/
function element(name, attrs, children) {
return {
name: name,
attributes: attrs,
children: children
};
}
/**
* Render element attributes
*
* @param {Object} attrs
* @returns {string}
*/
function attrsToHtml(attrs) {
return Object.keys(attrs).filter(notNull).map(function (key) {
var value = attrs[key];
return key + '="' + value + '"';
}).join(" ");
}
/**
* Render element to string
*
* @param {Object} el
* @returns {string}
*/
function toHtml(el) {
if (typeof el === 'string') { return el; }
if (isArray(el)) { return el.map(toHtml).join('\n'); }
var attrs = el.attributes ? " " + attrsToHtml(el.attributes) : "";
var children;
if (typeof el.children === 'string') {
children = el.children;
} else {
children = (el.children || []).filter(notNull).map(toHtml).join('\n');
}
return "<" + el.name + attrs + ">" +
children +
"</" + el.name + ">";
}
return {
element: element,
toHtml: toHtml,
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment