GithubHelp home page GithubHelp logo

isabella232 / kitura-stenciltemplateengine Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kitura/kitura-stenciltemplateengine

0.0 0.0 0.0 394 KB

Stencil templating for Kitura

License: Apache License 2.0

Swift 100.00%

kitura-stenciltemplateengine's Introduction

Kitura

APIDoc Build Status - Master macOS Linux Apache 2 Slack Status

Kitura-StencilTemplateEngine

Kitura-StencilTemplateEngine is a plugin for Kitura Template Engine for using Stencil with the Kitura server framework. This makes it easy to use Stencil templating, with a Kitura server, to create an HTML page with integrated Swift variables.

Swift version

The latest version of Kitura-StencilTemplateEngine requires Swift 4.0 or newer. You can download this version of the Swift binaries by following this link. Compatibility with other Swift versions is not guaranteed.

Stencil Template File

The template file is basically HTML with gaps where we can insert code and variables. Stencil is a templating language used to write a template file and Kitura-StencilTemplateEngine can use any standard Stencil template.

The Stencil user guide provides documentation and examples on how to write a Stencil template file.

The Kitura router, by default, will look in the Views folder for Stencil template files, that is files with the extension .stencil.

Usage

Add dependencies

Add the Kitura-StencilTemplateEngine package to the dependencies within your application’s Package.swift file. Substitute "x.x.x" with the latest Kitura-StencilTemplateEngine release.

.package(name: "KituraStencil", url: "https://github.com/Kitura/Kitura-StencilTemplateEngine.git", from: "x.x.x")

Add KituraStencil to your target's dependencies:

.target(name: "example", dependencies: ["KituraStencil"]),

Import package

import KituraStencil

Example

The following example takes a server generated using kitura init and modifies it to serve Stencil-formatted text from a .stencil file.

The files which will be edited in this example, are structured as follows:

<ServerRepositoryName>
├── Package.swift
├── Sources
│    └── Application
│         └── Application.swift
└── Views
     └── Example.stencil

The Views folder and Example.stencil file will be created later on in this example, since they are not initialized by kitura init.

Dependencies

Add the dependencies to your Package.swift file (as defined in Add dependencies above).

Application.swift

Inside the Application.swift file, add the following code to render the Example.stencil template file on the "/articles" route.

import KituraStencil

Add the following code inside the postInit() function:

router.add(templateEngine: StencilTemplateEngine())
router.get("/articles") { _, response, next in
var context: [String: [[String:Any]]] =
    [
        "articles": [
            ["title" : "Using Stencil with Swift", "author" : "Kitura"],
            ["title" : "Server-Side Swift with Kitura", "author" : "Kitura"],
        ]
    ]
    try response.render("Example.stencil", context: context)
    response.status(.OK)
    next()
}

Example.stencil

Create the Views folder and put the following Stencil template code into a file called Example.stencil:

<html>
    There are {{ articles.count }} articles. <br />

    {% for article in articles %}
        - {{ article.title }} written by {{ article.author }}. <br />
    {% endfor %}
</html>

This example is adapted from the Stencil user guide code. It will display the number of articles followed by a list of the articles and their authors.

Run the application and once the server is running, go to http://localhost:8080/articles to view the rendered Stencil template.

API Documentation

For more information visit our API reference.

Community

We love to talk server-side Swift, and Kitura. Join our Slack to meet the team!

License

This library is licensed under Apache 2.0. Full license text is available in LICENSE.

kitura-stenciltemplateengine's People

Contributors

andrew-lees11 avatar dannys42 avatar djones6 avatar dunncoding avatar helenmasters avatar ianpartridge avatar lahariganti avatar lanza avatar nocturnalsolutions avatar quanvo87 avatar seabaylea avatar shihabmehboob avatar shmuelk avatar swiftdevops avatar twostraws avatar vadimeisenbergibm avatar youming-lin 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.