From bdc14ab1e121fdae32aac82a156db327b3bc7529 Mon Sep 17 00:00:00 2001 From: shnhrrsn Date: Sat, 5 Mar 2016 23:49:55 -0500 Subject: [PATCH 1/2] 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) } } From aa1399be559292648cb4f45095b75578d2e23afa Mon Sep 17 00:00:00 2001 From: shnhrrsn Date: Sun, 6 Mar 2016 00:15:57 -0500 Subject: [PATCH 2/2] Fixed tests for namespace changes --- Tests/FilterSpec.swift | 6 +++--- Tests/StencilSpec.swift | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Tests/FilterSpec.swift b/Tests/FilterSpec.swift index 70d77e7..cef617f 100644 --- a/Tests/FilterSpec.swift +++ b/Tests/FilterSpec.swift @@ -4,7 +4,7 @@ import Stencil func testFilter() { describe("template filters") { - let context = Context(dictionary: ["name": "Kyle"]) + let context: [String: Any] = ["name": "Kyle"] $0.it("allows you to register a custom filter") { let template = Template(templateString: "{{ name|repeat }}") @@ -18,7 +18,7 @@ func testFilter() { return nil } - let result = try template.render(context, namespace: namespace) + let result = try template.render(Context(dictionary: context, namespace: namespace)) try expect(result) == "Kyle Kyle" } @@ -29,7 +29,7 @@ func testFilter() { throw TemplateSyntaxError("No Repeat") } - try expect(try template.render(context, namespace: namespace)).toThrow(TemplateSyntaxError("No Repeat")) + try expect(try template.render(Context(dictionary: context, namespace: namespace))).toThrow(TemplateSyntaxError("No Repeat")) } $0.it("allows whitespace in expression") { diff --git a/Tests/StencilSpec.swift b/Tests/StencilSpec.swift index 1434c82..9d2a166 100644 --- a/Tests/StencilSpec.swift +++ b/Tests/StencilSpec.swift @@ -46,7 +46,7 @@ func testStencil() { return CustomNode() } - let result = try template.render(namespace: namespace) + let result = try template.render(Context(namespace: namespace)) try expect(result) == "Hello World" } @@ -59,7 +59,7 @@ func testStencil() { return "Hello World" } - try expect(try template.render(namespace: namespace)) == "Hello World" + try expect(try template.render(Context(namespace: namespace))) == "Hello World" } } }