diff --git a/Stencil/Context.swift b/Stencil/Context.swift index b6ea6dc..0f56cd7 100644 --- a/Stencil/Context.swift +++ b/Stencil/Context.swift @@ -1,9 +1,9 @@ /// A container for template variables. -public class Context : Equatable { - var dictionaries:[[String:AnyObject]] +public class Context { + var dictionaries:[[String: Any]] /// Initialise a Context with a dictionary - public init(dictionary:[String:AnyObject]) { + public init(dictionary:[String: Any]) { dictionaries = [dictionary] } @@ -12,7 +12,7 @@ public class Context : Equatable { dictionaries = [] } - public subscript(key: String) -> AnyObject? { + public subscript(key: String) -> Any? { /// Retrieves a variable's value, starting at the current context and going upwards get { for dictionary in Array(dictionaries.reverse()) { @@ -34,16 +34,12 @@ public class Context : Equatable { } /// Push a new level into the Context - public func push(dictionary:[String:AnyObject]? = nil) { + public func push(dictionary: [String: Any]? = nil) { dictionaries.append(dictionary ?? [:]) } /// Pop the last level off of the Context - public func pop() -> [String:AnyObject]? { + public func pop() -> [String: Any]? { return dictionaries.popLast() } } - -public func ==(lhs:Context, rhs:Context) -> Bool { - return lhs.dictionaries == rhs.dictionaries -} diff --git a/Stencil/Filters.swift b/Stencil/Filters.swift index ed31250..d97b03f 100644 --- a/Stencil/Filters.swift +++ b/Stencil/Filters.swift @@ -30,4 +30,4 @@ func lowercase(value: Any?) -> Any? { } return value -} \ No newline at end of file +} diff --git a/Stencil/Node.swift b/Stencil/Node.swift index f4892ae..c0551a9 100644 --- a/Stencil/Node.swift +++ b/Stencil/Node.swift @@ -154,7 +154,8 @@ public class ForNode : NodeType { } public func render(context: Context) throws -> String { - if let values = variable.resolve(context) as? [AnyObject] { + let values = variable.resolve(context) + if let values = values as? NSArray { return try values.map { item in context.push() context[loopVariable] = item diff --git a/Stencil/Variable.swift b/Stencil/Variable.swift index 4d65456..b726399 100644 --- a/Stencil/Variable.swift +++ b/Stencil/Variable.swift @@ -48,7 +48,7 @@ public struct Variable : Equatable, Resolvable { /// Resolve the variable in the given context public func resolve(context:Context) -> Any? { - var current:AnyObject? = context + var current: Any? = context if (variable.hasPrefix("'") && variable.hasSuffix("'")) || (variable.hasPrefix("\"") && variable.hasSuffix("\"")) { // String literal @@ -58,9 +58,11 @@ public struct Variable : Equatable, Resolvable { for bit in lookup() { if let context = current as? Context { current = context[bit] - } else if let dictionary = current as? [String:AnyObject] { + } else if let dictionary = current as? [String: Any] { current = dictionary[bit] - } else if let array = current as? [AnyObject] { + } else if let dictionary = current as? [String: AnyObject] { + current = dictionary[bit] + } else if let array = current as? [Any] { if let index = Int(bit) { current = array[index] } else if bit == "first" { @@ -70,6 +72,16 @@ public struct Variable : Equatable, Resolvable { } else if bit == "count" { current = array.count } + } else if let array = current as? NSArray { + if let index = Int(bit) { + current = array[index] + } else if bit == "first" { + current = array.firstObject + } else if bit == "last" { + current = array.lastObject + } else if bit == "count" { + current = array.count + } } else if let object = current as? NSObject { current = object.valueForKey(bit) } else {