Very basic Swift templating engine. macOS and Linux ready.
Templater is a very simple tool aimed at solving the simple task: in a string template, fill in a few variables with certain values. Something like Stencil or Mustache. Just a super simple version of them.
I needed to fill in a large markdown file with fresh data every day, to generate a pretty report. The existing libraries seemed like an overkill for the simple usecase, so I wrote a lightweight (literally less than 100 lines of code) solution myself.
The template string must contain one or more strings like {{ variable_name }}
, such as Hello, {{ name }}!
. When you render this template with the context of name = "world"
, you'll get Hello, world!
.
do {
//create the template
let template = Template("Hello, {{ name }}! Today is {{ day }} and it will be {{ weather }}.")
//have your context
let context = [
"name": "Tim",
"day": "Thursday",
"weather": "sunny"
]
//render the context
let result = try template.fill(with: context)
//result: "Hello, Tim! Today is Thursday and it will be sunny."
} catch {
print("Template error: \(error)")
}
.Package(url: "https://github.com/czechboy0/Templater.git", majorVersion: 0, minor: 2)
Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.
Please create an issue with a description of your problem or open a pull request with a fix.
MIT
Honza Dvorsky - http://honzadvorsky.com, @czechboy0