A cross-platform Swift package to render any Swift type as HTML.
PointFreeHTML enables any Swift type to be rendered as HTML through a simple protocol conformance. Other libraries use it to render their types to HTML.
- Universal HTML Protocol: Any Swift type can be rendered as HTML by conforming to the
HTMLprotocol - Performance Focused:
- Zero-copy byte rendering (~3,500 docs/sec)
- RFC pattern with bytes as canonical representation
- Optimized attribute escaping with fast-path detection
- Capacity pre-allocation for known document sizes
- Declarative Syntax: SwiftUI-like syntax with
@Builderresult builder - Type Safety: Compile-time checking prevents malformed HTML
- Composable Components: Build complex UIs from reusable components
- Minimal Dependencies: Core library has minimal external dependencies
import PointFreeHTML
struct Greeting: HTML.View {
let name: String
var body: some HTML.View {
tag("h1") { "Hello, \(name)!" }
}
}
let greeting = Greeting(name: "World")
// Render to String (validates UTF-8)
let htmlString = try String(greeting)
// Render to bytes (zero-copy, canonical)
let htmlBytes = ContiguousArray(greeting)
// Or use Array (convenience wrapper)
let htmlArray: [UInt8] = Array(greeting)
// Legacy API (deprecated but still supported)
// let htmlBytes = greeting.render() // ⚠️ DeprecatedPointFreeHTML follows the RFC pattern where bytes are canonical. All rendering goes through ContiguousArray<UInt8> (zero-copy) or Array<UInt8> (convenience), with String derived from bytes via validation.
For comprehensive examples of building HTML elements and components, see swift-html, which provides a complete developer experience built on top of PointFreeHTML.
See swift-html-css-pointfree for an example of how third-party libraries can integrate PointFreeHTML as their rendering engine.
PointFreeHTML integrates seamlessly with the broader Swift web development ecosystem:
swift-html builds on PointFreeHTML to provide domain-accurate HTML and CSS integration and additional convenience APIs:
import HTML // This imports swift-html which includes PointFreeHTML
struct StyledComponent: HTML.View {
var body: some HTML.View {
tag("div") {
tag("a") { "Styled Heading" }
.attribute("href", "#")
.inlineStyle("color", "blue")
.inlineStyle("font-size", "24px")
.inlineStyle("margin-bottom", "16px")
}
}
}PointFreeHTML works with Swift server frameworks like Vapor:
import Vapor
import PointFreeHTML
app.get("hello", ":name") { req -> String in
let name = req.parameters.get("name") ?? "World"
struct Greeting: HTML.View {
let name: String
var body: some HTML.View {
tag("h1") { "Hello, \(name)!" }
}
}
return try String(Greeting(name: name))
}Add PointFreeHTML to your Package.swift:
dependencies: [
.package(url: "https://github.com/coenttb/pointfree-html", from: "0.1.0")
],
targets: [
.target(
name: "YourTarget",
dependencies: [
.product(name: "PointFreeHTML", package: "pointfree-html")
]
)
]Add the package dependency in Xcode:
- File → Add Package Dependencies
- Enter:
https://github.com/coenttb/pointfree-html
PointFreeHTML includes support for snapshot testing:
import PointFreeHTMLTestSupport
@Test
func testMyComponent() {
let component = Greeting(name: "Coen ten Thije Boonkkamp")
assertInlineSnapshot(of: component, as: .html) {
"""
<h1>Hello, Coen ten Thije Boonkkamp!</h1>
"""
}
}PointFreeHTML powers production applications:
- coenttb.com: Personal website built entirely with PointFreeHTML
- coenttb-com-server: Open-source backend demonstrating full-stack Swift
- coenttb-blog: A Swift package for blog functionality with HTML generation.
- coenttb-web: A Swift package with tools for web development building on swift-web.
- pointfree-html-to-pdf: A Swift package integrating pointfree-html with swift-html-to-pdf.
- pointfree-html-translating: A Swift package integrating pointfree-html with swift-translating.
- swift-html: The Swift library for domain-accurate and type-safe HTML & CSS.
- swift-html-css-pointfree: A Swift package integrating swift-html-standard and swift-css-types with pointfree-html.
- swift-html-prism: A Swift package integrating PrismJS with swift-html.
- swift-web-foundation: A Swift package with tools to simplify web development.
- pointfreeco/swift-dependencies: A dependency management library for controlling dependencies in Swift.
- pointfreeco/swift-snapshot-testing: Delightful snapshot testing for Swift.
- apple/swift-collections: Commonly used data structures for Swift.
PointFreeHTML is part of a comprehensive Swift web development ecosystem:
- swift-html: Type-safe HTML & CSS DSL built on PointFreeHTML -** use this for examples and full developer experience**
- swift-html-css-pointfree: Integration layer combining PointFreeHTML with CSS types - use this as example for third-party library integration
- swift-html-standard: Complete Swift domain model of HTML elements and attributes
- swift-css-types: Complete Swift domain model of CSS properties and types
- coenttb-html: Extensions for HTML, Markdown, Email, and PDF generation
- pointfree-html-to-pdf: Convert HTML to PDF on iOS and macOS
- swift-web: Modular web development tools
- coenttb-web: Feature collection for Swift servers
- coenttb-server: Modern Swift server framework
- coenttb-server-vapor: Vapor integration
- swift-languages: Cross-platform translation library
Comprehensive documentation is available at the Swift Package Index.
This project builds upon the foundational work by Point-Free (Brandon Williams and Stephen Celis). PointFreeHTML is a fork and adaptation of their original swift-html library.
Contributions are welcome! Please feel free to:
- Open issues for bugs or feature requests
- Submit pull requests
- Improve documentation
- Share your projects built with PointFreeHTML
- Issues: GitHub Issues
- Newsletter: Subscribe
- Social: Follow @coenttb
- Professional: LinkedIn
PointFreeHTML is licensed under the MIT License. See LICENSE for details.