[Template] Expose token parser
This commit is contained in:
@@ -8,6 +8,10 @@ public class Context : Equatable {
|
||||
dictionaries = [dictionary]
|
||||
}
|
||||
|
||||
public init() {
|
||||
dictionaries = []
|
||||
}
|
||||
|
||||
public subscript(key: String) -> AnyObject? {
|
||||
get {
|
||||
for dictionary in reverse(dictionaries) {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import Foundation
|
||||
|
||||
public class Template {
|
||||
let parser:TokenParser
|
||||
public let parser:TokenParser
|
||||
|
||||
public convenience init?(named:String) {
|
||||
self.init(named:named, inBundle:nil)
|
||||
@@ -45,4 +45,9 @@ public class Template {
|
||||
return .Error(error:error)
|
||||
}
|
||||
}
|
||||
|
||||
public func render() -> Result {
|
||||
let context = Context()
|
||||
return render(context)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,12 @@ func assertFailure(result:TokenParser.Results, description:String) {
|
||||
}
|
||||
}
|
||||
|
||||
class CustomNode : Node {
|
||||
func render(context:Context) -> (String?, Error?) {
|
||||
return ("Hello World", nil)
|
||||
}
|
||||
}
|
||||
|
||||
class StencilTests: XCTestCase {
|
||||
func testReadmeExample() {
|
||||
let templateString = "There are {{ articles.count }} articles.\n" +
|
||||
@@ -46,4 +52,16 @@ class StencilTests: XCTestCase {
|
||||
|
||||
XCTAssertEqual(result, Result.Success(string: fixture))
|
||||
}
|
||||
|
||||
func testCustomTag() {
|
||||
let templateString = "{% custom %}"
|
||||
let template = Template(templateString:templateString)
|
||||
|
||||
template.parser.registerTag("custom") { parser, token in
|
||||
return .Success(node:CustomNode())
|
||||
}
|
||||
|
||||
let result = template.render()
|
||||
XCTAssertEqual(result, Result.Success(string: "Hello World"))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user