Skip to content
This repository has been archived by the owner on Jun 10, 2018. It is now read-only.

Iterate over arrays #31

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 lib/compiler.js

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

6 changes: 6 additions & 0 deletions src/compiler.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ exports.precompile = precompile = (source) ->
"""
function(__obj) {
if (!__obj) __obj = {};
if (Object.prototype.toString.call(__obj) == '[object Array]') {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just check for __obj.length and __obj.join here instead?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely, that might be better. A bit crazy that typeof array is broken.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

var result = [];
for (var i = 0; i < __obj.length; i++)
result.push(arguments.callee.call(this, __obj[i]));
return result.join('');
}
var __out = [], __capture = function(callback) {
var out = __out, result;
__out = [];
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/project.eco
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<a href="<%= @url %>"><%= @name %></a>
<%- @description %>
6 changes: 6 additions & 0 deletions test/fixtures/project.out.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<a href="/projects/1">PowerTMS Active Shipments Page Redesign</a>

<a href="/projects/2">SCU Intranet</a>
<p><em>On hold</em></p>
<a href="/projects/3">Sales Template</a>

9 changes: 9 additions & 0 deletions test/test_eco.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ module.exports =
]
test.same fixture("projects.out.2"), output
test.done()

"rendering project.eco with an array": (test) ->
output = eco.render fixture("project.eco"), [
{ name: "PowerTMS Active Shipments Page Redesign", url: "/projects/1" },
{ name: "SCU Intranet", url: "/projects/2", description: "<p><em>On hold</em></p>" },
{ name: "Sales Template", url: "/projects/3" }
]
test.same fixture("project.out.1"), output
test.done()

"rendering helpers.eco": (test) ->
output = eco.render fixture("helpers.eco"),
Expand Down