forked from SwiftGen/StencilSwiftKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetNode.swift
39 lines (31 loc) · 946 Bytes
/
SetNode.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//
// StencilSwiftKit
// Copyright (c) 2017 SwiftGen
// MIT Licence
//
import Stencil
class SetNode: NodeType {
let variableName: String
let nodes: [NodeType]
class func parse(_ parser: TokenParser, token: Token) throws -> NodeType {
let comps = token.components()
guard comps.count == 2 else {
throw TemplateSyntaxError("'set' tag takes one argument, the name of the variable to set")
}
let variable = comps[1]
let setNodes = try parser.parse(until(["endset"]))
guard parser.nextToken() != nil else {
throw TemplateSyntaxError("`endset` was not found.")
}
return SetNode(variableName: variable, nodes: setNodes)
}
init(variableName: String, nodes: [NodeType]) {
self.variableName = variableName
self.nodes = nodes
}
func render(_ context: Context) throws -> String {
let result = try renderNodes(nodes, context)
context[variableName] = result
return ""
}
}