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