Files
swiftpm-mustache/Sources/HummingbirdMustache/Template.swift
Adam Fowler 071b182a00 2.x.x Template struct (#22)
* Start of turning template into a struct

* Everything is Sendable now, just doesnt work

* Add library to context

* Make sure render is initialized with library

* comment about inheritance spec

* Add register back in

* Re-instate register functions

* Re-instate commented out print

* Fix tabbing in Partial tests

* Make HBMustacheLibrary.loadTemplates async

* Update platforms, swift version
2024-01-31 15:43:46 +00:00

48 lines
1.7 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Hummingbird server framework project
//
// Copyright (c) 2021-2021 the Hummingbird authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See hummingbird/CONTRIBUTORS.txt for the list of Hummingbird authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
/// Class holding Mustache template
public struct HBMustacheTemplate: Sendable {
/// Initialize template
/// - Parameter string: Template text
/// - Throws: HBMustacheTemplate.Error
public init(string: String) throws {
self.tokens = try Self.parse(string)
}
/// Render object using this template
/// - Parameter object: Object to render
/// - Returns: Rendered text
public func render(_ object: Any, library: HBMustacheLibrary? = nil) -> String {
self.render(context: .init(object, library: library))
}
internal init(_ tokens: [Token]) {
self.tokens = tokens
}
enum Token: Sendable {
case text(String)
case variable(name: String, transform: String? = nil)
case unescapedVariable(name: String, transform: String? = nil)
case section(name: String, transform: String? = nil, template: HBMustacheTemplate)
case invertedSection(name: String, transform: String? = nil, template: HBMustacheTemplate)
case inheritedSection(name: String, template: HBMustacheTemplate)
case partial(String, indentation: String?, inherits: [String: HBMustacheTemplate]?)
case contentType(HBMustacheContentType)
}
var tokens: [Token]
}