Convert Figma logo to code with AI

stencilproject logoStencil

Stencil is a simple and powerful template language for Swift.

2,341
223
2,341
33

Top Related Projects

10,235

A very fast and expressive template engine.

11,005

Liquid markup language. Safe, customer facing template language for flexible web apps.

A powerful templating engine with inheritance, asynchronous control, and more (jinja2 inspired)

79,088

The Web framework for perfectionists with deadlines.

Minimal templating on steroids.

Quick Overview

Stencil is a simple and powerful template language for Swift. It provides a syntax similar to Django and Mustache, allowing developers to create flexible and reusable templates for various purposes, such as generating dynamic content or code generation.

Pros

  • Easy to learn and use, with a syntax familiar to developers who have worked with Django or Mustache
  • Supports custom filters and tags, allowing for extensibility and customization
  • Provides safety features like automatic HTML escaping to prevent XSS attacks
  • Offers good performance and is actively maintained

Cons

  • Limited built-in filters and tags compared to some more mature template engines
  • Lack of extensive documentation and examples for advanced use cases
  • May require additional setup and configuration for use in server-side Swift applications
  • Not as widely adopted as some other template engines in the Swift ecosystem

Code Examples

  1. Basic variable rendering:
let template = "Hello, {{ name }}!"
let context = ["name": "World"]
let rendered = try stencil.render(template: template, context: context)
// Output: "Hello, World!"
  1. Using filters:
let template = "The price is {{ price | currency }}"
let context = ["price": 42.5]
let rendered = try stencil.render(template: template, context: context)
// Output: "The price is $42.50"
  1. Conditional statements:
let template = """
{% if isLoggedIn %}
    Welcome back, {{ username }}!
{% else %}
    Please log in.
{% endif %}
"""
let context = ["isLoggedIn": true, "username": "John"]
let rendered = try stencil.render(template: template, context: context)
// Output: "Welcome back, John!"

Getting Started

To use Stencil in your Swift project, follow these steps:

  1. Add Stencil to your project using Swift Package Manager:
dependencies: [
    .package(url: "https://github.com/stencilproject/Stencil.git", from: "0.15.1")
]
  1. Import Stencil in your Swift file:
import Stencil
  1. Create a simple template and render it:
let environment = Environment(loader: FileSystemLoader(paths: ["path/to/templates"]))
let template = try environment.loadTemplate(name: "greeting.stencil")
let context = ["name": "Swift Developer"]
let rendered = try template.render(context)
print(rendered)

Competitor Comparisons

10,235

A very fast and expressive template engine.

Pros of Jinja

  • More mature and widely adopted, with extensive documentation and community support
  • Offers a richer set of features, including complex control structures and filters
  • Supports multiple programming languages beyond Python

Cons of Jinja

  • Can be more complex to learn and use, especially for beginners
  • May have a larger footprint and slower performance for simpler templating needs

Code Comparison

Jinja:

{% for item in items %}
    <li>{{ item.name | capitalize }}</li>
{% endfor %}

Stencil:

{% for item in items %}
    <li>{{ item.name | capitalize }}</li>
{% endfor %}

Both Jinja and Stencil use similar syntax for basic templating operations, making it easier for developers to switch between them. However, Jinja offers more advanced features and flexibility, while Stencil focuses on simplicity and Swift integration.

Jinja is better suited for complex templating needs across multiple languages, while Stencil is ideal for Swift-based projects that require a lightweight and easy-to-use templating solution.

11,005

Liquid markup language. Safe, customer facing template language for flexible web apps.

Pros of Liquid

  • More mature and widely adopted, especially in e-commerce platforms
  • Extensive documentation and community support
  • Built-in filters and tags for common e-commerce operations

Cons of Liquid

  • Less flexible syntax compared to Stencil
  • Limited extensibility without custom Ruby code
  • Performance can be slower for complex templates

Code Comparison

Liquid:

{% if product.title contains 'Pack' %}
  This product is part of a pack
{% endif %}

Stencil:

