Compare commits
10 Commits
0.5.3
...
0.6.0-beta
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4ffc888ba4 | ||
|
|
3c21975b97 | ||
|
|
df9065f5a8 | ||
|
|
05b71736aa | ||
|
|
aa1399be55 | ||
|
|
bdc14ab1e1 | ||
|
|
67d4c52535 | ||
|
|
48026cde2c | ||
|
|
dc4b965aaa | ||
|
|
2190afee0d |
10
README.md
10
README.md
@@ -25,8 +25,8 @@ let context = Context(dictionary: [
|
|||||||
])
|
])
|
||||||
|
|
||||||
do {
|
do {
|
||||||
let template = Template(named: "template.stencil")
|
let template = try Template(named: "template.stencil")
|
||||||
let rendered = template.render(context)
|
let rendered = try template.render(context)
|
||||||
print(rendered)
|
print(rendered)
|
||||||
} catch {
|
} catch {
|
||||||
print("Failed to render template \(error)")
|
print("Failed to render template \(error)")
|
||||||
@@ -73,8 +73,8 @@ following lookup:
|
|||||||
For example, if `people` was an array:
|
For example, if `people` was an array:
|
||||||
|
|
||||||
```html+django
|
```html+django
|
||||||
There are {{ people.count }} people, {{ people.first }} is first person.
|
There are {{ people.count }} people. {{ people.first }} is the first person,
|
||||||
Followed by {{ people.1 }}.
|
followed by {{ people.1 }}.
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Filters
|
#### Filters
|
||||||
@@ -142,7 +142,7 @@ A for loop allows you to iterate over an array found by variable lookup.
|
|||||||
{% for item in items %}
|
{% for item in items %}
|
||||||
{{ item }}
|
{{ item }}
|
||||||
{% empty %}
|
{% empty %}
|
||||||
There we're no items.
|
There were no items.
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -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? {
|
||||||
@@ -35,20 +37,19 @@ public class Context {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Push a new level into the Context
|
/// Push a new level into the Context
|
||||||
public func push(dictionary: [String: Any]? = nil) {
|
private func push(dictionary: [String: Any]? = nil) {
|
||||||
dictionaries.append(dictionary ?? [:])
|
dictionaries.append(dictionary ?? [:])
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Pop the last level off of the Context
|
/// Pop the last level off of the Context
|
||||||
public func pop() -> [String: Any]? {
|
private func pop() -> [String: Any]? {
|
||||||
return dictionaries.popLast()
|
return dictionaries.popLast()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Push a new level onto the context for the duration of the execution of the given closure
|
/// Push a new level onto the context for the duration of the execution of the given closure
|
||||||
public func push<Result>(dictionary: [String: Any]? = nil, @noescape closure: (() throws -> Result)) rethrows -> Result {
|
public func push<Result>(dictionary: [String: Any]? = nil, @noescape closure: (() throws -> Result)) rethrows -> Result {
|
||||||
push(dictionary)
|
push(dictionary)
|
||||||
let result = try closure()
|
defer { pop() }
|
||||||
pop()
|
return try closure()
|
||||||
return result
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
62
Sources/ForTag.swift
Normal file
62
Sources/ForTag.swift
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
public class ForNode : NodeType {
|
||||||
|
let variable:Variable
|
||||||
|
let loopVariable:String
|
||||||
|
let nodes:[NodeType]
|
||||||
|
let emptyNodes: [NodeType]
|
||||||
|
|
||||||
|
public class func parse(parser:TokenParser, token:Token) throws -> NodeType {
|
||||||
|
let components = token.components()
|
||||||
|
|
||||||
|
guard components.count == 4 && components[2] == "in" else {
|
||||||
|
throw TemplateSyntaxError("'for' statements should use the following 'for x in y' `\(token.contents)`.")
|
||||||
|
}
|
||||||
|
|
||||||
|
let loopVariable = components[1]
|
||||||
|
let variable = components[3]
|
||||||
|
|
||||||
|
var emptyNodes = [NodeType]()
|
||||||
|
|
||||||
|
let forNodes = try parser.parse(until(["endfor", "empty"]))
|
||||||
|
|
||||||
|
guard let token = parser.nextToken() else {
|
||||||
|
throw TemplateSyntaxError("`endfor` was not found.")
|
||||||
|
}
|
||||||
|
|
||||||
|
if token.contents == "empty" {
|
||||||
|
emptyNodes = try parser.parse(until(["endfor"]))
|
||||||
|
parser.nextToken()
|
||||||
|
}
|
||||||
|
|
||||||
|
return ForNode(variable: variable, loopVariable: loopVariable, nodes: forNodes, emptyNodes:emptyNodes)
|
||||||
|
}
|
||||||
|
|
||||||
|
public init(variable:String, loopVariable:String, nodes:[NodeType], emptyNodes:[NodeType]) {
|
||||||
|
self.variable = Variable(variable)
|
||||||
|
self.loopVariable = loopVariable
|
||||||
|
self.nodes = nodes
|
||||||
|
self.emptyNodes = emptyNodes
|
||||||
|
}
|
||||||
|
|
||||||
|
public func render(context: Context) throws -> String {
|
||||||
|
let values = try variable.resolve(context)
|
||||||
|
|
||||||
|
if let values = values as? [Any] where values.count > 0 {
|
||||||
|
let count = values.count
|
||||||
|
return try values.enumerate().map { index, item in
|
||||||
|
let forContext: [String: Any] = [
|
||||||
|
"first": index == 0,
|
||||||
|
"last": index == (count - 1),
|
||||||
|
"counter": index + 1,
|
||||||
|
]
|
||||||
|
|
||||||
|
return try context.push([loopVariable: item, "forloop": forContext]) {
|
||||||
|
try renderNodes(nodes, context)
|
||||||
|
}
|
||||||
|
}.joinWithSeparator("")
|
||||||
|
}
|
||||||
|
|
||||||
|
return try context.push {
|
||||||
|
try renderNodes(emptyNodes, context)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
78
Sources/IfTag.swift
Normal file
78
Sources/IfTag.swift
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
public class IfNode : NodeType {
|
||||||
|
public let variable:Variable
|
||||||
|
public let trueNodes:[NodeType]
|
||||||
|
public let falseNodes:[NodeType]
|
||||||
|
|
||||||
|
public class func parse(parser:TokenParser, token:Token) throws -> NodeType {
|
||||||
|
let components = token.components()
|
||||||
|
guard components.count == 2 else {
|
||||||
|
throw TemplateSyntaxError("'if' statements should use the following 'if condition' `\(token.contents)`.")
|
||||||
|
}
|
||||||
|
let variable = components[1]
|
||||||
|
var trueNodes = [NodeType]()
|
||||||
|
var falseNodes = [NodeType]()
|
||||||
|
|
||||||
|
trueNodes = try parser.parse(until(["endif", "else"]))
|
||||||
|
|
||||||
|
guard let token = parser.nextToken() else {
|
||||||
|
throw TemplateSyntaxError("`endif` was not found.")
|
||||||
|
}
|
||||||
|
|
||||||
|
if token.contents == "else" {
|
||||||
|
falseNodes = try parser.parse(until(["endif"]))
|
||||||
|
parser.nextToken()
|
||||||
|
}
|
||||||
|
|
||||||
|
return IfNode(variable: variable, trueNodes: trueNodes, falseNodes: falseNodes)
|
||||||
|
}
|
||||||
|
|
||||||
|
public class func parse_ifnot(parser:TokenParser, token:Token) throws -> NodeType {
|
||||||
|
let components = token.components()
|
||||||
|
guard components.count == 2 else {
|
||||||
|
throw TemplateSyntaxError("'ifnot' statements should use the following 'if condition' `\(token.contents)`.")
|
||||||
|
}
|
||||||
|
let variable = components[1]
|
||||||
|
var trueNodes = [NodeType]()
|
||||||
|
var falseNodes = [NodeType]()
|
||||||
|
|
||||||
|
falseNodes = try parser.parse(until(["endif", "else"]))
|
||||||
|
|
||||||
|
guard let token = parser.nextToken() else {
|
||||||
|
throw TemplateSyntaxError("`endif` was not found.")
|
||||||
|
}
|
||||||
|
|
||||||
|
if token.contents == "else" {
|
||||||
|
trueNodes = try parser.parse(until(["endif"]))
|
||||||
|
parser.nextToken()
|
||||||
|
}
|
||||||
|
|
||||||
|
return IfNode(variable: variable, trueNodes: trueNodes, falseNodes: falseNodes)
|
||||||
|
}
|
||||||
|
|
||||||
|
public init(variable:String, trueNodes:[NodeType], falseNodes:[NodeType]) {
|
||||||
|
self.variable = Variable(variable)
|
||||||
|
self.trueNodes = trueNodes
|
||||||
|
self.falseNodes = falseNodes
|
||||||
|
}
|
||||||
|
|
||||||
|
public func render(context: Context) throws -> String {
|
||||||
|
let result = try variable.resolve(context)
|
||||||
|
var truthy = false
|
||||||
|
|
||||||
|
if let result = result as? [Any] {
|
||||||
|
truthy = !result.isEmpty
|
||||||
|
} else if let result = result as? [String:Any] {
|
||||||
|
truthy = !result.isEmpty
|
||||||
|
} else if result != nil {
|
||||||
|
truthy = true
|
||||||
|
}
|
||||||
|
|
||||||
|
return try context.push {
|
||||||
|
if truthy {
|
||||||
|
return try renderNodes(trueNodes, context)
|
||||||
|
} else {
|
||||||
|
return try renderNodes(falseNodes, context)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
||||||
<plist version="1.0">
|
|
||||||
<dict>
|
|
||||||
<key>CFBundleDevelopmentRegion</key>
|
|
||||||
<string>en</string>
|
|
||||||
<key>CFBundleExecutable</key>
|
|
||||||
<string>$(EXECUTABLE_NAME)</string>
|
|
||||||
<key>CFBundleIdentifier</key>
|
|
||||||
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
|
|
||||||
<key>CFBundleInfoDictionaryVersion</key>
|
|
||||||
<string>6.0</string>
|
|
||||||
<key>CFBundleName</key>
|
|
||||||
<string>$(PRODUCT_NAME)</string>
|
|
||||||
<key>CFBundlePackageType</key>
|
|
||||||
<string>FMWK</string>
|
|
||||||
<key>CFBundleShortVersionString</key>
|
|
||||||
<string>1.0</string>
|
|
||||||
<key>CFBundleSignature</key>
|
|
||||||
<string>????</string>
|
|
||||||
<key>CFBundleVersion</key>
|
|
||||||
<string>$(CURRENT_PROJECT_VERSION)</string>
|
|
||||||
<key>NSHumanReadableCopyright</key>
|
|
||||||
<string>Copyright © 2014 Cocode. All rights reserved.</string>
|
|
||||||
<key>NSPrincipalClass</key>
|
|
||||||
<string></string>
|
|
||||||
</dict>
|
|
||||||
</plist>
|
|
||||||
@@ -74,10 +74,9 @@ class ExtendsNode : NodeType {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let blockContext = BlockContext(blocks: blocks)
|
let blockContext = BlockContext(blocks: blocks)
|
||||||
context.push([BlockContext.contextKey: blockContext])
|
return try context.push([BlockContext.contextKey: blockContext]) {
|
||||||
let result = try template.render(context)
|
return try template.render(context)
|
||||||
context.pop()
|
}
|
||||||
return result
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
|
|
||||||
public struct TemplateSyntaxError : ErrorType, Equatable, CustomStringConvertible {
|
public struct TemplateSyntaxError : ErrorType, Equatable, CustomStringConvertible {
|
||||||
public let description:String
|
public let description:String
|
||||||
|
|
||||||
@@ -8,15 +9,18 @@ public struct TemplateSyntaxError : ErrorType, Equatable, CustomStringConvertibl
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public func ==(lhs:TemplateSyntaxError, rhs:TemplateSyntaxError) -> Bool {
|
public func ==(lhs:TemplateSyntaxError, rhs:TemplateSyntaxError) -> Bool {
|
||||||
return lhs.description == rhs.description
|
return lhs.description == rhs.description
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public protocol NodeType {
|
public protocol NodeType {
|
||||||
/// Render the node in the given context
|
/// Render the node in the given context
|
||||||
func render(context:Context) throws -> String
|
func render(context:Context) throws -> String
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Render the collection of nodes in the given context
|
/// Render the collection of nodes in the given context
|
||||||
public func renderNodes(nodes:[NodeType], _ context:Context) throws -> String {
|
public func renderNodes(nodes:[NodeType], _ context:Context) throws -> String {
|
||||||
return try nodes.map { try $0.render(context) }.joinWithSeparator("")
|
return try nodes.map { try $0.render(context) }.joinWithSeparator("")
|
||||||
@@ -34,6 +38,7 @@ public class SimpleNode : NodeType {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public class TextNode : NodeType {
|
public class TextNode : NodeType {
|
||||||
public let text:String
|
public let text:String
|
||||||
|
|
||||||
@@ -46,10 +51,12 @@ public class TextNode : NodeType {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public protocol Resolvable {
|
public protocol Resolvable {
|
||||||
func resolve(context: Context) throws -> Any?
|
func resolve(context: Context) throws -> Any?
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public class VariableNode : NodeType {
|
public class VariableNode : NodeType {
|
||||||
public let variable: Resolvable
|
public let variable: Resolvable
|
||||||
|
|
||||||
@@ -75,191 +82,3 @@ public class VariableNode : NodeType {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#if !os(Linux)
|
|
||||||
public class NowNode : NodeType {
|
|
||||||
public let format:Variable
|
|
||||||
|
|
||||||
public class func parse(parser:TokenParser, token:Token) throws -> NodeType {
|
|
||||||
var format:Variable?
|
|
||||||
|
|
||||||
let components = token.components()
|
|
||||||
guard components.count <= 2 else {
|
|
||||||
throw TemplateSyntaxError("'now' tags may only have one argument: the format string `\(token.contents)`.")
|
|
||||||
}
|
|
||||||
if components.count == 2 {
|
|
||||||
format = Variable(components[1])
|
|
||||||
}
|
|
||||||
|
|
||||||
return NowNode(format:format)
|
|
||||||
}
|
|
||||||
|
|
||||||
public init(format:Variable?) {
|
|
||||||
self.format = format ?? Variable("\"yyyy-MM-dd 'at' HH:mm\"")
|
|
||||||
}
|
|
||||||
|
|
||||||
public func render(context: Context) throws -> String {
|
|
||||||
let date = NSDate()
|
|
||||||
let format = try self.format.resolve(context)
|
|
||||||
var formatter:NSDateFormatter?
|
|
||||||
|
|
||||||
if let format = format as? NSDateFormatter {
|
|
||||||
formatter = format
|
|
||||||
} else if let format = format as? String {
|
|
||||||
formatter = NSDateFormatter()
|
|
||||||
formatter!.dateFormat = format
|
|
||||||
} else {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
return formatter!.stringFromDate(date)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
public class ForNode : NodeType {
|
|
||||||
let variable:Variable
|
|
||||||
let loopVariable:String
|
|
||||||
let nodes:[NodeType]
|
|
||||||
let emptyNodes: [NodeType]
|
|
||||||
|
|
||||||
public class func parse(parser:TokenParser, token:Token) throws -> NodeType {
|
|
||||||
let components = token.components()
|
|
||||||
|
|
||||||
guard components.count == 4 && components[2] == "in" else {
|
|
||||||
throw TemplateSyntaxError("'for' statements should use the following 'for x in y' `\(token.contents)`.")
|
|
||||||
}
|
|
||||||
|
|
||||||
let loopVariable = components[1]
|
|
||||||
let variable = components[3]
|
|
||||||
|
|
||||||
var emptyNodes = [NodeType]()
|
|
||||||
|
|
||||||
let forNodes = try parser.parse(until(["endfor", "empty"]))
|
|
||||||
|
|
||||||
guard let token = parser.nextToken() else {
|
|
||||||
throw TemplateSyntaxError("`endfor` was not found.")
|
|
||||||
}
|
|
||||||
|
|
||||||
if token.contents == "empty" {
|
|
||||||
emptyNodes = try parser.parse(until(["endfor"]))
|
|
||||||
parser.nextToken()
|
|
||||||
}
|
|
||||||
|
|
||||||
return ForNode(variable: variable, loopVariable: loopVariable, nodes: forNodes, emptyNodes:emptyNodes)
|
|
||||||
}
|
|
||||||
|
|
||||||
public init(variable:String, loopVariable:String, nodes:[NodeType], emptyNodes:[NodeType]) {
|
|
||||||
self.variable = Variable(variable)
|
|
||||||
self.loopVariable = loopVariable
|
|
||||||
self.nodes = nodes
|
|
||||||
self.emptyNodes = emptyNodes
|
|
||||||
}
|
|
||||||
|
|
||||||
public func render(context: Context) throws -> String {
|
|
||||||
let values = try variable.resolve(context)
|
|
||||||
|
|
||||||
if let values = values as? [Any] where values.count > 0 {
|
|
||||||
let count = values.count
|
|
||||||
return try values.enumerate().map { index, item in
|
|
||||||
let forContext: [String: Any] = [
|
|
||||||
"first": index == 0,
|
|
||||||
"last": index == (count - 1),
|
|
||||||
"counter": index + 1,
|
|
||||||
]
|
|
||||||
|
|
||||||
return try context.push([loopVariable: item, "forloop": forContext]) {
|
|
||||||
try renderNodes(nodes, context)
|
|
||||||
}
|
|
||||||
}.joinWithSeparator("")
|
|
||||||
}
|
|
||||||
|
|
||||||
return try context.push {
|
|
||||||
try renderNodes(emptyNodes, context)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class IfNode : NodeType {
|
|
||||||
public let variable:Variable
|
|
||||||
public let trueNodes:[NodeType]
|
|
||||||
public let falseNodes:[NodeType]
|
|
||||||
|
|
||||||
public class func parse(parser:TokenParser, token:Token) throws -> NodeType {
|
|
||||||
let components = token.components()
|
|
||||||
guard components.count == 2 else {
|
|
||||||
throw TemplateSyntaxError("'if' statements should use the following 'if condition' `\(token.contents)`.")
|
|
||||||
}
|
|
||||||
let variable = components[1]
|
|
||||||
var trueNodes = [NodeType]()
|
|
||||||
var falseNodes = [NodeType]()
|
|
||||||
|
|
||||||
trueNodes = try parser.parse(until(["endif", "else"]))
|
|
||||||
|
|
||||||
guard let token = parser.nextToken() else {
|
|
||||||
throw TemplateSyntaxError("`endif` was not found.")
|
|
||||||
}
|
|
||||||
|
|
||||||
if token.contents == "else" {
|
|
||||||
falseNodes = try parser.parse(until(["endif"]))
|
|
||||||
parser.nextToken()
|
|
||||||
}
|
|
||||||
|
|
||||||
return IfNode(variable: variable, trueNodes: trueNodes, falseNodes: falseNodes)
|
|
||||||
}
|
|
||||||
|
|
||||||
public class func parse_ifnot(parser:TokenParser, token:Token) throws -> NodeType {
|
|
||||||
let components = token.components()
|
|
||||||
guard components.count == 2 else {
|
|
||||||
throw TemplateSyntaxError("'ifnot' statements should use the following 'if condition' `\(token.contents)`.")
|
|
||||||
}
|
|
||||||
let variable = components[1]
|
|
||||||
var trueNodes = [NodeType]()
|
|
||||||
var falseNodes = [NodeType]()
|
|
||||||
|
|
||||||
falseNodes = try parser.parse(until(["endif", "else"]))
|
|
||||||
|
|
||||||
guard let token = parser.nextToken() else {
|
|
||||||
throw TemplateSyntaxError("`endif` was not found.")
|
|
||||||
}
|
|
||||||
|
|
||||||
if token.contents == "else" {
|
|
||||||
trueNodes = try parser.parse(until(["endif"]))
|
|
||||||
parser.nextToken()
|
|
||||||
}
|
|
||||||
|
|
||||||
return IfNode(variable: variable, trueNodes: trueNodes, falseNodes: falseNodes)
|
|
||||||
}
|
|
||||||
|
|
||||||
public init(variable:String, trueNodes:[NodeType], falseNodes:[NodeType]) {
|
|
||||||
self.variable = Variable(variable)
|
|
||||||
self.trueNodes = trueNodes
|
|
||||||
self.falseNodes = falseNodes
|
|
||||||
}
|
|
||||||
|
|
||||||
public func render(context: Context) throws -> String {
|
|
||||||
let result = try variable.resolve(context)
|
|
||||||
var truthy = false
|
|
||||||
|
|
||||||
if let result = result as? [Any] {
|
|
||||||
truthy = !result.isEmpty
|
|
||||||
} else if let result = result as? [String:Any] {
|
|
||||||
truthy = !result.isEmpty
|
|
||||||
} else if result != nil {
|
|
||||||
truthy = true
|
|
||||||
}
|
|
||||||
|
|
||||||
context.push()
|
|
||||||
let output:String
|
|
||||||
if truthy {
|
|
||||||
output = try renderNodes(trueNodes, context)
|
|
||||||
} else {
|
|
||||||
output = try renderNodes(falseNodes, context)
|
|
||||||
}
|
|
||||||
context.pop()
|
|
||||||
|
|
||||||
return output
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
43
Sources/NowTag.swift
Normal file
43
Sources/NowTag.swift
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
#if !os(Linux)
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
|
||||||
|
public class NowNode : NodeType {
|
||||||
|
public let format:Variable
|
||||||
|
|
||||||
|
public class func parse(parser:TokenParser, token:Token) throws -> NodeType {
|
||||||
|
var format:Variable?
|
||||||
|
|
||||||
|
let components = token.components()
|
||||||
|
guard components.count <= 2 else {
|
||||||
|
throw TemplateSyntaxError("'now' tags may only have one argument: the format string `\(token.contents)`.")
|
||||||
|
}
|
||||||
|
if components.count == 2 {
|
||||||
|
format = Variable(components[1])
|
||||||
|
}
|
||||||
|
|
||||||
|
return NowNode(format:format)
|
||||||
|
}
|
||||||
|
|
||||||
|
public init(format:Variable?) {
|
||||||
|
self.format = format ?? Variable("\"yyyy-MM-dd 'at' HH:mm\"")
|
||||||
|
}
|
||||||
|
|
||||||
|
public func render(context: Context) throws -> String {
|
||||||
|
let date = NSDate()
|
||||||
|
let format = try self.format.resolve(context)
|
||||||
|
var formatter:NSDateFormatter?
|
||||||
|
|
||||||
|
if let format = format as? NSDateFormatter {
|
||||||
|
formatter = format
|
||||||
|
} else if let format = format as? String {
|
||||||
|
formatter = NSDateFormatter()
|
||||||
|
formatter!.dateFormat = format
|
||||||
|
} else {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
return formatter!.stringFromDate(date)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
@@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "Stencil",
|
"name": "Stencil",
|
||||||
"version": "0.5.3",
|
"version": "0.6.0-beta.1",
|
||||||
"summary": "Stencil is a simple and powerful template language for Swift.",
|
"summary": "Stencil is a simple and powerful template language for Swift.",
|
||||||
"homepage": "https://github.com/kylef/Stencil",
|
"homepage": "https://github.com/kylef/Stencil",
|
||||||
"license": {
|
"license": {
|
||||||
@@ -13,7 +13,7 @@
|
|||||||
"social_media_url": "http://twitter.com/kylefuller",
|
"social_media_url": "http://twitter.com/kylefuller",
|
||||||
"source": {
|
"source": {
|
||||||
"git": "https://github.com/kylef/Stencil.git",
|
"git": "https://github.com/kylef/Stencil.git",
|
||||||
"tag": "0.5.3"
|
"tag": "0.6.0-beta.1"
|
||||||
},
|
},
|
||||||
"source_files": [
|
"source_files": [
|
||||||
"Sources/*.swift"
|
"Sources/*.swift"
|
||||||
|
|||||||
@@ -27,31 +27,26 @@ func testContext() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$0.it("allows you to retrieve a value from a parent") {
|
$0.it("allows you to retrieve a value from a parent") {
|
||||||
context.push()
|
try context.push {
|
||||||
|
try expect(context["name"] as? String) == "Kyle"
|
||||||
try expect(context["name"] as? String) == "Kyle"
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$0.it("allows you to override a parent's value") {
|
$0.it("allows you to override a parent's value") {
|
||||||
context.push()
|
try context.push {
|
||||||
context["name"] = "Katie"
|
context["name"] = "Katie"
|
||||||
|
try expect(context["name"] as? String) == "Katie"
|
||||||
try expect(context["name"] as? String) == "Katie"
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$0.it("allows you to pop to restore previous state") {
|
$0.it("allows you to pop to restore previous state") {
|
||||||
context.push()
|
context.push {
|
||||||
context["name"] = "Katie"
|
context["name"] = "Katie"
|
||||||
context.pop()
|
}
|
||||||
|
|
||||||
try expect(context["name"] as? String) == "Kyle"
|
try expect(context["name"] as? String) == "Kyle"
|
||||||
}
|
}
|
||||||
|
|
||||||
$0.it("allows you to push a dictionary onto the stack") {
|
|
||||||
context.push(["name": "Katie"])
|
|
||||||
try expect(context["name"] as? String) == "Katie"
|
|
||||||
}
|
|
||||||
|
|
||||||
$0.it("allows you to push a dictionary and run a closure then restoring previous state") {
|
$0.it("allows you to push a dictionary and run a closure then restoring previous state") {
|
||||||
var didRun = false
|
var didRun = false
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import Stencil
|
|||||||
|
|
||||||
func testFilter() {
|
func testFilter() {
|
||||||
describe("template filters") {
|
describe("template filters") {
|
||||||
let context = Context(dictionary: ["name": "Kyle"])
|
let context: [String: Any] = ["name": "Kyle"]
|
||||||
|
|
||||||
$0.it("allows you to register a custom filter") {
|
$0.it("allows you to register a custom filter") {
|
||||||
let template = Template(templateString: "{{ name|repeat }}")
|
let template = Template(templateString: "{{ name|repeat }}")
|
||||||
@@ -18,7 +18,7 @@ func testFilter() {
|
|||||||
return nil
|
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"
|
try expect(result) == "Kyle Kyle"
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -29,7 +29,7 @@ func testFilter() {
|
|||||||
throw TemplateSyntaxError("No Repeat")
|
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") {
|
$0.it("allows whitespace in expression") {
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ func testStencil() {
|
|||||||
return CustomNode()
|
return CustomNode()
|
||||||
}
|
}
|
||||||
|
|
||||||
let result = try template.render(namespace: namespace)
|
let result = try template.render(Context(namespace: namespace))
|
||||||
try expect(result) == "Hello World"
|
try expect(result) == "Hello World"
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,7 +59,7 @@ func testStencil() {
|
|||||||
return "Hello World"
|
return "Hello World"
|
||||||
}
|
}
|
||||||
|
|
||||||
try expect(try template.render(namespace: namespace)) == "Hello World"
|
try expect(try template.render(Context(namespace: namespace))) == "Hello World"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ func testVariable() {
|
|||||||
])
|
])
|
||||||
|
|
||||||
#if os(OSX)
|
#if os(OSX)
|
||||||
context.push(["object": Object()])
|
context["object"] = Object()
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
$0.it("can resolve a string literal with double quotes") {
|
$0.it("can resolve a string literal with double quotes") {
|
||||||
|
|||||||
Reference in New Issue
Block a user