From bdc14ab1e121fdae32aac82a156db327b3bc7529 Mon Sep 17 00:00:00 2001 From: shnhrrsn Date: Sat, 5 Mar 2016 23:49:55 -0500 Subject: [PATCH] Added namespace to Context --- Sources/Context.swift | 16 +++++++++------- Sources/Template.swift | 7 ++++--- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/Sources/Context.swift b/Sources/Context.swift index 131b619..65fad03 100644 --- a/Sources/Context.swift +++ b/Sources/Context.swift @@ -1,15 +1,17 @@ /// A container for template variables. public class Context { var dictionaries: [[String: Any]] + let namespace: Namespace - /// Initialise a Context with a dictionary - public init(dictionary: [String: Any]) { - dictionaries = [dictionary] - } + /// Initialise a Context with an optional dictionary and optional namespace + public init(dictionary: [String: Any]? = nil, namespace: Namespace = Namespace()) { + if let dictionary = dictionary { + dictionaries = [dictionary] + } else { + dictionaries = [] + } - /// Initialise an empty Context - public init() { - dictionaries = [] + self.namespace = namespace } public subscript(key: String) -> Any? { diff --git a/Sources/Template.swift b/Sources/Template.swift index 86893d2..367770f 100644 --- a/Sources/Template.swift +++ b/Sources/Template.swift @@ -36,9 +36,10 @@ public class Template { } /// Render the given template - public func render(context: Context? = nil, namespace: Namespace? = nil) throws -> String { - let parser = TokenParser(tokens: tokens, namespace: namespace ?? Namespace()) + public func render(context: Context? = nil) throws -> String { + let context = context ?? Context() + let parser = TokenParser(tokens: tokens, namespace: context.namespace) let nodes = try parser.parse() - return try renderNodes(nodes, context ?? Context()) + return try renderNodes(nodes, context) } }