Swift template engine
TemplateDSL will help you
- code generation
- html pages
- any complex strings
Add dependency to Package.swift
...
.package(url: "https://github.com/Alexander-Ignition/swift-template-dsl", from: "0.0.1"),
... and your target
.target(name: "ExampleApp", dependencies: [
.product(name: "TemplateDSL", package: "swift-template-dsl")
]),
Two simple steps:
- write yout Template.
import TemplateDSL
struct Page: Template {
let items = Array(0..<3)
var body: [Template] {
Text("Header")
ForEach(items) { item in
Indent {
Text("- item: \(item)")
}
}
Text("Footer")
}
}
- render Template.
let page = Page()
let string = page.render()
print(string)
/*
Header
- item: 0
- item: 1
- item: 2
Footer
*/
You can use the SwiftUI preview to quickly view the templates.
See the more complex example in Sources/TemplateExample/Colors.swift
#if canImport(SwiftUI) && DEBUG
import SwiftUI
struct Page_Previews: PreviewProvider {
private static let string = Page().render()
static var previews: some View {
TextField("None", text: .constant(string)).padding()
}
}
#endif