diff --git a/Stencil/TemplateLoader/Include.swift b/Stencil/TemplateLoader/Include.swift index 647af38..a5added 100644 --- a/Stencil/TemplateLoader/Include.swift +++ b/Stencil/TemplateLoader/Include.swift @@ -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)") diff --git a/Stencil/TemplateLoader/Inheritence.swift b/Stencil/TemplateLoader/Inheritence.swift index 51342ea..f1efa28 100644 --- a/Stencil/TemplateLoader/Inheritence.swift +++ b/Stencil/TemplateLoader/Inheritence.swift @@ -27,13 +27,13 @@ extension CollectionType { class ExtendsNode : NodeType { - let templateName: String + let templateName: Variable let blocks: [String:BlockNode] class func parse(parser: TokenParser, token: Token) throws -> NodeType { - let bits = token.contents.componentsSeparatedByString("\"") + let bits = token.components() - guard bits.count == 3 else { + guard bits.count == 2 else { throw TemplateSyntaxError("'extends' takes one argument, the template file to be extended") } @@ -51,10 +51,10 @@ class ExtendsNode : NodeType { return dict } - return ExtendsNode(templateName: bits[1], blocks: nodes) + return ExtendsNode(templateName: Variable(bits[1]), blocks: nodes) } - init(templateName: String, blocks: [String:BlockNode]) { + init(templateName: Variable, blocks: [String: BlockNode]) { self.templateName = templateName self.blocks = blocks } @@ -64,6 +64,10 @@ class ExtendsNode : 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:String = loader.paths.map { $0.description }.joinWithSeparator(", ") throw TemplateSyntaxError("'\(templateName)' template not found in \(paths)") diff --git a/StencilSpecs/TemplateLoader/IncludeSpec.swift b/StencilSpecs/TemplateLoader/IncludeSpec.swift index 80edb85..6a0c7e9 100644 --- a/StencilSpecs/TemplateLoader/IncludeSpec.swift +++ b/StencilSpecs/TemplateLoader/IncludeSpec.swift @@ -23,13 +23,13 @@ describe("Include") { let nodes = try parser.parse() let node = nodes.first as? IncludeNode try expect(nodes.count) == 1 - try expect(node?.templateName) == "test.html" + try expect(node?.templateName) == Variable("\"test.html\"") } } $0.describe("rendering") { $0.it("throws an error when rendering without a loader") { - let node = IncludeNode(templateName: "test.html") + let node = IncludeNode(templateName: Variable("\"test.html\"")) do { try node.render(Context()) @@ -39,7 +39,7 @@ describe("Include") { } $0.it("throws an error when it cannot find the included template") { - let node = IncludeNode(templateName: "unknown.html") + let node = IncludeNode(templateName: Variable("\"unknown.html\"")) do { try node.render(Context(dictionary: ["loader": loader])) @@ -49,7 +49,7 @@ describe("Include") { } $0.it("successfully renders a found included template") { - let node = IncludeNode(templateName: "test.html") + let node = IncludeNode(templateName: Variable("\"test.html\"")) let context = Context(dictionary: ["loader":loader, "target": "World"]) let value = try node.render(context) try expect(value) == "Hello World!"