Skip to content

Commit

Permalink
Merge e98933d into 2d8cdab
Browse files Browse the repository at this point in the history
  • Loading branch information
tbranyen committed May 23, 2014
2 parents 2d8cdab + e98933d commit 87634ab
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,19 @@ var output = tmpl.render(context);
/// output == "1 2 3 4 "
```

#### Iterating an array of objects shorthand. ####

If you pass an array of objects to Combyne, you may iterate it via a shorthand:

``` javascript
var template = "{%each%}{{foo}} {%endeach%}";
var context = [{ foo: 1 }, { foo: 2 }, { foo: 3 }, { foo: 4 }];

var tmpl = combyne(template);

var output = tmpl.render(context);
/// output == "1 2 3 4 "

#### Change the iterated identifer within loops. ####

``` javascript
Expand Down
15 changes: 11 additions & 4 deletions lib/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,22 +263,29 @@ define(function(require, exports, module) {
* @return {string} The compiled JavaScript source string value.
*/
Compiler.prototype.compileLoop = function(node) {
var conditions = node.conditions || [];

var keyVal = [
// Key
(node.conditions[3] ? node.conditions[3].value : "i"),
(conditions[3] ? conditions[3].value : "i"),

// Value.
(node.conditions[2] ? node.conditions[2].value : ".")
(conditions[2] ? conditions[2].value : ".")
];

// Normalize the value to the condition if it exists.
var value = conditions.length && conditions[0].value;

// Construct the loop, utilizing map because it will return back the
// template as an array and ready to join into the template.
var loop = [
"map(", normalizeIdentifier(node.conditions[0].value), ",",
"map(", value ? normalizeIdentifier(value) : "data", ",",

// Index keyword.
"'", keyVal[0], "'", ",",

// Value keyword.
"'", keyVal[1], "'", ",",
"'", value ? keyVal[1] : "", "'", ",",

// Outer scope data object.
"data", ",",
Expand Down
18 changes: 16 additions & 2 deletions lib/shared/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,14 @@ define(function(require, exports, module) {
// Create a new scoped data object.
dataObject = createObject(data);
dataObject[index] = i;
dataObject[value] = obj[i];

if (value) {
dataObject[value] = obj[i];
}
// If no value was supplied, use this object to key off of.
else {
dataObject = obj[i];
}

// Add return value of iterator function to output.
output.push(iterator(dataObject));
Expand All @@ -59,7 +66,14 @@ define(function(require, exports, module) {
// Create a new scoped data object.
dataObject = createObject(data);
dataObject[index] = key;
dataObject[value] = obj[key];

if (value) {
dataObject[value] = obj[key];
}
// If no value was supplied, use this object to key off of.
else {
dataObject = obj[key];
}

// Add return value of iterator function to output.
output.push(iterator(dataObject));
Expand Down
7 changes: 7 additions & 0 deletions test/tests/expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,13 @@ define(function(require, exports, module) {

assert.equal(output, "value");
});

it("can scope lookups to context object", function() {
var tmpl = combyne("{%each%}{{key}}{%endeach%}");
var output = tmpl.render([{key:"value"}]);

assert.equal(output, "value");
});
});

describe("object loop", function() {
Expand Down

0 comments on commit 87634ab

Please sign in to comment.