Skip to content
This repository
Browse code

Change `forloop` vars to `loop`

closes gh-47
  • Loading branch information...
commit c57c65eace3f80c3f54011faa49bea590f3eddde 1 parent 3d5edd8
Paul Armstrong authored
2  README.md
Source Rendered
@@ -33,7 +33,7 @@ Basic Example
33 33 <h1>{{ pagename|title }}</h1>
34 34 <ul>
35 35 {% for author in authors %}
36   - <li{% if forloop.index <= 0 %} class="first"{% endif %}>{{ author }}</li>
  36 + <li{% if loop.index <= 0 %} class="first"{% endif %}>{{ author }}</li>
37 37 {% empty %}
38 38 <li>There are no authors.</li>
39 39 {% endfor %}
14 docs/tags.md
Source Rendered
@@ -80,15 +80,15 @@ Wrap any section in `{% raw %}...{% endraw %}` to stop the token parser from mod
80 80 For loops have 4 special context variables accessible inside of the loop:
81 81
82 82 {% for x in y %}
83   - {% if forloop.first %}<ul>{% endif %}
84   - <li>{{ forloop.index }} - {{ forloop.key }}: {{ x }}</li>
85   - {% if forloop.last %}</ul>{% endif %}
  83 + {% if loop.first %}<ul>{% endif %}
  84 + <li>{{ loop.index }} - {{ loop.key }}: {{ x }}</li>
  85 + {% if loop.last %}</ul>{% endif %}
86 86 {% endfor %}
87 87
88   -* `forloop.index`: the zero-indexed spot in the iterator.
89   -* `forloop.key`: if the iterator is an object, this will be the key of the current item, otherwise it will be the same as the `forloop.index`.
90   -* `forloop.first`: `true` if the current object is the first in the object or array.
91   -* `forloop.last`: `true` if the current object is the last in the object or array.
  88 +* `loop.index`: the zero-indexed spot in the iterator.
  89 +* `loop.key`: if the iterator is an object, this will be the key of the current item, otherwise it will be the same as the `loop.index`.
  90 +* `loop.first`: `true` if the current object is the first in the object or array.
  91 +* `loop.last`: `true` if the current object is the last in the object or array.
92 92
93 93 You can also apply filters to the object that you are iterating over.
94 94
2  examples/express/views/people.html
@@ -5,7 +5,7 @@
5 5 {% block body %}
6 6 <ul>
7 7 {% for person in people %}
8   - <li><a href="/people/{{ forloop.index }}">{{ person.name }}</a> age {{ person.age }}</li>
  8 + <li><a href="/people/{{ loop.index }}">{{ person.name }}</a> age {{ person.age }}</li>
