feat(context): Add flatten method

This commit is contained in:
Kyle Fuller
2016-11-28 05:51:27 +00:00
parent 429290e0b7
commit 1975cfd627
4 changed files with 74 additions and 0 deletions

View File

@@ -52,4 +52,16 @@ public class Context {
defer { _ = pop() } defer { _ = pop() }
return try closure() return try closure()
} }
public func flatten() -> [String: Any] {
var accumulator: [String: Any] = [:]
for dictionary in dictionaries {
for (key, value) in dictionary {
accumulator.updateValue(value, forKey: key)
}
}
return accumulator
}
} }

View File

@@ -58,5 +58,15 @@ func testContext() {
try expect(didRun).to.beTrue() try expect(didRun).to.beTrue()
try expect(context["name"] as? String) == "Kyle" try expect(context["name"] as? String) == "Kyle"
} }
$0.it("allows you to flatten the context contents") {
try context.push(dictionary: ["test": "abc"]) {
let flattened = context.flatten()
try expect(flattened.count) == 2
try expect(flattened["name"] as? String) == "Kyle"
try expect(flattened["test"] as? String) == "abc"
}
}
} }
} }

51
docs/api/context.rst Normal file
View File

@@ -0,0 +1,51 @@
Context
=======
A Context is a structure containing any templates you would like to use in a
template. Its somewhat like a dictionary, however you can push and pop to
scope variables. So that means that when iterating over a for loop, you can
push a new scope into the context to store any variables local to the scope.
You can initialise a ``Context`` with a ``Dictionary``.
.. code-block:: swift
Context(dictionary: [String: Any]? = nil)
API
----
Subscripting
~~~~~~~~~~~~
You can use subscripting to get and set values from the context.
.. code-block:: swift
context["key"] = value
let value = context["key"]
``push()``
~~~~~~~~~~
A ``Context`` is a stack. You can push a new level onto the ``Context`` so that
modifications can easily be poped off. This is useful for isolating mutations
into scope of a template tag. Such as ``{% if %}`` and ``{% for %}`` tags.
.. code-block:: swift
context.push(["name": "example"]) {
// context contains name which is `example`.
}
// name is popped off the context after the duration of the closure.
``flatten()``
~~~~~~~~~~~~~
Using ``flatten()`` method you can get whole ``Context`` stack as one
dictionary including all variables.
.. code-block:: swift
let dictionary = context.flatten()

View File

@@ -39,4 +39,5 @@ Contents:
templates templates
builtins builtins
api/context
custom-template-tags-and-filters custom-template-tags-and-filters