Resolve extends and include arguments as variables
This commit is contained in:
@@ -2,19 +2,19 @@ import PathKit
|
|||||||
|
|
||||||
|
|
||||||
public class IncludeNode : NodeType {
|
public class IncludeNode : NodeType {
|
||||||
public let templateName: String
|
public let templateName: Variable
|
||||||
|
|
||||||
public class func parse(parser: TokenParser, token: Token) throws -> NodeType {
|
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")
|
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
|
self.templateName = templateName
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -23,6 +23,10 @@ public class IncludeNode : NodeType {
|
|||||||
throw TemplateSyntaxError("Template loader not in context")
|
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 {
|
guard let template = loader.loadTemplate(templateName) else {
|
||||||
let paths = loader.paths.map { $0.description }.joinWithSeparator(", ")
|
let paths = loader.paths.map { $0.description }.joinWithSeparator(", ")
|
||||||
throw TemplateSyntaxError("'\(templateName)' template not found in \(paths)")
|
throw TemplateSyntaxError("'\(templateName)' template not found in \(paths)")
|
||||||
|
|||||||
@@ -27,13 +27,13 @@ extension CollectionType {
|
|||||||
|
|
||||||
|
|
||||||
class ExtendsNode : NodeType {
|
class ExtendsNode : NodeType {
|
||||||
let templateName: String
|
let templateName: Variable
|
||||||
let blocks: [String:BlockNode]
|
let blocks: [String:BlockNode]
|
||||||
|
|
||||||
class func parse(parser: TokenParser, token: Token) throws -> NodeType {
|
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")
|
throw TemplateSyntaxError("'extends' takes one argument, the template file to be extended")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,10 +51,10 @@ class ExtendsNode : NodeType {
|
|||||||
return dict
|
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.templateName = templateName
|
||||||
self.blocks = blocks
|
self.blocks = blocks
|
||||||
}
|
}
|
||||||
@@ -64,6 +64,10 @@ class ExtendsNode : NodeType {
|
|||||||
throw TemplateSyntaxError("Template loader not in context")
|
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 {
|
guard let template = loader.loadTemplate(templateName) else {
|
||||||
let paths:String = loader.paths.map { $0.description }.joinWithSeparator(", ")
|
let paths:String = loader.paths.map { $0.description }.joinWithSeparator(", ")
|
||||||
throw TemplateSyntaxError("'\(templateName)' template not found in \(paths)")
|
throw TemplateSyntaxError("'\(templateName)' template not found in \(paths)")
|
||||||
|
|||||||
@@ -23,13 +23,13 @@ describe("Include") {
|
|||||||
let nodes = try parser.parse()
|
let nodes = try parser.parse()
|
||||||
let node = nodes.first as? IncludeNode
|
let node = nodes.first as? IncludeNode
|
||||||
try expect(nodes.count) == 1
|
try expect(nodes.count) == 1
|
||||||
try expect(node?.templateName) == "test.html"
|
try expect(node?.templateName) == Variable("\"test.html\"")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$0.describe("rendering") {
|
$0.describe("rendering") {
|
||||||
$0.it("throws an error when rendering without a loader") {
|
$0.it("throws an error when rendering without a loader") {
|
||||||
let node = IncludeNode(templateName: "test.html")
|
let node = IncludeNode(templateName: Variable("\"test.html\""))
|
||||||
|
|
||||||
do {
|
do {
|
||||||
try node.render(Context())
|
try node.render(Context())
|
||||||
@@ -39,7 +39,7 @@ describe("Include") {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$0.it("throws an error when it cannot find the included template") {
|
$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 {
|
do {
|
||||||
try node.render(Context(dictionary: ["loader": loader]))
|
try node.render(Context(dictionary: ["loader": loader]))
|
||||||
@@ -49,7 +49,7 @@ describe("Include") {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$0.it("successfully renders a found included template") {
|
$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 context = Context(dictionary: ["loader":loader, "target": "World"])
|
||||||
let value = try node.render(context)
|
let value = try node.render(context)
|
||||||
try expect(value) == "Hello World!"
|
try expect(value) == "Hello World!"
|
||||||
|
|||||||
Reference in New Issue
Block a user