Skip to content
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

Fixes #21. #22

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
29 changes: 28 additions & 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 iteration (see **Nested Loops** below): `{{@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 All @@ -21,6 +21,33 @@

For more advanced usage check the [`t_test.html`](https://github.com/jasonmoo/t.js/blob/master/t_test.html).

### Nested Loops

When looping over the `_val` variable of a deeply nested loop of loops like:

```
{{@_val}} 1
{{@_val}} 2
{{/@_val}} 3
{{/@_val}} 4
```

the parser cannot match the opening `@_val` with its corresponding `/@_val` picking the first end tag it finds,
that is, it matches `1` with `3` instead of `4`.

To tell them apart, the start and end tags can be followed by a space and a label:

```
{{@_val outer}}
{{@_val inner}}
{{/@_val inner}}
{{/@_val outer}}
```

See this [Github issue](https://github.com/jasonmoo/t.js/issues/21) for more details.

---

This software is released under the MIT license.

___
Expand Down
2 changes: 1 addition & 1 deletion t.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
(function() {

var blockregex = /\{\{(([@!]?)(.+?))\}\}(([\s\S]+?)(\{\{:\1\}\}([\s\S]+?))?)\{\{\/\1\}\}/g,
var blockregex = /\{\{(([@!]?)([\.\w]+)[\s\S]*?)\}\}(([\s\S]+?)(\{\{:\1\}\}([\s\S]+?))?)\{\{\/\1\}\}/g,
valregex = /\{\{([=%])(.+?)\}\}/g;

function t(template) {
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.

32 changes: 29 additions & 3 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 @@ -64,7 +64,17 @@ <h4>Test Values</h4>

{{/@test_values}}


<ul id="nested_loops">{{@nested_loops}}
<li>{{=_key}}
<ul>{{@_val year}}
<li>{{=_key}}
<ul>{{@_val month}}
<li>{{=_val}}</li>
{{/@_val month}}</ul>
</li>
{{/@_val year}}</ul>
</li>
{{/@nested_loops}}</ul>
</script>
<script>

Expand Down Expand Up @@ -95,7 +105,17 @@ <h4>Test Values</h4>
"zero": 0,
"string_zero": "0",
"null": null
}
},
nested_loops: {
'2016': {
'03': [111, 112, 113],
'12': [121, 122, 123]
},
'2018': {
'05': [211, 212, 213],
'09': [221, 222, 223]
},
},
});
document.body.appendChild(div);
console.log(div.innerHTML);
Expand All @@ -118,6 +138,12 @@ <h4>Test Values</h4>
equal(document.getElementById("null").innerHTML, "", "Does not print null");

equal((new t("{{%quot}}")).render({quot: '"'}), "&quot;", "Escapes quotes properly")
equal(
document.getElementById('nested_loops').innerHTML.replaceAll(/\s/g,''),
`<li>2016<ul><li>12<ul><li>121</li><li>122</li><li>123</li></ul></li><li>03<ul><li>111</li><li>112</li><li>113</li></ul></li></ul></li><li>2018<ul><li>05<ul><li>211</li><li>212</li><li>213</li></ul></li><li>09<ul><li>221</li><li>222</li><li>223</li></ul></li></ul></li>`,
"Handles nested loops properly"
);


});

Expand Down