Merge pull request #59 from shnhrrsn/namespace-fix
Added namespace to Context
This commit is contained in:
@@ -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? {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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") {
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user