Created
August 12, 2015 14:13
-
-
Save allenhwkim/d1a714d01dfa833cbb75 to your computer and use it in GitHub Desktop.
micro template test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var microTemplate = require(__dirname + '/micro-template.js'); | |
/******************************************************* | |
* `<%= %>` expression test | |
*******************************************************/ | |
var layout = [ | |
'<script type="text/html" id="item_tmpl">', | |
' <div id="<%=id%>" class="<%=(i % 2 == 1 ? " even" : "")%>">', | |
' <div class="grid_1 alpha right">', | |
' <img class="righted" src="<%=profile_image_url%>"/>', | |
' </div>', | |
' <div class="grid_6 omega contents">', | |
' <p><b><a href="/<%=from_user%>"><%=from_user%></a>:</b> <%=text%></p>', | |
' </div>', | |
' </div>', | |
'</script>' | |
].join("\n"); | |
var data = { //jshint ignore:line | |
id: 'myid', | |
i: 11, | |
profile_image_url: 'myurl', //jshint ignore:line | |
from_user: 'me', //jshint ignore:line | |
text: 'me text' | |
}; | |
var expectedOutput = [ | |
'<script type="text/html" id="item_tmpl">', | |
' <div id="myid" class=" even">', | |
' <div class="grid_1 alpha right">', | |
' <img class="righted" src="myurl"/>', | |
' </div>', | |
' <div class="grid_6 omega contents">', | |
' <p><b><a href="/me">me</a>:</b> me text</p>', | |
' </div>', | |
' </div>', | |
'</script>', | |
].join("\n"); | |
var output = microTemplate(layout, data); | |
if (output !== expectedOutput) {throw "error 1";} | |
/******************************************************* | |
* native javascript test. e.g., for loop | |
*******************************************************/ | |
var layout = [ //jshint ignore:line | |
'<script type="text/html" id="user_tmpl">', | |
'<% for ( var i = 0; i < users.length; i++ ) { %>', | |
' <li><a href="<%=users[i].url%>"><%=users[i].name%></a></li>', | |
'<% } %>', | |
'</script>' | |
].join("\n"); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment