feat: Allow subclassing templates (#79)

This commit is contained in:
Kyle Fuller
2016-12-07 21:46:04 +00:00
committed by GitHub
parent d7b152089e
commit 26f30cbd9d
5 changed files with 28 additions and 8 deletions

View File

@@ -25,11 +25,17 @@ func testEnvironment() {
let result = try environment.renderTemplate(name: "example.html")
try expect(result) == "Hello World!"
}
$0.it("allows you to provide a custom template class") {
let environment = Environment(loader: ExampleLoader(), templateClass: CustomTemplate.self)
let result = try environment.renderTemplate(string: "Hello World")
try expect(result) == "here"
}
}
}
fileprivate class ExampleLoader: Loader {
func loadTemplate(name: String, environment: Environment) throws -> Template {
if name == "example.html" {
@@ -39,3 +45,10 @@ fileprivate class ExampleLoader: Loader {
throw TemplateDoesNotExist(templateNames: [name], loader: self)
}
}
class CustomTemplate: Template {
override func render(_ dictionary: [String: Any]? = nil) throws -> String {
return "here"
}
}