Allow Template.render() to be called multiple times

Allow Template.render() to be called multiple times, for the use case where a single template is rendered against multiple Contexts.
This commit is contained in:
Andy Choi
2015-09-30 20:56:54 -07:00
parent 2ab9b85305
commit b03ec50a42
2 changed files with 8 additions and 2 deletions

View File

@@ -4,6 +4,7 @@ import PathKit
/// A class representing a template
public class Template {
public let parser:TokenParser
private var nodes:[NodeType]? = nil
/// Create a template with the given name inside the main bundle
public convenience init(named:String) throws {
@@ -42,7 +43,10 @@ public class Template {
/// Render the given template
public func render(context:Context? = nil) throws -> String {
let nodes = try parser.parse()
return try renderNodes(nodes, context ?? Context())
if nodes == nil {
nodes = try parser.parse()
}
return try renderNodes(nodes!, context ?? Context())
}
}