Code Monkey home page Code Monkey logo

unfiltered-scalate.g8's Introduction

A giter8 template for Unfiltered applications with Scalate, Jetty.

g8 unfiltered/unfiltered-scalate

The instructions below refer to the default Scalate interface created in this template project.


Unfiltered Scalate Integration

Getting Started

Integrating Scalate templates into your Unfiltered app is dead simple with the Unfiltered Scalate Module. Here is a quick and dirty example.

import unfiltered.request._
import unfiltered.response._
import unfiltered.jetty._

object Server{
  def main(args: Array[String]): Unit = {
    val binding = SocketPortBinding(host = "localhost", port = 8080)
    val server = unfiltered.jetty.Server.portBinding(binding).plan(unfiltered.filter.Planify {
      case req => Ok ~> Scalate(req, "hello.ssp")
    }).run
  }
}

and the template looks like, and must be placed in src/main/resources/templates:

<%@ var name: String = "Deron Williams" %>
<%@ var city: String = "Salt Lake City" %>
<h1>Hello, ${name} from ${city}</h1>

To replace the name and city variables with your own, you pass tuples of (String, Any) in the Scalate object

Scalate(req, "hello.ssp", ("name", "Dustin"), ("city", "Paris"))

Next you probably want to serve static assets, like images, css, and javascript files. To do that, using Jetty as an example, you need to setup a context, and the code would look something like:

object Server{
  def main(args: Array[String]): Unit = {
    val binding = SocketPortBinding(host = "localhost", port = 8080)
    val server = unfiltered.jetty.Server.portBinding(binding)
    server.context("/public"){ ctx =>
      ctx.resources(new java.net.URL("file:///Users/molecule/development/scalate_demo/src/main/resources/public"))
    }.plan(unfiltered.filter.Planify {
      case req => Ok ~> Scalate(req, "hello.ssp")
    }).run
  }
}

now you can reference static assets from your template like /public/main.css or /public/images/logo.png

...and that's about it!

Further Configuration and Usage

Custom Template Engine

If you wish to move your templates somewhere else, or you want to configure the org.fusesource.scalate.TemplateEngine for production use, you can create your own TemplateEngine and pass it to the secondary parameters set manually:

import unfiltered.response._
import unfiltered.jetty._
import org.fusesource.scalate.TemplateEngine

object Server{
  def main(args: Array[String]): Unit = {
    val templateDirs = List(new java.io.File("/my/own/template/dirs"))
    val scalateMode = "production"
    val engine = new TemplateEngine(templateDirs, scalateMode)
    
    val binding = SocketPortBinding(host = "localhost", port = 8080)
    val server = unfiltered.jetty.Server.portBinding(binding)
    server.context("/public"){ ctx =>
      ctx.resources(new java.net.URL("file:///Users/molecule/development/scalate_demo/src/main/resources/public"))
    }.plan(unfiltered.filter.Planify {
      case req => Ok ~> Scalate(req, "hello.ssp")(engine)
    }).run
  }
}

or the TemplateEngine can be implicit:

implicit val engine = new TemplateEngine(templateDirs, scalateMode)

val binding = SocketPortBinding(host = "localhost", port = 8080)
val server = unfiltered.jetty.Server.portBinding(binding)
server.context("/public"){ ctx =>
  ctx.resources(new java.net.URL("file:///Users/molecule/development/scalate_demo/src/main/resources/public"))
}.plan(unfiltered.filter.Planify {
  case req => Ok ~> Scalate(req, "hello.ssp")
}).run

unfiltered-scalate.g8's People

Contributors

chrislewis avatar dependabot[bot] avatar noahlz avatar omarkilani avatar softprops avatar unfiltered-app[bot] avatar xuwei-k avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  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.