Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fix 'this' wrong usage (when var 'template' not created), make loop i…

…ndexes local; create test page; tested in http://jsfiddle.net/alexbaumgertner/UXZDy/91/
  • Loading branch information...
commit 6a0fc8f4266caae4e2c215b84325f4745e9cd1f0 1 parent 1df4fd1
@alexbaumgertner alexbaumgertner authored
Showing with 41 additions and 2 deletions.
  1. +1 −0  .gitignore
  2. +2 −2 nano.js
  3. +2 −0  readme.md
  4. +36 −0 testPage.html
View
1  .gitignore
@@ -0,0 +1 @@
+.idea
View
4 nano.js
@@ -3,7 +3,7 @@
function nano(template, data) {
return template.replace(/\{([\w\.]*)\}/g, function(str, key) {
var keys = key.split("."), v = data[keys.shift()];
- for (i = 0, l = keys.length; i < l; i++) v = v[this];
+ for (var i = 0, l = keys.length; i < l; i++) v = v[keys[i]];
return (typeof v !== "undefined" && v !== null) ? v : "";
});
-};
+}
View
2  readme.md
@@ -31,6 +31,8 @@ and you get ready string:
&lt;p&gt;Hello Thomas! Your account is &lt;strong&gt;active&lt;/strong&gt;&lt;/p&gt;
</code>
+Test page: <a href="testPage.html">testPage.html</a>
+
Simple huh?
***More Advanced Example***
View
36 testPage.html
@@ -0,0 +1,36 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <title>Test page for nano</title>
+ <!-- include lib -->
+ <script type="text/javascript" src="nano.js"></script>
+
+ <!-- test data -->
+ <script type="text/javascript">
+ data = {
+ user: {
+ login: "tomek",
+ first_name: "Thomas",
+ last_name: "Mazur",
+ account: {
+ status: "active",
+ expires_at: "2009-12-31"
+ }
+ }
+ }
+ </script>
+
+</head>
+<body>
+
+
+<p id="testLayout">nano("&lt;p&gt;Hello {user.first_name} {user.last_name}! Your account&nbsp;is &lt;strong&gt;{user.account.status}&lt;/strong&gt;&lt;/p&gt;", data)</p>
+
+
+<script type="text/javascript">
+ window.onload = function () {
+ document.getElementById('testLayout').innerHTML = nano("<p>Hello {user.first_name} {user.last_name}! Your account is <strong>{user.account.status}</strong></p>", data);
+ }
+</script>
+</body>
+</html>
Please sign in to comment.
Something went wrong with that request. Please try again.