From 68e6ce30224f07b7f0cde9775578517dfcb21824 Mon Sep 17 00:00:00 2001 From: Valentin Knabel Date: Tue, 11 Oct 2016 13:45:15 +0200 Subject: [PATCH] feat: Creating of templates from a string literal (#71) --- Sources/Template.swift | 17 ++++++++++++++++- Tests/StencilTests/TemplateSpec.swift | 7 +++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/Sources/Template.swift b/Sources/Template.swift index aeb7c3d..250c409 100644 --- a/Sources/Template.swift +++ b/Sources/Template.swift @@ -6,7 +6,7 @@ let NSFileNoSuchFileError = 4 #endif /// A class representing a template -open class Template { +open class Template: ExpressibleByStringLiteral { let tokens: [Token] /// Create a template with the given name inside the given bundle @@ -35,6 +35,21 @@ open class Template { 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 open func render(_ context: Context? = nil) throws -> String { let context = context ?? Context() diff --git a/Tests/StencilTests/TemplateSpec.swift b/Tests/StencilTests/TemplateSpec.swift index a06f1bd..cba7b17 100644 --- a/Tests/StencilTests/TemplateSpec.swift +++ b/Tests/StencilTests/TemplateSpec.swift @@ -10,5 +10,12 @@ func testTemplate() { let result = try template.render(context) 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" + } } }