Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
19d712b4a4 | ||
|
|
201b8e263c | ||
|
|
03928721c4 | ||
|
|
07835063ed | ||
|
|
3c13d81b21 | ||
|
|
1668830d9b | ||
|
|
14195b3199 | ||
|
|
ae75ea5911 | ||
|
|
9c9ebbe559 | ||
|
|
5cdf1d326b | ||
|
|
f78562a1fd |
@@ -3,6 +3,6 @@ import PackageDescription
|
||||
let package = Package(
|
||||
name: "Stencil",
|
||||
dependencies: [
|
||||
.Package(url: "https://github.com/kylef/PathKit.git", majorVersion: 0, minor: 5),
|
||||
.Package(url: "https://github.com/kylef/PathKit.git", majorVersion: 0, minor: 6),
|
||||
]
|
||||
)
|
||||
|
||||
@@ -158,8 +158,15 @@ public class ForNode : NodeType {
|
||||
let values = try variable.resolve(context)
|
||||
|
||||
if let values = values as? [Any] where values.count > 0 {
|
||||
return try values.map { item in
|
||||
try context.push([loopVariable: item]) {
|
||||
let count = values.count
|
||||
return try values.enumerate().map { index, item in
|
||||
let forContext: [String: Any] = [
|
||||
"first": index == 0,
|
||||
"last": index == (count - 1),
|
||||
"counter": index + 1,
|
||||
]
|
||||
|
||||
return try context.push([loopVariable: item, "forloop": forContext]) {
|
||||
try renderNodes(nodes, context)
|
||||
}
|
||||
}.joinWithSeparator("")
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
public func until(tags:[String])(parser:TokenParser, token:Token) -> Bool {
|
||||
if let name = token.components().first {
|
||||
for tag in tags {
|
||||
if name == tag {
|
||||
return true
|
||||
public func until(tags: [String]) -> ((TokenParser, Token) -> Bool) {
|
||||
return { parser, token in
|
||||
if let name = token.components().first {
|
||||
for tag in tags {
|
||||
if name == tag {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
public typealias Filter = Any? throws -> Any?
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "Stencil",
|
||||
"version": "0.4.0",
|
||||
"version": "0.5.2",
|
||||
"summary": "Stencil is a simple and powerful template language for Swift.",
|
||||
"homepage": "https://github.com/kylef/Stencil",
|
||||
"license": {
|
||||
@@ -8,12 +8,12 @@
|
||||
"file": "LICENSE"
|
||||
},
|
||||
"authors": {
|
||||
"Kyle Fuller": "inbox@kylefuller.co.uk"
|
||||
"Kyle Fuller": "kyle@fuller.li"
|
||||
},
|
||||
"social_media_url": "http://twitter.com/kylefuller",
|
||||
"source": {
|
||||
"git": "https://github.com/kylef/Stencil.git",
|
||||
"tag": "0.4.0"
|
||||
"tag": "0.5.2"
|
||||
},
|
||||
"source_files": [
|
||||
"Sources/*.swift"
|
||||
@@ -24,7 +24,7 @@
|
||||
},
|
||||
"requires_arc": true,
|
||||
"dependencies": {
|
||||
"PathKit": [ "~> 0.5.0" ]
|
||||
"PathKit": [ "~> 0.6.0" ]
|
||||
},
|
||||
"test_specification": {
|
||||
"source_files": [
|
||||
@@ -34,7 +34,7 @@
|
||||
],
|
||||
"dependencies": {
|
||||
"Spectre": [ "~> 0.5.0" ],
|
||||
"PathKit": [ "~> 0.5.0" ]
|
||||
"PathKit": [ "~> 0.6.0" ]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,4 +41,22 @@ describe("ForNode") {
|
||||
let node = ForNode(variable: "items", loopVariable: "item", nodes: nodes, emptyNodes: [])
|
||||
try expect(try node.render(nsarray_context)) == "123"
|
||||
}
|
||||
|
||||
$0.it("renders the given nodes while providing if the item is first in the context") {
|
||||
let nodes: [NodeType] = [VariableNode(variable: "item"), VariableNode(variable: "forloop.first")]
|
||||
let node = ForNode(variable: "items", loopVariable: "item", nodes: nodes, emptyNodes: [])
|
||||
try expect(try node.render(context)) == "1true2false3false"
|
||||
}
|
||||
|
||||
$0.it("renders the given nodes while providing if the item is last in the context") {
|
||||
let nodes: [NodeType] = [VariableNode(variable: "item"), VariableNode(variable: "forloop.last")]
|
||||
let node = ForNode(variable: "items", loopVariable: "item", nodes: nodes, emptyNodes: [])
|
||||
try expect(try node.render(context)) == "1false2false3true"
|
||||
}
|
||||
|
||||
$0.it("renders the given nodes while providing item counter") {
|
||||
let nodes: [NodeType] = [VariableNode(variable: "item"), VariableNode(variable: "forloop.counter")]
|
||||
let node = ForNode(variable: "items", loopVariable: "item", nodes: nodes, emptyNodes: [])
|
||||
try expect(try node.render(context)) == "112233"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user