From 000e9a7f1abae062d2971592d8256351b3de620d Mon Sep 17 00:00:00 2001 From: Kyle Fuller Date: Thu, 7 Sep 2017 18:40:21 +0100 Subject: [PATCH] feat: Add DictionaryLoader to load templates from dictionary Closes #97 --- CHANGELOG.md | 2 ++ Sources/Loader.swift | 27 +++++++++++++++++++++++++++ Tests/StencilTests/LoaderSpec.swift | 23 +++++++++++++++++++++++ docs/api.rst | 10 ++++++++++ 4 files changed, 62 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index dcff662..b14aaa6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ - Adds `counter0` to for loop context allowing you to get the current index of the for loop 0 indexed. +- Introduces a new `DictionaryLoader` for loading templates from a Swift + Dictionary. ### Bug Fixes diff --git a/Sources/Loader.swift b/Sources/Loader.swift index c12e9c4..201dbae 100644 --- a/Sources/Loader.swift +++ b/Sources/Loader.swift @@ -75,6 +75,33 @@ public class FileSystemLoader: Loader, CustomStringConvertible { } +public class DictionaryLoader: Loader { + public let templates: [String: String] + + public init(templates: [String: String]) { + self.templates = templates + } + + public func loadTemplate(name: String, environment: Environment) throws -> Template { + if let content = templates[name] { + return environment.templateClass.init(templateString: content, environment: environment, name: name) + } + + throw TemplateDoesNotExist(templateNames: [name], loader: self) + } + + public func loadTemplate(names: [String], environment: Environment) throws -> Template { + for name in names { + if let content = templates[name] { + return environment.templateClass.init(templateString: content, environment: environment, name: name) + } + } + + throw TemplateDoesNotExist(templateNames: names, loader: self) + } +} + + extension Path { func safeJoin(path: Path) throws -> Path { let newPath = self + path diff --git a/Tests/StencilTests/LoaderSpec.swift b/Tests/StencilTests/LoaderSpec.swift index e4d6588..6e207d6 100644 --- a/Tests/StencilTests/LoaderSpec.swift +++ b/Tests/StencilTests/LoaderSpec.swift @@ -29,4 +29,27 @@ func testTemplateLoader() { try expect(try environment.loadTemplate(name: "../LoaderSpec.swift")).toThrow() } } + + describe("DictionaryLoader") { + let loader = DictionaryLoader(templates: [ + "index.html": "Hello World" + ]) + let environment = Environment(loader: loader) + + $0.it("errors when a template cannot be found") { + try expect(try environment.loadTemplate(name: "unknown.html")).toThrow() + } + + $0.it("errors when an array of templates cannot be found") { + try expect(try environment.loadTemplate(names: ["unknown.html", "unknown2.html"])).toThrow() + } + + $0.it("can load a template from a known templates") { + _ = try environment.loadTemplate(name: "index.html") + } + + $0.it("can load a known template from a collection of templates") { + _ = try environment.loadTemplate(names: ["unknown.html", "index.html"]) + } + } } diff --git a/docs/api.rst b/docs/api.rst index c080235..76c0a1a 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -73,6 +73,16 @@ on the file system. FileSystemLoader(bundle: [Bundle.main]) +DictionaryLoader +~~~~~~~~~~~~~~~~ + +Loads templates from a dictionary. + +.. code-block:: swift + + DictionaryLoader(templates: ["index.html": "Hello World"]) + + Custom Loaders ~~~~~~~~~~~~~~