Ignite is a static site builder for Swift developers, offering an expressive, powerful API to build beautiful websites that work great on all devices.
Ignite doesn't try to convert SwiftUI code to HTML, or simply map HTML tags to Swift code. Instead, it aims to use SwiftUI-like syntax to help you build great websites even if you have no knowledge of HTML or CSS.
The easiest way to get started is to use the Ignite command-line tool included with this package:
- Run
git clone https://github.com/twostraws/Ignite
to clone this repository to your computer. - Change into the new directory, e.g.
cd Ignite
. - Now run
make
to build the Ignite command-line tool. - Then run
make install
to install the Ignite command-line tool to/usr/local/bin
. - If that command fails because of permissions issues, you should run
sudo make install
instead.
Note
To change install directory: make install PREFIX_DIR=/my/install/dir
Once that command-line tool is installed, you can run the following command to create a new site called ExampleSite:
ignite new ExampleSite
Once installed, the command-line tool is helpful for running a local web server for testing and for building your project.
Tip
Using the Ignite tool to run a local web server is the best way to preview your site.
Alternatively, you can bring Ignite into an existing project using Swift Package Manager by adding a package dependency for https://github.com/twostraws/Ignite.
Once that completes, import Ignite into your Swift code wherever needed:
import Ignite
Once you've built your site and are ready to see how it looks, do not just double-click one of the files in Finder. This will open the file directly in your browser, which means it won't know how to locate the rest of your site – the stylesheets, JavaScript code, etc – so it will not display correctly.
Instead, the best way to preview your site is using the Ignite CLI tool, which you installed in Getting Started above:
- Run
ignite run --preview
to preview your site and open it in your web browser. - If Ignite tells you there is already a web server running on that port, run
ignite run --preview --force
.
That will open your web browser straight to your site. You can then return to Xcode and make changes to your site freely – every time you press Cmd+R to build your site, you can refresh your browser to see the changes.
The IgniteSamples repository contains lots of sample code for you to try out – you can see it running here: You can see all the output from this repository running here: https://ignitesamples.hackingwithswift.com.
Basic Ignite code looks similar to SwiftUI code:
Text("Swift rocks")
.font(.title1)
Text(markdown: "Add *inline* Markdown")
.foregroundStyle(.secondary)
Link("Swift", target: "https://www.swift.org")
.linkStyle(.button)
Divider()
Image("logo.jpg")
.accessibilityLabel("The Swift logo.")
.padding()
But it also includes a range of more advanced controls such as dropdown buttons:
Dropdown("Click Me") {
Link("Accordions", target: AccordionExamples())
Link("Carousels", target: CarouselExamples())
Divider()
Text("Or you can just…")
Link("Go back home", target: "/")
}
.role(.primary)
It includes accordions that show or hide items based on what is selected:
Accordion {
Item("First", startsOpen: true) {
Text("This item will start open by default.")
}
Item("Second") {
Text("This is the second accordion item.")
}
Item("Third") {
Text("This is the third accordion item.")
}
}
.openMode(.individual)
It has automatic code syntax highlighting for a dozen languages:
CodeBlock(language: "swift", """
struct ContentView: View {
var body: some View {
Text("Hello, Swift!")
}
}
""")
Plus carousels, badges, alerts, tables, and so much more.
There is a separate repository called IgniteSamples, which provides sample code for a wide variety of protocols, elements, and modifiers used by Ignite.
If you're looking for code to help you get started, that's the best place – you can build that site and run it locally, the copy and paste any code you want to try.
Ignite sites are just Swift package, but they use a specific folder structure to help build your site effectively.
- Assets: This is where your custom site assets should be placed, using whatever subfolders you want.
- Build: This is created automatically by Ignite whenever you build your site. Do not place important information here, because it will be deleted on your next build.
- Content: This is where you want to place any Markdown files for posts you want, again using any subfolder structure you want. (Optional)
- Includes: This is where you place any custom HTML you've written that you want to include. (Optional)
- Sources: This is where you'll place all your Swift code for your site, using any subfolder structure that suits you.
This folder structure is already in place in the Ignite Starter Template repository, and I recommend you start with that.
Adding Markdown files to Content will render these to HTML pages and include them in Build with their respective folder structure, minus the Content part.
For example, adding a new file called apps.md
to Content means having this folder structure:
├── Content
│ └── apps.md
And it results inn this Build structure:
├── Build
│ ├── …
│ ├── apps
│ │ └── index.html
│ ├── …
A precondition for this to work is to have a layout available to render your content. If you don't have a valid layout in place, Ignite will issue a warning saying "Your site must provide at least one layout in order to render Markdown."
You can create custom content layouts by making types conform to the ContentLayout
protocol, which will automatically be given a content
property to access the content it is displaying. For example:
import Foundation
import Ignite
struct CustomContentLayout: ContentLayout {
var body: some HTML {
Text(content.title)
.font(.title1)
if let image = content.image {
Image(image, description: content.imageDescription)
.resizable()
.cornerRadius(20)
.frame(maxHeight: 300)
}
if content.hasTags {
Group {
Text("Tagged with: \(content.tags.joined(separator: ", "))")
Text("\(content.estimatedWordCount) words; \(content.estimatedReadingMinutes) minutes to read.")
}
}
Text(content.body)
}
}
Once you've defined a custom layout, you should add it to your Site
struct. This can be done by adding this new layout to the layouts
property of the site, like this:
struct ExampleSite: Site {
var name = "Hello World"
var url = URL(static: "https://www.example.com")
var homePage = Home()
var layout = MyLayout()
/* This part adds the custom layout */
var layouts: [any ContentLayout] {
CustomContentLayout()
}
}
Once you have installed the Ignite command-line tool from this repository, you can use it in various ways.
First, you can create new site like this:
ignite new YourSite
When that completes, it will tell you the commands to use to open your new site for editing in Xcode:
cd YourSite
open Package.swift
Tip
If you want to build with Xcode, go to the Product menu and choose Destination > My Mac.
Back in your terminal window, once you have run that cd
command the current working directory of your terminal is your website's directory. This means you can run the following command to build your site, rather than using Xcode:
ignite build
That will convert all your Swift code to HTML in your Build folder.
You can also run this command:
ignite run --preview
That will launch a local web server you should use to preview your site, and also open it in your browser. If you're working in Xcode, you can continue performing builds as normal then refresh your browser to see your changes.
Tip
The Ignite command-line tool has various configuration options available. Run ignite help
to get general help, or add help
before a subcommand to get further details, e.g. ignite help run
.
Website | Repository |
---|---|
jcalderita Portfolio | GitHub |
Fotogroep de Gender | GitHub |
sookim-1's T.W.L | GitHub |
I welcome all contributions, whether that's adding new tests, fixing up existing code, adding comments, or improving this README – everyone is welcome!
- You must comment your code thoroughly, using documentation comments or regular comments as applicable.
- Please ensure you run SwiftLint in the Sources directory, and fix all outstanding issues.
- All code must be licensed under the MIT license so it can benefit the most people.
- Ensure you build IgniteSamples using your modified copy of Ignite, and compare it to the live version.
- If you create a new element, please consider adding it to the IgniteSamples repository, so folks can see it more easily.
Ignite is an open-source project with many contributors, including Paul Hudson, JP Toro, Henrik Christensen, Michael Freiwald, and Jobert Sá.
All contributions are welcome, but please ensure you abide by the Code of Conduct.
MIT License.
Copyright (c) 2024 Paul Hudson.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Ignite was originally created by Paul Hudson, who writes free Swift tutorials over at Hacking with Swift. It’s available under the MIT license, which permits commercial use, modification, distribution, and private use.