Added namespace to Context
This commit is contained in:
@@ -1,15 +1,17 @@
|
|||||||
/// A container for template variables.
|
/// A container for template variables.
|
||||||
public class Context {
|
public class Context {
|
||||||
var dictionaries: [[String: Any]]
|
var dictionaries: [[String: Any]]
|
||||||
|
let namespace: Namespace
|
||||||
|
|
||||||
/// Initialise a Context with a dictionary
|
/// Initialise a Context with an optional dictionary and optional namespace
|
||||||
public init(dictionary: [String: Any]) {
|
public init(dictionary: [String: Any]? = nil, namespace: Namespace = Namespace()) {
|
||||||
dictionaries = [dictionary]
|
if let dictionary = dictionary {
|
||||||
}
|
dictionaries = [dictionary]
|
||||||
|
} else {
|
||||||
|
dictionaries = []
|
||||||
|
}
|
||||||
|
|
||||||
/// Initialise an empty Context
|
self.namespace = namespace
|
||||||
public init() {
|
|
||||||
dictionaries = []
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public subscript(key: String) -> Any? {
|
public subscript(key: String) -> Any? {
|
||||||
|
|||||||
@@ -36,9 +36,10 @@ public class Template {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Render the given template
|
/// Render the given template
|
||||||
public func render(context: Context? = nil, namespace: Namespace? = nil) throws -> String {
|
public func render(context: Context? = nil) throws -> String {
|
||||||
let parser = TokenParser(tokens: tokens, namespace: namespace ?? Namespace())
|
let context = context ?? Context()
|
||||||
|
let parser = TokenParser(tokens: tokens, namespace: context.namespace)
|
||||||
let nodes = try parser.parse()
|
let nodes = try parser.parse()
|
||||||
return try renderNodes(nodes, context ?? Context())
|
return try renderNodes(nodes, context)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user