Resolve extends and include arguments as variables

This commit is contained in:
Kyle Fuller
2015-11-21 14:42:23 +00:00
parent 48a9a65bd5
commit 3bd3aec296
3 changed files with 22 additions and 14 deletions

View File

@@ -2,19 +2,19 @@ import PathKit
public class IncludeNode : NodeType {
public let templateName: String
public let templateName: Variable
public class func parse(parser: TokenParser, token: Token) throws -> NodeType {
let bits = token.contents.characters.split("\"", allowEmptySlices: true).map(String.init)
let bits = token.components()
guard bits.count == 3 else {
guard bits.count == 2 else {
throw TemplateSyntaxError("'include' tag takes one argument, the template file to be included")
}
return IncludeNode(templateName: bits[1])
return IncludeNode(templateName: Variable(bits[1]))
}
public init(templateName: String) {
public init(templateName: Variable) {
self.templateName = templateName
}
@@ -23,6 +23,10 @@ public class IncludeNode : NodeType {
throw TemplateSyntaxError("Template loader not in context")
}
guard let templateName = try self.templateName.resolve(context) as? String else {
throw TemplateSyntaxError("'\(self.templateName)' could not be resolved as a string")
}
guard let template = loader.loadTemplate(templateName) else {
let paths = loader.paths.map { $0.description }.joinWithSeparator(", ")
throw TemplateSyntaxError("'\(templateName)' template not found in \(paths)")