JSFiddle - React, Tailwind, and code Playground
by TomXu
HTML
<!doctype html public 'ahh hell yeah'>
<html>
<head>
<meta charset=utf-8>
<title>Simple Templating</title>
</head>
<body>
<div class="result"></div>
<script type="template" id="template">
<h2>
<a href="{{href}}">
{{title}}
</a>
</h2>
<img src="{{imgSrc}}" alt="{{title}}">
</script>
</body>
</html>
CSS
a:link, a:visited {
color: #3D81EE;
}
JavaScript
;(function() {
// simulates AJAX request
var data = [
{
title: "Knockout应用开发指南",
href: "http://www.cnblogs.com/TomXu/archive/2011/11/21/2257154.html",
imgSrc: "http://images.cnblogs.com/cnblogs_com/TomXu/339203/o_knockout2.jpg"
},
{
title: "微软ASP.NET站点部署指南",
href: "http://www.cnblogs.com/TomXu/archive/2011/11/25/2263050.html",
imgSrc: "http://www.cnblogs.com/images/cnblogs_com/TomXu/339203/o_vs.jpg"
},
{
title: "HTML5学习笔记简明版",
href: "http://www.cnblogs.com/TomXu/archive/2011/12/06/2277499.html",
imgSrc: "http://images.cnblogs.com/cnblogs_com/TomXu/339203/o_LearningHtml5.png"
}
],
template = document.querySelector('#template').innerHTML,
result = document.querySelector('.result'),
i = 0, len = data.length,
fragment = '';
for ( ; i < len; i++ ) {
fragment += template
.replace( /\{\{title\}\}/, data[i].title )
.replace( /\{\{href\}\}/, data[i].href )
.replace( /\{\{imgSrc\}\}/, data[i].imgSrc );
}
result.innerHTML = fragment;
})();