diff --git a/Stencil/Context.swift b/Stencil/Context.swift index 24568e4..b6ea6dc 100644 --- a/Stencil/Context.swift +++ b/Stencil/Context.swift @@ -1,13 +1,13 @@ -import Foundation - /// A container for template variables. public class Context : Equatable { - var dictionaries:[Dictionary] + var dictionaries:[[String:AnyObject]] - public init(dictionary:Dictionary) { + /// Initialise a Context with a dictionary + public init(dictionary:[String:AnyObject]) { dictionaries = [dictionary] } + /// Initialise an empty Context public init() { dictionaries = [] } @@ -16,7 +16,7 @@ public class Context : Equatable { /// Retrieves a variable's value, starting at the current context and going upwards get { for dictionary in Array(dictionaries.reverse()) { - if let value:AnyObject = dictionary[key] { + if let value = dictionary[key] { return value } } @@ -26,24 +26,21 @@ public class Context : Equatable { /// Set a variable in the current context, deleting the variable if it's nil set(value) { - if dictionaries.count > 0 { - var dictionary = dictionaries.removeLast() + if var dictionary = dictionaries.popLast() { dictionary[key] = value dictionaries.append(dictionary) } } } - public func push() { - push(Dictionary()) + /// Push a new level into the Context + public func push(dictionary:[String:AnyObject]? = nil) { + dictionaries.append(dictionary ?? [:]) } - public func push(dictionary:Dictionary) { - dictionaries.append(dictionary) - } - - public func pop() { - dictionaries.removeLast() + /// Pop the last level off of the Context + public func pop() -> [String:AnyObject]? { + return dictionaries.popLast() } } diff --git a/Stencil/Parser.swift b/Stencil/Parser.swift index 8fab916..b8a0717 100644 --- a/Stencil/Parser.swift +++ b/Stencil/Parser.swift @@ -28,7 +28,7 @@ public class TokenParser { } private var tokens:[Token] - private var tags = Dictionary() + private var tags = [String:TagParser]() public init(tokens:[Token]) { self.tokens = tokens diff --git a/Stencil/Variable.swift b/Stencil/Variable.swift index a9f6e63..578baa7 100644 --- a/Stencil/Variable.swift +++ b/Stencil/Variable.swift @@ -25,7 +25,7 @@ public struct Variable : Equatable { for bit in lookup() { if let context = current as? Context { current = context[bit] - } else if let dictionary = current as? Dictionary { + } else if let dictionary = current as? [String:AnyObject] { current = dictionary[bit] } else if let array = current as? [AnyObject] { if let index = Int(bit) {