Add development support for reloading templates when you render them (#30)

* Add support for reloading templates when you render them

* comment

* Ensure reload is only available in DEBUG

* move preprocessor block

* swift format

* MustacheTemplate.init?(filename:) internal

* Only pass reload flag down in DEBUG builds

* Rebase with main
This commit is contained in:
Adam Fowler
2024-07-16 15:46:57 +01:00
committed by GitHub
parent 7689de0a42
commit 5bb66ac425
7 changed files with 153 additions and 22 deletions

View File

@@ -2,7 +2,7 @@
//
// This source file is part of the Hummingbird server framework project
//
// Copyright (c) 2021-2021 the Hummingbird authors
// Copyright (c) 2021-2024 the Hummingbird authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
@@ -27,17 +27,14 @@ extension MustacheLibrary {
var templates: [String: MustacheTemplate] = [:]
for case let path as String in enumerator {
guard path.hasSuffix(extWithDot) else { continue }
guard let data = fs.contents(atPath: directory + path) else { continue }
let string = String(decoding: data, as: Unicode.UTF8.self)
var template: MustacheTemplate
do {
template = try MustacheTemplate(string: string)
guard let template = try MustacheTemplate(filename: directory + path) else { continue }
// drop ".mustache" from path to get name
let name = String(path.dropLast(extWithDot.count))
templates[name] = template
} catch let error as MustacheTemplate.ParserError {
throw ParserError(filename: path, context: error.context, error: error.error)
}
// drop ".mustache" from path to get name
let name = String(path.dropLast(extWithDot.count))
templates[name] = template
}
return templates
}