feat: Creating of templates from a string literal (#71)

This commit is contained in:
Valentin Knabel
2016-10-11 13:45:15 +02:00
committed by Kyle Fuller
parent 65c3052aee
commit 68e6ce3022
2 changed files with 23 additions and 1 deletions

View File

@@ -6,7 +6,7 @@ let NSFileNoSuchFileError = 4
#endif #endif
/// A class representing a template /// A class representing a template
open class Template { open class Template: ExpressibleByStringLiteral {
let tokens: [Token] let tokens: [Token]
/// Create a template with the given name inside the given bundle /// Create a template with the given name inside the given bundle
@@ -35,6 +35,21 @@ open class Template {
tokens = lexer.tokenize() tokens = lexer.tokenize()
} }
// Create a template with a template string literal
public convenience required init(stringLiteral value:String) {
self.init(templateString: value)
}
// Create a template with a template string literal
public convenience required init(extendedGraphemeClusterLiteral value:StringLiteralType) {
self.init(stringLiteral: value)
}
// Create a template with a template string literal
public convenience required init(unicodeScalarLiteral value:StringLiteralType) {
self.init(stringLiteral: value)
}
/// Render the given template /// Render the given template
open func render(_ context: Context? = nil) throws -> String { open func render(_ context: Context? = nil) throws -> String {
let context = context ?? Context() let context = context ?? Context()

View File

@@ -10,5 +10,12 @@ func testTemplate() {
let result = try template.render(context) let result = try template.render(context)
try expect(result) == "Hello World" try expect(result) == "Hello World"
} }
$0.it("can render a template from a string literal") {
let context = Context(dictionary: [ "name": "Kyle" ])
let template: Template = "Hello World"
let result = try template.render(context)
try expect(result) == "Hello World"
}
} }
} }