Swiftpack.co - apparata/TemplateKit as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by apparata.
apparata/TemplateKit 0.7.2
A simple templating library written in Swift.
⭐️ 4
🕓 21 weeks ago
iOS macOS
.package(url: "https://github.com/apparata/TemplateKit.git", from: "0.7.2")

TemplateKit

Simple Value

let template: Template = "This is a <{ thing }>"

try template.render(context: [
    "thing": "test"
])

Simple Value with Transformer

let template: Template = "This is an uppercase <{ #uppercased thing }>"

try template.render(context: [
    "thing": "Test"
])

Nested Value

let template: Template = "This fruit is named <{ fruit.name }>"

struct Fruit {
    let name: String
}

try template.render(context: [
    "fruit": Fruit(name: "banana")
])

Built-in Transformers

  • #lowercased
  • #uppercased
  • #uppercasingFirstLetter
  • #lowercasingFirstLetter
  • #trimmed
  • #removingWhitespace
  • #collapsingWhitespace

Conditionals

if ... ... end

let template: Template = """
<{ if isMonday }>
This is only kept if isMonday is true.
<{ #uppercased whatever }>
Some more text.
<{ end }>
"""

try template.render(context: [
    "whatever": "Test",
    "isMonday": true
])

if ... ... else ... end

let template: Template = """
<{ if thing }>
Thing is true.
<{ else }>
Thing is false.
<{ end }>
"""

try template.render(context: [
    "thing": true
])

Conditional Expression

The conditional expression can be more complicated than a simple boolean value. Here is the complete grammar for the conditional expression:

expr      := term ('or' term)*
term      := factor ('and' factor)*
factor    := 'not'? ( statement | '(' expr ')' )
statement := terminal ( ('==' | '!=') string )?
string    := ('"' | ''') character* ('"' | ''')

Example: if not ...

let template: Template = """
<{ if not isMonday }>
Today is not Monday.
<{ end }>
"""

try template.render(context: [
    "isMonday": true
])

Example: if not (... and ...)

let template: Template = """
<{ if not (isMonday and isRaining) }>
Today is not Monday and it is not raining.
<{ end }>
"""

try template.render(context: [
    "isMonday": true,
    "isRaining": true
])

Example: if ... == "..."

let template: Template = """
<{ if license == "MIT" }>
This is the MIT license.
<{ end }>
"""

try template.render(context: [
    "license": "MIT"
])

Loops: for ... in ...

let template: Template = """
Here are some types of fruits:
<{ for fruit in fruits }>
Fruit name: <{ fruit }>
<{ end }>
"""

try template.render(context: [
    "fruits": [
        "banana",
        "orange",
        "pineapple",
        "pear"
    ]
])

Import Templates

let template: Template = """
<{ import "../Common/Content.txt" }>
"""

Longer Example:

let template: Template = """
This is a test.
<{ #lowercased whatever }>
So is this.
<{ stuff }>
Whatever
<{ if potato }>
    Only if potato is true.
    <{ if cucumber }>
        Only if both potato and cucumber are true.
    <{ end }>
<{ end }>
Here are some types of fruits:
<{ for fruit in fruits }>
Fruit name: <{ fruit }>
<{ end }>

Here they are again:
<{ for fruit in fruits }>Fruit name: <{ car }> <{ end }>
"""

let context: [String: Any?] = [
    "whatever": "[WHATEVER]",
    "stuff": 1337,
    "potato": true,
    "cucumber": 1,
    "fruits": [
        "banana",
        "orange",
        "pineapple",
        "pear"
    ]
]

try template.render(context: context)

GitHub

link
Stars: 4
Last commit: 21 weeks ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

0.6.0: Add "import" node
21 weeks ago

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