Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

adding tests/examples

  • Loading branch information...
commit a625abf59e57299c62d71c6ce1c3d007268586a5 1 parent 62cd79c
@addyosmani authored
Showing with 71 additions and 4 deletions.
  1. +4 −4 microtemplatez.js
  2. +67 −0 test.html
View
8 microtemplatez.js
@@ -6,13 +6,13 @@
var templatez = (function(){
return function(tmpl, data){
return tmpl.replace((RegExp("{{\\s*([a-z0-9_][\\.a-z0-9_]*)\\s*}}", "gi")), function(tag, k){
- var path = k.split("."),
- len = path.length,
+ var p = k.split("."),
+ len = p.length,
temp = data,
i = 0;
for(i=0; i<len; i++){
- temp = temp[path[i]];
- if (i === len - 1){
+ temp = temp[p[i]];
+ if (i === (len - 1)){
return temp;
}
}
View
67 test.html
@@ -0,0 +1,67 @@
+<html>
+<head></head>
+<body>
+<script type="text/javascript" src="microtemplatez.js"></script>
+
+
+<script type="text/micro" id="test">
+ <img src="{{avatar}}" title="{{username}}" alt="{{username}}"/>
+ <span>{{username}}</span>
+</script>
+
+<script type="text/javascript">
+
+/*test 1*/
+var markup1 = "Test 1: I am a {{user.age}} year old {{user.sex}} from {{country}}",
+ data1 = {
+ country: "Ireland",
+ user: {
+ age: 25,
+ name: "Addy",
+ sex: "male"
+ }
+ };
+
+alert(templatez(markup1, data1));
+
+
+
+/*test2*/
+var markup2 = document.getElementById("test").innerHTML,
+ data2 = {
+ username: "addyosmani",
+ avatar: "http://a0.twimg.com/profile_images/1256987680/addyosmaniicon_reasonably_small.jpg",
+ age: 25
+ };
+
+alert('Test 2:' + templatez(markup2, data2));
+
+
+
+
+/*test 3*/
+var markup3 = "<li><b>{{Name}}</b> ({{ReleaseYear}})</li>",
+ buffer = "";
+
+var movies = [
+{ Name: "The Red Violin", ReleaseYear: "1998" },
+{ Name: "Eyes Wide Shut", ReleaseYear: "1999" },
+{ Name: "The Inheritance", ReleaseYear: "1976" }
+];
+
+for(var i=0; i<movies.length; i++){
+ buffer += templatez(markup3, movies[i]);
+}
+alert('Test 3:' + buffer);
+
+
+
+
+
+
+
+</script>
+
+</body>
+</html>
+
Please sign in to comment.
Something went wrong with that request. Please try again.