Friday, August 12, 2022
HomeiOS DevelopmentConstructing stylesheets utilizing Leaf - The.Swift.Dev.

Constructing stylesheets utilizing Leaf – The.Swift.Dev.

A fast CSS demo mission

The very first step is so as to add Leaf as a dependency to your mission. You need to observe that Leaf 4 will not be completed but and these model new options are solely out there from the tau pre-release.

import PackageDescription

let package deal = Package deal(
    title: "myProject",
    platforms: [
    dependencies: [
        .package(url: "", from: "4.32.0"),
        .package(url: "", .exact("4.0.0-tau.1")),
        .package(url: "", .exact("1.0.0-tau.1.1")),
    targets: [
        .target(name: "App", dependencies: [
            .product(name: "Leaf", package: "leaf"),
            .product(name: "Vapor", package: "vapor"),
        .goal(title: "Run", dependencies: ["App"]),
        .testTarget(title: "AppTests", dependencies: [
            .target(name: "App"),
            .product(name: "XCTVapor", package: "vapor"),

We’re able to import Leaf in your Swift recordsdata, since there’s a new LeafFileMiddleware out there as a part of Leaf we’ll create some publicly out there template recordsdata and use this middleware to render them. Create a brand new Public listing inside the foundation folder of the mission and place an new index.html file there. You can even use a .leaf extension, however for the sake of simplicity (and Xcode syntax highlighting causes) we’ll use the .html extension this time.

<!DOCTYPE html>
    <meta charset="utf-8">
    <meta title="viewport" content material="width=device-width, initial-scale=1">
    <hyperlink rel="stylesheet" href="/css/model.css">

Fairly fundamental HTML5 boilerplate code, besides that we’ll print the title utilizing a Leaf tag. We’ll set a worth for this context variable by some Swift code in a second. Within the head part we additionally import our css/model.css stylesheet file. Now it’s best to create a css folder contained in the Public listing and place a model.css file within it.

* {
    margin: 0;
    padding: 0;
physique {
    font-family: -apple-system, system-ui, BlinkMacSystemFont, "Helvetica", "Segoe UI", Roboto, Ubuntu;
    font-size: 16px;
    line-height: 1.4em;
    background: #(background);
h1 {
    padding: #(padding);
@media (max-width: 599px) {}
@media (min-width: 600px) {}
@media (min-width: 900px) {}
@media (min-width: 1200px) {}
@media (min-width: 1800px) {}

Since this file is “secretly” a leaf template file we will use the #(variable) syntax to print out values. We’re going to move a background shade key and a padding key with some customized values as context variables.

Now let me present you learn how to configure this new LeafFileMiddleware, so we will render each our html and css templates.

import Vapor
import Leaf
public func configure(_ app: Utility) throws {

    if !app.surroundings.isRelease {
        LeafRenderer.Possibility.caching = .bypass

    LeafFileMiddleware.defaultMediaType = .html
    LeafFileMiddleware.processableExtensions = ["leaf", "html", "css", "js"]
    LeafFileMiddleware.contexts = [
        .css: [
            "background": "#eee",
            "padding": "16px",
        .html: [
            "title": "Hello world!"
    if let lfm = LeafFileMiddleware(publicDirectory: app.listing.publicDirectory) {

First we disable the cache, however that is a reasonably apparent chunk of code, subsequent we set the default media kind to html. This might be used to set the Content material-Kind header if the file extension within the request is an unknown kind. The processableExtensions property will inform the LeafFileMiddleware to course of and render solely these recordsdata, every thing else with a unique extension might be streamed identical to once you use an everyday FileMiddleware.

As you possibly can see we will set totally different context values for particular media varieties, in our case all of the css recordsdata can use the background and padding properties and each html file can reap the benefits of the title context variable. Additionally it is attainable to set them by a subscript syntax:

LeafFileMiddleware[.css] = [
    "background": "green",
    "padding": "16px",

LeafFileMiddleware[.html] = [
    "title": "Hello world!"

The final step is to create the precise middleware with a publicDirectory argument. This listing is the placement the place the system will search for publicly out there recordsdata and if wanted they are often processed as common Leaf templates. You can even setup listing indexing by the LeafFileMiddleware, however that is a unique matter.

If you happen to navigate to the http://localhost:8080/index.html handle it’s best to see your rendered index.html file with the best stylesheet utilized to it. In fact you possibly can register a customized route and render your templates utilizing the standard Assets / Views location if wanted, however I simply wished to indicate you this cool trick, because it permits us to serve public recordsdata utilizing a extra dynamic strategy.



Please enter your comment!
Please enter your name here

Most Popular