Skip to content

Added ability to loop through Map instances (and other iterables) #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Name-spaced variables: `{{=User.address.city}}`
* If/else blocks: `{{value}} <<markup>> {{:value}} <<alternate markup>> {{/value}}`
* If not blocks: `{{!value}} <<markup>> {{/!value}}`
* Object/Array iteration: `{{@object_value}} {{=_key}}:{{=_val}} {{/@object_value}}`
* Object/Array/Map (any *Iterator*) iteration: `{{@object_value}} {{=_key}}:{{=_val}} {{/@object_value}}`
* Multi-line templates (no removal of newlines required to render)
* Render the same template multiple times with different data
* Works in all modern browsers
Expand Down
42 changes: 18 additions & 24 deletions t.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@
}

function get_value(vars, key) {
var parts = key.split('.');
while (parts.length) {
if (!(parts[0] in vars)) {
return false;
}
vars = vars[parts.shift()];
}
return vars;
return key
.split('.')
.reduce(
(out, part) => (out instanceof Map ? out.get(part) : out[part]) ?? false,
vars
);
}

function render(fragment, vars) {
Expand Down Expand Up @@ -67,22 +65,18 @@

// process array/obj iteration
if (meta == '@') {
// store any previous vars
// reuse existing vars
_ = vars._key;
__ = vars._val;
for (i in val) {
if (val.hasOwnProperty(i)) {
vars._key = i;
vars._val = val[i];
temp += render(inner, vars);
}
}
vars._key = _;
vars._val = __;
return temp;
}

return Array.from(
!!val?.[Symbol.iterator] ? val.entries() : Object.entries(val),
([_key, _val]) => render(
inner,
{
...vars,
_key,
_val,
}
)
).join('');
}
})
.replace(valregex, function(_, meta, key) {
var val = get_value(vars,key);
Expand Down
3 changes: 1 addition & 2 deletions t.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 13 additions & 4 deletions t_test.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<script src="http://code.jquery.com/qunit/qunit-1.9.0.js"></script>
<link rel="stylesheet" type="text/css" href="http://code.jquery.com/qunit/qunit-1.9.0.css">

<script src="t.min.js"></script>
<script src="t.js"></script>
<script type="t/template" id="test">

<h1>{{=greeting}}</h1>
Expand Down Expand Up @@ -63,8 +63,11 @@ <h4>Test Values</h4>
<p>{{=_key}}<span id="{{=_key}}" data-val="{{%_val}}">{{=_val}}</span></p>

{{/@test_values}}


<ul id="map">
{{@map}}
<li>{{=_key}}: {{=_val}}</li>
{{/@map}}
</ul>
</script>
<script>

Expand Down Expand Up @@ -95,7 +98,12 @@ <h4>Test Values</h4>
"zero": 0,
"string_zero": "0",
"null": null
}
},
map: new Map([
[1,'One'],
[2,'Two'],
[3, 'Three']
])
});
document.body.appendChild(div);
console.log(div.innerHTML);
Expand All @@ -117,6 +125,7 @@ <h4>Test Values</h4>
equal(document.getElementById("false").innerHTML, "", "Does not print boolean false");
equal(document.getElementById("null").innerHTML, "", "Does not print null");

equal(document.getElementById("map").innerHTML.replaceAll(/\s/g,''),"<li>1:One</li><li>2:Two</li><li>3:Three</li>", "Doesn't loop through Maps" )
equal((new t("{{%quot}}")).render({quot: '"'}), "&quot;", "Escapes quotes properly")

});
Expand Down