Swiftpack.co -  loopwerk/SagaStencilRenderer as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
loopwerk/SagaStencilRenderer
A renderer for Saga that uses Stencil to turn a RenderingContext into a String
.package(url: "https://github.com/loopwerk/SagaStencilRenderer.git", from: "0.2.0")

SagaStencilRenderer

A renderer for Saga that uses Stencil to turn a RenderingContext into a String.

It comes with a free function named stencil which takes an HTML template and a Stencil Environment, and returns a function that goes from RenderingContext to String, which can then be plugged into Saga's writers.

Example

Package.swift

// swift-tools-version:5.2

import PackageDescription

let package = Package(
  name: "Example",
  platforms: [
    .macOS(.v10_15)
  ],
  dependencies: [
    .package(url: "https://github.com/loopwerk/Saga", from: "0.21.0"),
    .package(url: "https://github.com/loopwerk/SagaParsleyMarkdownReader", from: "0.4.0"),
    .package(url: "https://github.com/loopwerk/SagaStencilRenderer", from: "0.2.0")
  ],
  targets: [
    .target(
      name: "Example",
      dependencies: [
        "Saga",
        "SagaParsleyMarkdownReader",
        "SagaStencilRenderer"
      ]
    ),
  ]
)

main.swift:

import Foundation
import Saga
import PathKit
import SagaParsleyMarkdownReader
import SagaStencilRenderer
import Stencil

let saga = try Saga(input: "content", siteMetadata: EmptyMetadata())

try saga.register(
    metadata: EmptyMetadata.self,
    readers: [.parsleyMarkdownReader()],
    itemWriteMode: .keepAsFile,
    writers: [
      .itemWriter(stencil("page.html", environment: getEnvironment(root: saga.rootPath)))
    ]
  )
  .run()
  .staticFiles()

func getEnvironment(root: Path) -> Environment {
  Environment(loader: FileSystemLoader(paths: [root + "templates"]))
}

You can extend the Environment with your own tags and filters, see the official Stencil docs. This is why you need to pass in an Environment, instead of SagaStencilRenderer creating one for you.

For example:

func getEnvironment(root: Path) -> Environment {
  let ext = Extension()
  
  ext.registerFilter("url") { (value: Any?) in
    guard let item = value as? AnyItem else {
      return ""
    }
    var url = "/" + item.relativeDestination.string
    if url.hasSuffix("/index.html") {
      url.removeLast(10)
    }
    return url
  }

  return Environment(loader: FileSystemLoader(paths: [root + "templates"]), extensions: [ext])
}

GitHub

link
Stars: 2
Last commit: 29 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics