End sections includes method
This commit is contained in:
@@ -9,6 +9,7 @@ extension HBMustacheTemplate {
|
|||||||
|
|
||||||
struct ParserState {
|
struct ParserState {
|
||||||
var sectionName: String?
|
var sectionName: String?
|
||||||
|
var sectionMethod: String?
|
||||||
var newLine: Bool
|
var newLine: Bool
|
||||||
var startDelimiter: String
|
var startDelimiter: String
|
||||||
var endDelimiter: String
|
var endDelimiter: String
|
||||||
@@ -20,9 +21,10 @@ extension HBMustacheTemplate {
|
|||||||
endDelimiter = "}}"
|
endDelimiter = "}}"
|
||||||
}
|
}
|
||||||
|
|
||||||
func withSectionName(_ name: String) -> ParserState {
|
func withSectionName(_ name: String, method: String? = nil) -> ParserState {
|
||||||
var newValue = self
|
var newValue = self
|
||||||
newValue.sectionName = name
|
newValue.sectionName = name
|
||||||
|
newValue.sectionMethod = method
|
||||||
return newValue
|
return newValue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,7 +90,7 @@ extension HBMustacheTemplate {
|
|||||||
tokens.append(.text(whiteSpaceBefore))
|
tokens.append(.text(whiteSpaceBefore))
|
||||||
whiteSpaceBefore = ""
|
whiteSpaceBefore = ""
|
||||||
}
|
}
|
||||||
let sectionTokens = try parse(&parser, state: state.withSectionName(name))
|
let sectionTokens = try parse(&parser, state: state.withSectionName(name, method: method))
|
||||||
tokens.append(.section(name: name, method: method, template: HBMustacheTemplate(sectionTokens)))
|
tokens.append(.section(name: name, method: method, template: HBMustacheTemplate(sectionTokens)))
|
||||||
|
|
||||||
case "^":
|
case "^":
|
||||||
@@ -101,14 +103,14 @@ extension HBMustacheTemplate {
|
|||||||
tokens.append(.text(whiteSpaceBefore))
|
tokens.append(.text(whiteSpaceBefore))
|
||||||
whiteSpaceBefore = ""
|
whiteSpaceBefore = ""
|
||||||
}
|
}
|
||||||
let sectionTokens = try parse(&parser, state: state.withSectionName(name))
|
let sectionTokens = try parse(&parser, state: state.withSectionName(name, method: method))
|
||||||
tokens.append(.invertedSection(name: name, method: method, template: HBMustacheTemplate(sectionTokens)))
|
tokens.append(.invertedSection(name: name, method: method, template: HBMustacheTemplate(sectionTokens)))
|
||||||
|
|
||||||
case "/":
|
case "/":
|
||||||
// end of section
|
// end of section
|
||||||
parser.unsafeAdvance()
|
parser.unsafeAdvance()
|
||||||
let (name, _) = try parseName(&parser, state: state)
|
let (name, method) = try parseName(&parser, state: state)
|
||||||
guard name == state.sectionName else {
|
guard name == state.sectionName, method == state.sectionMethod else {
|
||||||
throw Error.sectionCloseNameIncorrect
|
throw Error.sectionCloseNameIncorrect
|
||||||
}
|
}
|
||||||
if isStandalone(&parser, state: state) {
|
if isStandalone(&parser, state: state) {
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ final class MethodTests: XCTestCase {
|
|||||||
func testFirstLast() throws {
|
func testFirstLast() throws {
|
||||||
let template = try HBMustacheTemplate(string: """
|
let template = try HBMustacheTemplate(string: """
|
||||||
{{#repo}}
|
{{#repo}}
|
||||||
<b>{{#first()}}first: {{/}}{{#last()}}last: {{/}}{{ name }}</b>
|
<b>{{#first()}}first: {{/first()}}{{#last()}}last: {{/last()}}{{ name }}</b>
|
||||||
{{/repo}}
|
{{/repo}}
|
||||||
|
|
||||||
""")
|
""")
|
||||||
@@ -53,7 +53,7 @@ final class MethodTests: XCTestCase {
|
|||||||
func testIndex() throws {
|
func testIndex() throws {
|
||||||
let template = try HBMustacheTemplate(string: """
|
let template = try HBMustacheTemplate(string: """
|
||||||
{{#repo}}
|
{{#repo}}
|
||||||
<b>{{#index()}}{{plusone(.)}}{{/}}) {{ name }}</b>
|
<b>{{#index()}}{{plusone(.)}}{{/index()}}) {{ name }}</b>
|
||||||
{{/repo}}
|
{{/repo}}
|
||||||
|
|
||||||
""")
|
""")
|
||||||
@@ -69,7 +69,7 @@ final class MethodTests: XCTestCase {
|
|||||||
func testEvenOdd() throws {
|
func testEvenOdd() throws {
|
||||||
let template = try HBMustacheTemplate(string: """
|
let template = try HBMustacheTemplate(string: """
|
||||||
{{#repo}}
|
{{#repo}}
|
||||||
<b>{{index()}}) {{#even()}}even {{/}}{{#odd()}}odd {{/}}{{ name }}</b>
|
<b>{{index()}}) {{#even()}}even {{/even()}}{{#odd()}}odd {{/odd()}}{{ name }}</b>
|
||||||
{{/repo}}
|
{{/repo}}
|
||||||
|
|
||||||
""")
|
""")
|
||||||
@@ -86,7 +86,7 @@ final class MethodTests: XCTestCase {
|
|||||||
let template = try HBMustacheTemplate(string: """
|
let template = try HBMustacheTemplate(string: """
|
||||||
{{#reversed(repo)}}
|
{{#reversed(repo)}}
|
||||||
<b>{{ name }}</b>
|
<b>{{ name }}</b>
|
||||||
{{/repo}}
|
{{/reversed(repo)}}
|
||||||
|
|
||||||
""")
|
""")
|
||||||
let object: [String: Any] = ["repo": [["name": "resque"], ["name": "hub"], ["name": "rip"]]]
|
let object: [String: Any] = ["repo": [["name": "resque"], ["name": "hub"], ["name": "rip"]]]
|
||||||
@@ -117,7 +117,7 @@ final class MethodTests: XCTestCase {
|
|||||||
let template = try HBMustacheTemplate(string: """
|
let template = try HBMustacheTemplate(string: """
|
||||||
{{#sorted(repo)}}
|
{{#sorted(repo)}}
|
||||||
<b>{{ index() }}) {{ . }}</b>
|
<b>{{ index() }}) {{ . }}</b>
|
||||||
{{/repo}}
|
{{/sorted(repo)}}
|
||||||
""")
|
""")
|
||||||
let object: [String: Any] = ["repo": ["resque", "hub", "rip"]]
|
let object: [String: Any] = ["repo": ["resque", "hub", "rip"]]
|
||||||
XCTAssertEqual(template.render(object), """
|
XCTAssertEqual(template.render(object), """
|
||||||
@@ -130,7 +130,7 @@ final class MethodTests: XCTestCase {
|
|||||||
|
|
||||||
func testDictionaryEnumerated() throws {
|
func testDictionaryEnumerated() throws {
|
||||||
let template = try HBMustacheTemplate(string: """
|
let template = try HBMustacheTemplate(string: """
|
||||||
{{#enumerated(.)}}<b>{{ key }} = {{ value }}</b>{{/.}}
|
{{#enumerated(.)}}<b>{{ key }} = {{ value }}</b>{{/enumerated(.)}}
|
||||||
""")
|
""")
|
||||||
let object: [String: Any] = ["one": 1, "two": 2]
|
let object: [String: Any] = ["one": 1, "two": 2]
|
||||||
let result = template.render(object)
|
let result = template.render(object)
|
||||||
@@ -139,7 +139,7 @@ final class MethodTests: XCTestCase {
|
|||||||
|
|
||||||
func testDictionarySortedByKey() throws {
|
func testDictionarySortedByKey() throws {
|
||||||
let template = try HBMustacheTemplate(string: """
|
let template = try HBMustacheTemplate(string: """
|
||||||
{{#sorted(.)}}<b>{{ key }} = {{ value }}</b>{{/.}}
|
{{#sorted(.)}}<b>{{ key }} = {{ value }}</b>{{/sorted(.)}}
|
||||||
""")
|
""")
|
||||||
let object: [String: Any] = ["one": 1, "two": 2, "three": 3]
|
let object: [String: Any] = ["one": 1, "two": 2, "three": 3]
|
||||||
let result = template.render(object)
|
let result = template.render(object)
|
||||||
|
|||||||
Reference in New Issue
Block a user