NPRPC docs

class TemplateLibrary

Swift

final class TemplateLibrary

Loads a directory of .mustache files and renders from it.

swift-mustache ships MustacheLibrary(directory:), but it is async (so it cannot be called from the server's synchronous bootstrap) and its reload: flag is compiled out of release builds and trips a preconditionFailure on templates that did not come from a file. Walking the directory here keeps startup synchronous and makes hot reload work the same way in both configurations.

nprpc_swift/Sources/NPRPCWeb/TemplateLibrary.swift:15

Constructors

init(directory: String, hotReload: Bool = false) throws

Loads every .mustache file under directory. Throws TemplateError if the directory is unreadable, holds no templates, or one fails to parse.

directory
root of the template tree. A template's name is its path below this directory without the .mustache extension, so partials/post_cards.mustache is {{> partials/post_cards}}.
hotReload
re-read the tree on every render. A handful of small file reads per request — fine for development, waste in production.

nprpc_swift/Sources/NPRPCWeb/TemplateLibrary.swift:31

Properties

var templateNames: [String] { get }

Template names currently loaded, for startup logging.

nprpc_swift/Sources/NPRPCWeb/TemplateLibrary.swift:53

Methods

func render(_ object: Any, withTemplate name: String) -> String?

Render object with the named template, or nil if no such template.

nprpc_swift/Sources/NPRPCWeb/TemplateLibrary.swift:38