Coder Social home page Coder Social logo

alisoftware / stencil Goto Github PK

View Code? Open in Web Editor NEW

This project forked from stencilproject/stencil

0.0 3.0 1.0 266 KB

Stencil is a simple and powerful template language for Swift.

License: BSD 2-Clause "Simplified" License

Swift 98.42% Objective-C 1.23% HTML 0.36%

stencil's Introduction

Stencil

Build Status

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.

{% for article in articles %}
  - {{ article.title }} by {{ article.author }}.
{% endfor %}
let context = Context(dictionary: [
  "articles": [
    [ "title": "Migrating from OCUnit to XCTest", "author": "Kyle Fuller" ],
    [ "title": "Memory Management with ARC", "author": "Kyle Fuller" ],
  ]
])

do {
  let template = Template(named: "template.stencil")
  let rendered = template.render(context)
  print(rendered)
} catch {
  print("Failed to render template \(error)")
}

Installation

Installation with CocoaPods is recommended.

pod 'Stencil'

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.

Templates

Variables

A variable can be defined in your template using the following:

{{ variable }}

Stencil will look up the variable inside the current variable context and evaluate it. When a variable contains a dot, it will try doing the following lookup:

  • Context lookup
  • Dictionary lookup
  • Array lookup (first, last, count, index)
  • Key value coding lookup

For example, if people was an array:

There are {{ people.count }} people, {{ people.first }} is first person.
Followed by {{ people.1 }}.

Filters

Filters allow you to transform the values of variables. For example, they look like:

{{ variable|uppercase }}
Capitalize

The capitalize filter allows you to capitalize a string. For example, stencil to Stencil.

{{ "stencil"|capitalize }}
Uppercase

The uppercase filter allows you to transform a string to uppercase. For example, Stencil to STENCIL.

{{ "Stencil"|uppercase }}
Lowercase

The uppercase filter allows you to transform a string to lowercase. For example, Stencil to stencil.

{{ "Stencil"|lowercase }}

Registering custom filters

template.parser.registerFilter("double") { value in
  if let value = value as? Int {
    return value * 2
  }

  return value
}

Tags

Tags are a mechanism to execute a piece of code, allowing you to have control flow within your template.

{% if variable %}
  {{ variable }} was found.
{% endif %}

A tag can also affect the context and define variables as follows:

{% for item in items %}
  {{ item }}
{% endfor %}

Stencil has a couple of built-in tags which are listed below. You can also extend Stencil by providing your own tags.

for

A for loop allows you to iterate over an array found by variable lookup.

{% for item in items %}
  {{ item }}
{% empty %}
  There we're no items.
{% endfor %}

if

{% if variable %}
  The variable was found in the current context.
{% else %}
  The variable was not found.
{% endif %}

ifnot

{% ifnot variable %}
  The variable was NOT found in the current context.
{% else %}
  The variable was found.
{% endif %}

include

You can include another template using the include tag.

{% include "comment.html" %}

The include tag requires a TemplateLoader to be found inside your context with the paths, or bundles used to lookup the template.

let context = Context(dictionary: [
  "loader": TemplateLoader(bundle:[NSBundle.mainBundle()])
])

Building custom tags

You can build a custom template tag. There are a couple of APIs to allow you to write your own custom tags. The following is the simplest form:

template.parser.registerSimpleTag("custom") { context in
  return "Hello World"
}

When your tag is used via {% custom %} it will execute the registered block of code allowing you to modify or retrieve a value from the context. Then return either a string rendered in your template, or throw an error.

If you want to accept arguments or to capture different tokens between two sets of template tags. You will need to call the registerTag API which accepts a closure to handle the parsing. You can find examples of the now, if and for tags found inside Node.swift.

Custom template tags must be registered prior to calling Template.render the first time.

The architecture of Stencil along with how to build advanced plugins can be found in the architecture document.

Comments

To comment out part of your template, you can use the following syntax:

{# My comment is completely hidden #}

License

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

stencil's People

Contributors

alisoftware avatar chunkerchunker avatar jpsim avatar kattrali avatar kylef avatar mrackwitz avatar pyanfield avatar segiddins avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.