swift-foundations/swift-html-render
Renders declarative HTML.View trees to escaped HTML markup as a String or UTF-8 bytes.
Key Features
- Declarative view tree — Build markup from
HTML.Viewcomponents with a@HTML.Builderbody, composed like SwiftUI. - Automatic escaping — Text content is escaped on render (
&,<,>; attribute values additionally escape"and'), so interpolated data cannot break out of its context. - Typed throws —
Stringand byte conversions throw a single typedHTML.Context.Configuration.Error. - String or bytes output — Render the same view to
String,[UInt8], orContiguousArray<UInt8>. - Asynchronous rendering —
await String(view)andawait UInt8for non-blocking serialization. - Complete documents —
HTML.Documentemits a full page with doctype,<html>,<head>, and<body>. - Format-agnostic core —
HTML.ViewrefinesRender.View, so one view tree can target anyRender.Contextoutput.
Quick Start
A view's text is escaped when it is rendered, so user-supplied content is safe to embed directly — naive string interpolation would inject the <b> markup verbatim:
import HTML_Rendering
// A reusable component in the view tree.
struct Comment: HTML.View {
let author: String
let message: String
var body: some HTML.View {
HTML.Group {
HTML.Text(author)
HTML.Text(": ")
HTML.Text(message)
}
}
}
let comment = Comment(author: "Ada", message: "<b>hi</b>")
let markup = try String(comment)
// markup == "Ada: <b>hi</b>"Installation
dependencies: [
.package(url: "https://github.com/swift-foundations/swift-html-render.git", branch: "main")
].target(
name: "YourTarget",
dependencies: [
.product(name: "HTML Rendering", package: "swift-html-render")
]
)Requires Swift 6.3.1 and macOS 26 / iOS 26 / tvOS 26 / watchOS 26 / visionOS 26.
Architecture
Five library products. The umbrella HTML Rendering is the default import; the other targets let consumers narrow the surface.
| Product | Import | When to import | |---------|--------|----------------| | HTML Rendering | HTML_Rendering | Default. Element builders, attribute helpers, and the rendering engine in one import. | | HTML Elements Rendering | HTML_Elements_Rendering | Typed element builders with attribute helpers (transitively includes Attributes and Core). | | HTML Attributes Rendering | HTML_Attributes_Rendering | Attribute application and the engine, without the standard element builders. | | HTML Rendering Core | HTML_Rendering_Core | The engine alone — HTML.View, HTML.Text, HTML.Document, escaping, and String / byte serialization — without standard elements or attributes. | | HTML Rendering Core Test Support | HTML_Rendering_Core_Test_Support | Test-target helpers (a public tag(_:) builder, element constructors, and a TestProperty) for asserting rendered output. |
Community
Discussion thread will be created at first public release.
License
Apache 2.0. See LICENSE.
Package Metadata
Repository: swift-foundations/swift-html-render
Default branch: main
README: README.md