{{#contains product.title 'Pack'}}
  This product is part of a pack
{{/contains}}

Key Differences

  • Syntax: Liquid uses {% %} for logic and {{ }} for output, while Stencil uses Handlebars-style syntax
  • Language: Liquid is implemented in Ruby, Stencil in Swift
  • Extensibility: Stencil allows for easier custom tag and filter creation
  • Performance: Stencil generally offers better performance, especially for complex templates
  • Use case: Liquid is more e-commerce focused, while Stencil is more general-purpose

Conclusion

Both Liquid and Stencil are powerful templating engines with their own strengths. Liquid excels in e-commerce scenarios and has a larger ecosystem, while Stencil offers more flexibility and potentially better performance for general-purpose templating needs.

A powerful templating engine with inheritance, asynchronous control, and more (jinja2 inspired)

Pros of Nunjucks

  • More extensive feature set, including asynchronous support and custom filters
  • Larger community and ecosystem, with more resources and third-party extensions
  • Better performance for complex templates and large datasets

Cons of Nunjucks

  • Steeper learning curve due to more advanced features
  • Heavier footprint, which may impact load times in browser environments
  • Less focus on simplicity and minimalism compared to Stencil

Code Comparison

Nunjucks:

{% for item in items %}
  <li>{{ item.name | capitalize }}</li>
{% endfor %}

Stencil:

{% for item in items %}
  <li>{{ item.name | capitalize }}</li>
{% endfor %}

While the basic syntax is similar, Nunjucks offers more advanced features like:

{% asyncEach item in items %}
  <li>{{ item.name | async_filter }}</li>
{% endeach %}

Stencil focuses on simplicity and doesn't include async operations or complex filters out of the box. Both templating engines provide similar core functionality, but Nunjucks offers more advanced features at the cost of increased complexity. Stencil aims for a simpler, more lightweight approach, which may be preferable for projects with straightforward templating needs.

79,088

The Web framework for perfectionists with deadlines.

Pros of Django

  • Full-featured web framework with built-in admin interface, ORM, and authentication system
  • Large, active community with extensive documentation and third-party packages
  • Follows the "batteries included" philosophy, providing a complete solution out of the box

Cons of Django

  • Steeper learning curve due to its comprehensive nature
  • Can be overkill for smaller projects or simple websites
  • Opinionated structure may limit flexibility in some cases

Code Comparison

Django (URL routing):

urlpatterns = [
    path('admin/', admin.site.urls),
    path('blog/', include('blog.urls')),
    path('', views.home, name='home'),
]

Stencil (template rendering):

let context = ["name": "World"]
let template = Template(templateString: "Hello, {{ name }}!")
let result = try template.render(context)
print(result) // Output: Hello, World!

Django is a full-stack web framework for Python, while Stencil is a lightweight templating engine for Swift. Django offers a complete solution for web development, including an ORM, admin interface, and authentication system. Stencil, on the other hand, focuses solely on templating and is more suitable for use in iOS and macOS applications. Django's comprehensive nature makes it ideal for complex web applications, while Stencil's simplicity makes it a good choice for rendering templates in Swift projects.

Minimal templating on steroids.

Pros of Handlebars.js

  • More mature and widely adopted, with a larger community and ecosystem
  • Supports both client-side and server-side rendering
  • Offers pre-compilation for improved performance

Cons of Handlebars.js

  • Syntax can be more verbose compared to Stencil
  • Limited built-in functionality, often requiring custom helpers
  • Steeper learning curve for complex templates

Code Comparison

Handlebars.js:

{{#each items}}
  <li>{{name}}: {{price}}</li>
{{/each}}

Stencil:

{% for item in items %}
  <li>{{ item.name }}: {{ item.price }}</li>
{% endfor %}

Both templating engines offer similar functionality for basic loops and variable output. Handlebars.js uses {{#each}} for iteration, while Stencil uses a more familiar {% for %} syntax. Stencil's syntax is generally more concise and resembles other popular templating languages.

Handlebars.js is better suited for projects requiring cross-platform compatibility and extensive customization through helpers. Stencil, on the other hand, offers a simpler syntax and is particularly well-integrated with Swift and Apple platforms, making it an excellent choice for iOS and macOS development.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Stencil

Stencil is a simple and powerful template language for Swift. It provides a syntax similar to Django and Mustache. If you're familiar with these, you will feel right at home with Stencil.

Example

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)

Philosophy

Stencil follows the same philosophy of Django:

If you have a background in programming, or if you’re used to languages which mix programming code directly into HTML, you’ll want to bear in mind that the Django template system is not simply Python embedded into HTML. This is by design: the template system is meant to express presentation, not program logic.

The User Guide

Resources for Stencil template authors to write Stencil templates:

Resources to help you integrate Stencil into a Swift project:

Projects that use Stencil

Sourcery, SwiftGen, Kitura, Weaver, Genesis

License

Stencil is licensed under the BSD license. See LICENSE for more info.