{"id":37324434,"url":"https://github.com/elementary-swift/elementary","last_synced_at":"2026-01-16T03:27:01.992Z","repository":{"id":245142966,"uuid":"817388408","full_name":"elementary-swift/elementary","owner":"elementary-swift","description":"A modern and efficient HTML rendering library - inspired by SwiftUI, built for the web.","archived":false,"fork":false,"pushed_at":"2026-01-06T13:06:29.000Z","size":186,"stargazers_count":299,"open_issues_count":2,"forks_count":14,"subscribers_count":5,"default_branch":"main","last_synced_at":"2026-01-14T17:39:50.673Z","etag":null,"topics":["dsl","html","server-side-swift","swift","templating","web"],"latest_commit_sha":null,"homepage":"https://swiftpackageindex.com/elementary-swift/elementary","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/elementary-swift.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"github":["sliemeobn"]}},"created_at":"2024-06-19T15:43:57.000Z","updated_at":"2026-01-14T02:40:28.000Z","dependencies_parsed_at":"2024-08-12T10:35:15.822Z","dependency_job_id":"aecc173f-53fb-45b3-968c-8941be27c854","html_url":"https://github.com/elementary-swift/elementary","commit_stats":null,"previous_names":["sliemeobn/elementary","elementary-swift/elementary"],"tags_count":34,"template":false,"template_full_name":null,"purl":"pkg:github/elementary-swift/elementary","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elementary-swift%2Felementary","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elementary-swift%2Felementary/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elementary-swift%2Felementary/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elementary-swift%2Felementary/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/elementary-swift","download_url":"https://codeload.github.com/elementary-swift/elementary/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elementary-swift%2Felementary/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28477203,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-16T03:13:13.607Z","status":"ssl_error","status_checked_at":"2026-01-16T03:11:47.863Z","response_time":107,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dsl","html","server-side-swift","swift","templating","web"],"created_at":"2026-01-16T03:27:01.365Z","updated_at":"2026-01-16T03:27:01.978Z","avatar_url":"https://github.com/elementary-swift.png","language":"Swift","readme":"# Elementary: HTML Templating in Pure Swift\n\n**A modern and efficient HTML rendering library - inspired by SwiftUI, built for the web.**\n\n[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Felementary-swift%2Felementary%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/elementary-swift/elementary) [![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Felementary-swift%2Felementary%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/elementary-swift/elementary)\n\n\u003e [!IMPORTANT]\n\u003e \n\u003e This repo 📦 moved 📦 to its new home at **`https://github.com/elementary-swift`**.    \n\u003e Please update your URLs to avoid trouble - and reach out if anything breaks!\n\n\u003e [!NOTE]\n\u003e\n\u003e For running Swift apps in the browser, check out  **[ElementaryUI](https://elementary.codes/)**.\n\u003e \n\u003e   \u003ca href=\"https://elementary.codes\"\u003e\n\u003e    \u003cimg src=\"https://elementary.codes/og-image.png\" width=\"400px\" alt=\"Elementary Logo\"\u003e\n\u003e  \u003c/a\u003e\n\n\n[Examples](#play-with-it) | [Motivation](#motivation-and-other-packages) | [Discussion](https://github.com/elementary-swift/elementary/discussions)\n\n```swift\nstruct MainPage: HTMLDocument {\n    var title = \"Elementary\"\n\n    var head: some HTML {\n        meta(.name(.description), .content(\"Typesafe HTML in modern Swift\"))\n    }\n\n    var body: some HTML {\n        main {\n            h1 { \"Features\" }\n\n            FeatureList(features: [\n                \"HTML in pure Swift\",\n                \"SwiftUI-inspired composition\",\n                \"Lightweight and fast\",\n                \"Framework agnostic and unopinionated\",\n            ])\n\n            a(.href(\"https://github.com/elementary-swift/elementary\"), .class(\"fancy-style\")) {\n                \"Learn more\"\n            }\n        }\n    }\n}\n\nstruct FeatureList: HTML {\n    var features: [String]\n\n    var body: some HTML {\n        ul {\n            for feature in features {\n                li { feature }\n            }\n        }\n    }\n}\n```\n\n## Use it\n\nAdd the dependency to `Package.swift`\n```swift\n.package(url: \"https://github.com/elementary-swift/elementary.git\", from: \"0.6.0\")\n.product(name: \"Elementary\", package: \"elementary\")\n```\n\nIntegration with [Hummingbird](https://github.com/hummingbird-community/hummingbird-elementary)\n```swift\n.package(url: \"https://github.com/hummingbird-community/hummingbird-elementary.git\", from: \"0.3.0\")\n.product(name: \"HummingbirdElementary\", package: \"hummingbird-elementary\")\n```\n\nIntegration with [Vapor](https://github.com/vapor-community/vapor-elementary)\n```swift\n.package(url: \"https://github.com/vapor-community/vapor-elementary.git\", from: \"0.1.0\")\n.product(name: \"VaporElementary\", package: \"vapor-elementary\")\n```\n\n## Play with it\n\nCheck out the [Hummingbird + Tailwind example app](https://github.com/elementary-swift/elementary/tree/main/Examples/HummingbirdDemo).\n\nFor a demo of [ElementaryHTMX](https://github.com/elementary-swift/elementary-htmx), see this [Hummingbird + HTMX Demo](https://github.com/elementary-swift/elementary-htmx/tree/main/Examples/HummingbirdDemo).\n\nFor a Vapor example, see the [Vapor + HTMX Demo](https://github.com/elementary-swift/elementary-htmx/tree/main/Examples/VaporDemo).\n\n## Lightweight and fast\n\nElementary renders straight to text, optimized for serving generated HTML from a [Hummingbird](https://github.com/hummingbird-project/hummingbird) or [Vapor](https://vapor.codes/) server app.\n\nAny type conforming to `HTML` can be rendered individually, ideal for testing or for sending fragments with [htmx](https://github.com/elementary-swift/elementary-htmx).\n\nThe default rendering mechanism produces chunks of HTML for efficient response streaming, so the browser can start loading a page while the server is still producing the rest of it. Swift concurrency is used to handle back pressure, so you your memory footprint stays low even for large pages.\n\n```swift\n// Stream HTML, optimized for responsiveness and back pressure-aware\ntry await MainPage().render(into: responseStreamWriter)\n```\n\nAlternatively, you can simply collect the rendered HTML in a string.\n\n```swift\nlet html: String = div(.class(\"pretty\")) { \"Hello\" }.render()\n// \u003cdiv class=\"pretty\"\u003eHello\u003c/div\u003e\n\nlet fragment: String = FeatureList(features: [\"Anything conforming to HTML can be rendered\"]).render()\n// \u003cul\u003e\u003cli\u003eAnything conforming to HTML can be rendered\u003c/li\u003e\u003c/ul\u003e\n\n// For testing purposes, there is also a formatted version\nprint(\n    div {\n        p(.class(\"greeting\")) { \"Hi mom!\" }\n        p { \"Look how pretty.\" }\n    }.renderFormatted()\n)\n\n// \u003cdiv\u003e\n//   \u003cp class=\"greeting\"\u003eHi mom!\u003c/p\u003e\n//   \u003cp\u003eLook how pretty.\u003c/p\u003e\n// \u003c/div\u003e\n```\n\nElementary has zero dependencies (not even Foundation) and does not use runtime reflection or existential containers (there is not a single `any` in the code base).\n\nBy design, it does not come with a layout engine, reactive state tracking, or built-in CSS styling: it just renders HTML.\n\n## Clean and composable\n\nStructure your HTML with a SwiftUI-inspired composition API.\n\n```swift\nstruct List: HTML {\n    var items: [String]\n    var importantIndex: Int\n\n    var body: some HTML {\n        // conditional rendering\n        if items.isEmpty {\n            p { \"No items\" }\n        } else {\n            ul {\n                // list rendering\n                for (index, item) in items.enumerated() {\n                    // seamless composition of elements\n                    ListItem(text: item, isImportant: index == importantIndex)\n                }\n            }\n        }\n    }\n}\n\nstruct ListItem: HTML {\n    var text: String\n    var isImportant: Bool = false\n\n    var body: some HTML {\n        // conditional attributes\n        li { text }\n            .attributes(.class(\"important\"), when: isImportant)\n    }\n}\n```\n\n## First class attribute handling\n\nElementary utilizes Swift's powerful generics to provide an attribute system that knows what goes where. Every element knows which Tag it is for.\n\nAs in HTML, attributes go right after the \"opening tag\".\n\n```swift\n// staying close to HTML syntax really helps\ndiv(.data(\"hello\", value: \"there\")) {\n    a(.href(\"/swift\"), .target(.blank)) {\n        img(.src(\"/swift.png\"))\n        span(.class(\"fancy\")) { \"Click Me\" }\n    }\n}\n```\n\nAttributes can also be altered by using the modifier syntax, this allows for easy handling of conditional attributes.\n\n```swift\ndiv {\n    p { \"Hello\" }\n        .attributes(.id(\"maybe-fancy\"))\n        .attributes(.class(\"fancy\"), when: isFancy)\n}\n```\n\nBy exposing the tag type of `content`, attributes will fall through and be applied correctly.\n\n```swift\nstruct Button: HTML {\n    var text: String\n\n    // by exposing the HTMLTag type information...\n    var body: some HTML\u003cHTMLTag.input\u003e {\n        input(.type(.button), .value(text))\n    }\n}\n\ndiv {\n    // ... Button will know it really is an \u003cinput\u003e element ...\n    Button(text: \"Hello\")\n        .attributes(.autofocus) // ... and pass on any attributes\n}\n```\n\nAs a sensible default, _class_ and _style_ attributes are merged (with a blank space or semicolon respectively). All other attributes are overwritten by default.\n\n## Seamless async support\n\nElementary supports Swift Concurrency in HTML content. Simply `await` something inside your HTML, while the first bytes are already flying towards the browser.\n\n```swift\ndiv {\n    let text = await getMyData()\n    p { \"This totally works: \\(text)\" }\n    MyComponent()\n}\n\nstruct MyComponent: HTML {\n    var body: some HTML {\n        AsyncContent {\n            \"So does this: \\(await getMoreData())\"\n        }\n    }\n}\n```\n\nBy using the `AsyncForEach` element, any `AsyncSequence` can be efficiently rendered straight to HTML.\n\n```swift\nul {\n    // the full result never needs to be stored in memory...\n    let users = try await db.users.findAll()\n    // ...as each async sequence element...\n    AsyncForEach(users) { user in\n        // ...is immediately streamed out as HTML\n        li { \"\\(user.name) \\(user.favoriteProgrammingLanguage)\" }\n    }\n}\n```\n\n## Enviroment values\n\nElementary utilizes `TaskLocal`s to provide a light-weight environment system.\n\n```swift\nenum MyValues {\n    // task-locals act as keys, ...\n    @TaskLocal static var userName = \"Anonymous\"\n}\n\nstruct MyComponent: HTML {\n    // ... their values can be accessed ...\n    @Environment(MyValues.$userName) var userName\n\n    var body: some HTML {\n        p { \"Hello, \\(userName)!\" }\n    }\n}\n\ndiv {\n    // ... and provided in a familiar way\n    MyComponent()\n        .environment(Values.$userName, \"Drax the Destroyer\")\n}\n```\n\n### 🚧 Work in progress 🚧\n\nThe list of built-in attributes is far from complete, but adding them is really simple (and can be done in external packages as well).\n\nFeel free to open a PR with additional attributes that are missing from the model.\n\n## Motivation and other packages\n\n[Plot](https://github.com/JohnSundell/Plot), [HTMLKit](https://github.com/vapor-community/HTMLKit), and [Swim](https://github.com/robb/Swim) are all excellent packages for doing a similar thing.\n\nMy main motivation for Elementary was to create an experience like these ([Swift Forums post](https://forums.swift.org/t/elementary-a-modern-and-efficient-html-rendering-library-inspired-by-swiftui-built-for-the-web/72647) for more context), but\n\n- stay true to HTML tag names and conventions (including the choice of lowercase types)\n- avoid allocating an intermedate structure and go straight to streaming HTML\n- using generics to stay away from allocating a ton of lists of existential `any`s\n- have a list of attributes go before the content block\n- provide _attribute fallthrough_ and merging\n- zero dependencies on other packages\n\n[Tokamak](https://github.com/TokamakUI/Tokamak) is an awesome project and very inspiring. It can produce HTML, but it's main focus is on a very different beast. Check it out!\n\n[swift-html](https://github.com/pointfreeco/swift-html) and [swift-dom](https://github.com/tayloraswift/swift-dom) will produce HTML nicely, but they use a different syntax for composing HTML elements.\n\n---\n\n*SwiftUI is a trademark of Apple Inc. This project is not affiliated with or connected to Apple in any way.*\n","funding_links":["https://github.com/sponsors/sliemeobn"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felementary-swift%2Felementary","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Felementary-swift%2Felementary","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felementary-swift%2Felementary/lists"}