Stencil

Stencil 是一个简单而强大的 Swift 模板语言。它提供了类似于 Django 和 Mustache 的语法。如果您熟悉这些,您会很快熟悉 Stencil。

示例

There are {{ articles.count }} articles.

<ul>
  {% for article in articles %}
    <li>{{ article.title }} by {{ article.author }}</li>
  {% endfor %}
</ul>
import Stencil

struct Article {
  let title: String
  let author: String
}

let context = [
  "articles": [
    Article(title: "Migrating from OCUnit to XCTest", author: "Kyle Fuller"),
    Article(title: "Memory Management with ARC", author: "Kyle Fuller"),
  ]
]

let environment = Environment(loader: FileSystemLoader(paths: ["templates/"]))
let rendered = try environment.renderTemplate(name: "article_list.html", context: context)

print(rendered)

理念

Stencil 遵循与 Django 相同的理念

如果您有编程背景,或者您习惯于将编程代码直接混入 HTML 的语言,您需要记住 Django 模板系统不仅仅是将 Python 嵌入到 HTML 中。 这是设计使然:模板系统旨在表达呈现,而不是程序逻辑。

用户指南

Stencil 模板作者编写 Stencil 模板的资源

帮助您将 Stencil 集成到 Swift 项目中的资源

使用 Stencil 的项目

Sourcery, SwiftGen, Kitura, Weaver, Genesis

许可证

Stencil 基于 BSD 许可证授权。有关更多信息,请参阅LICENSE