9 9 {% endfor %}
10 10 </ul>
11 11 {% endblock %}
34 lib/tags.js
@@ -242,32 +242,32 @@ exports['for'] = function (indent, parentBlock) {
242 242
243 243 operand1 = helpers.escapeVarName(operand1);
244 244
245   - loopShared = 'forloop.index = __forloopIndex;\n' +
246   - 'forloop.first = (__forloopIndex === 0);\n' +
247   - 'forloop.last = (__forloopIndex === __forloopLength - 1);\n' +
248   - '_context["' + operand1 + '"] = __forloopIter[forloop.key];\n' +
  245 + loopShared = 'loop.index = __loopIndex;\n' +
  246 + 'loop.first = (__loopIndex === 0);\n' +
  247 + 'loop.last = (__loopIndex === __loopLength - 1);\n' +
  248 + '_context["' + operand1 + '"] = __loopIter[loop.key];\n' +
249 249 parser.compile.apply(this, [indent + ' ', parentBlock]);
250 250
251 251 out = '(function () {\n' +
252   - ' var forloop = {}, __forloopKey, __forloopIndex = 0, __forloopLength = 0,' +
  252 + ' var loop = {}, __loopKey, __loopIndex = 0, __loopLength = 0,' +
253 253 '__origOperand1Value = _context["' + operand1 + '"];\n' +
254   - helpers.setVar('__forloopIter', operand2) +
  254 + helpers.setVar('__loopIter', operand2) +
255 255 ' else {\n' +
256 256 ' return;\n' +
257 257 ' }\n' +
258 258 // Basic for loops are MUCH faster than for...in. Prefer this arrays.
259   - ' if (_.isArray(__forloopIter)) {\n' +
260   - ' __forloopIndex = 0; __forloopLength = __forloopIter.length;\n' +
261   - ' for (; __forloopIndex < __forloopLength; __forloopIndex += 1) {\n' +
262   - ' forloop.key = __forloopIndex;\n' +
  259 + ' if (_.isArray(__loopIter)) {\n' +
  260 + ' __loopIndex = 0; __loopLength = __loopIter.length;\n' +
  261 + ' for (; __loopIndex < __loopLength; __loopIndex += 1) {\n' +
  262 + ' loop.key = __loopIndex;\n' +
263 263 loopShared +
264 264 ' }\n' +
265   - ' } else if (typeof __forloopIter === "object") {\n' +
266   - ' __keys = _.keys(__forloopIter);\n' +
267   - ' __forloopLength = __keys.length;\n' +
268   - ' __forloopIndex = 0;\n' +
269   - ' for (; __forloopIndex < __forloopLength; __forloopIndex += 1) {\n' +
270   - ' forloop.key = __keys[__forloopIndex];\n' +
  265 + ' } else if (typeof __loopIter === "object") {\n' +
  266 + ' __keys = _.keys(__loopIter);\n' +
  267 + ' __loopLength = __keys.length;\n' +
  268 + ' __loopIndex = 0;\n' +
  269 + ' for (; __loopIndex < __loopLength; __loopIndex += 1) {\n' +
  270 + ' loop.key = __keys[__loopIndex];\n' +
271 271 loopShared +
272 272 ' }\n' +
273 273 ' }\n' +
@@ -283,7 +283,7 @@ exports.empty = function (indent) {
283 283 throw new Error('Cannot call "empty" tag outside of "for" context.');
284 284 }
285 285
286   - return '} if (_.keys(__forloopIter).length === 0) {\n';
  286 + return '} if (_.keys(__loopIter).length === 0) {\n';
287 287 };
288 288
289 289 /**
6 tests/speed.js
@@ -14,7 +14,7 @@ swig.init({
14 14 root: __dirname + '/templates'
15 15 });
16 16
17   -tplString = '{% for foo in bar %}{{ forloop.index }} - {{ foo }}{% endfor %}';
  17 +tplString = '{% for foo in bar %}{{ loop.index }} - {{ foo }}{% endfor %}';
18 18 tplF = swig.compile(tplString);
19 19 console.time('object loop');
20 20 i = 10000;
@@ -40,8 +40,8 @@ console.log(" ~ " + Math.round(1000000 / (new Date() - d)) + " renders per se
40 40 tplString = "{% for v in array %}" +
41 41 "{% if 1 %}" +
42 42 "{% for k in v %}" +
43   - "\n{{forloop.index}} {{k}}: " +
44   - "{% if forloop.index in 'msafas' %}" +
  43 + "\n{{loop.index}} {{k}}: " +
  44 + "{% if loop.index in 'msafas' %}" +
45 45 "<p>Hello World {{k}}{{foo}}{{k}}{{foo}}{{k}}{{foo}}</p>" +
46 46 "{% endif %}" +
47 47 "{% endfor %}" +
12 tests/tags.test.js
@@ -287,7 +287,7 @@ exports['for'] = testCase({
287 287 },
288 288
289 289 variables: function (test) {
290   - var tpl = swig.compile('{% for foo in bar %}[{{ forloop.index }}, {{ forloop.key }}]{% endfor %}');
  290 + var tpl = swig.compile('{% for foo in bar %}[{{ loop.index }}, {{ loop.key }}]{% endfor %}');
291 291 test.strictEqual(tpl({ bar: ['foo', 'bar', 'baz'] }), '[0, 0][1, 1][2, 2]', 'array loop');
292 292 test.strictEqual(tpl({ bar: { baz: 'foo', pow: 'bar', foo: 'baz' }}), '[0, baz][1, pow][2, foo]', 'object loop');
293 293 test.done();
@@ -302,21 +302,21 @@ exports['for'] = testCase({
302 302 },
303 303
304 304 index: function (test) {
305   - var tpl = swig.compile('{% for foo in bar %}{{ forloop.index }}{% endfor %}');
  305 + var tpl = swig.compile('{% for foo in bar %}{{ loop.index }}{% endfor %}');
306 306 test.strictEqual(tpl({ bar: ['foo', 'bar', 'baz'] }), '012', 'index in object');
307 307 test.strictEqual(tpl({ bar: { baz: 'foo', pow: 'bar', foo: 'baz' }}), '012', 'index in object');
308 308 test.done();
309 309 },
310 310
311 311 first: function (test) {
312   - var tpl = swig.compile('{% for foo in bar %}{% if forloop.first %}{{ foo }}{% endif %}{% endfor %}');
  312 + var tpl = swig.compile('{% for foo in bar %}{% if loop.first %}{{ foo }}{% endif %}{% endfor %}');
313 313 test.strictEqual(tpl({ bar: ['foo', 'bar', 'baz'] }), 'foo', 'first in array');
314 314 test.strictEqual(tpl({ bar: { baz: 'foo', pow: 'bar', foo: 'baz' }}), 'foo', 'first in object');
315 315 test.done();
316 316 },
317 317
318 318 last: function (test) {
319   - var tpl = swig.compile('{% for foo in bar %}{% if forloop.last %}{{ foo }}{% endif %}{% endfor %}');
  319 + var tpl = swig.compile('{% for foo in bar %}{% if loop.last %}{{ foo }}{% endif %}{% endfor %}');
320 320 test.strictEqual(tpl({ bar: ['foo', 'bar', 'baz'] }), 'baz', 'last in array');
321 321 test.strictEqual(tpl({ bar: { baz: 'foo', pow: 'bar', foo: 'baz' }}), 'baz', 'last in object');
322 322 test.done();
@@ -342,8 +342,8 @@ exports['for'] = testCase({
342 342 test.done();
343 343 },
344 344
345   - 'use forloop.index in var': function (test) {
346   - var tpl = swig.compile('{% for key in bar %}{{ foo[forloop.index] }} {% endfor %}');
  345 + 'use loop.index in var': function (test) {
  346 + var tpl = swig.compile('{% for key in bar %}{{ foo[loop.index] }} {% endfor %}');
347 347 test.strictEqual(tpl({ bar: ['a', 'b', 'c'], foo: ['hi', 'and', 'bye' ]}), 'hi and bye ');
348 348 test.done();
349 349 }
2  tests/templates/included.html
@@ -2,7 +2,7 @@
2 2 {% if 1 %}
3 3 {% for k in v %}
4 4 <p>{{k}}: {{v}}</p>
5   - {% if forloop.index === "af" %}
  5 + {% if loop.index === "af" %}
6 6 <p>foo: {{foo}}</p>
7 7 <p>Hello World {{k}} {{foo}}</p>
8 8 <p>array.length: {% include "included_2.html" %}</p>

0 comments on commit c57c65e

Please sign in to comment.
Something went wrong with that request. Please try again.