Teacup is templates in CoffeeScript.
Compose DSL functions to build strings of HTML.
Package templates and helpers in CommonJS, AMD modules, or vanilla coffeescript.
Integrate with the tools you love: Express, Backbone, Rails, and more.
The Basics
Use the renderable
helper to create a function that returns an HTML string when called.
require 'teacup' template = renderable ul -> li item for item in items consolelog template'One''Two'# Outputs <ul><li>One</li><li>Two</li></ul>
Use the render
helper to render a template to a string immediately.
require 'teacup' output = render -> ul -> li 'First Item' li 'Second Item' consolelog output# Outputs <ul><li>First Item</li><li>Second Item</li></ul>
Express
To use Teacup as your Express template engine:
Install from npm
$ npm install teacup
Register Teacup as a view engine.
express = require 'express'teacup = require 'teacup/lib/express'app = expressappconfigure ->appengine "coffee"teacuprenderFile
Then write your views as regular old coffee files that export a renderable template.
# views/example.coffee require 'teacup' module.exports = renderable div '#example'-> h1 "Hello, "
You can use Teacup templates even if your Express app is not using CoffeeScript.
connect-assets
If you are using connect-assets to compile your CoffeeScript in
an asset pipeline, you can use the Teacup middleware which registers connect-assets js
and css
helpers with Teacup.
Grab the module to get started
$ npm install teacup
Then configure the middleware
express = require 'express'connectAssets = require 'teacup/lib/connect-assets'app = expressappconfigure -> appuse connectAssetssrc: 'assets'jsDir: 'javascripts'cssDir: 'stylesheets'
And in your templates:
require 'teacup' module.exports = renderable -> html -> head -> js 'app' css 'app' body -> # ...
The Teacup middleware passes the provided options to connect-assets and returns an instance of the connect-assets middleware.
Browser
To use for client-side rendering, all you need is teacup.js. You can
toss it in a script tag, require()
and browserify it, load it with an AMD loader, send it down an asset pipeline
like Rails or connect-assets, or use some sweet custom build process.
Teacup claims window.teacup if you arent using AMD or CommonJS.
teacup template = renderable ul -> li item for item in items consolelog template'One''Two'
Backbone
Feel free to write your template in the same file as a Backbone View and call it from view.render()
like so:
teacup template = renderable div -> h1 "Welcome to our tea party" p "We have a few kids at the table..." ul -> kidseach li kidget 'name' form -> input placeholder: 'Add another' : @kids = kids super : -> @$elhtml template@kids @$'form input'focus @
Check out teacup-backbone-example for a complete Backbone + Express app.
Rails
The Teacup::Rails gem makes Teacup available to the asset pipeline in Rails 3.1+.
Guide
Escaping
Teacup escapes input by default. To disable escaping, use the raw
helper.
require 'teacup' inner = render -> h1 'Header' consolelog render -> div inner# Outputs <div><h1>Header</h1></div> consolelog render -> div -> raw inner# Outputs <div><h1>Header</h1></div>
Text
The text helper inserts a string in the template without wrapping it in a tag. It creates a text node.
require 'teacup' consolelog render -> p -> text 'Sometimes you just want ' b 'plain' text ' text.'# Outputs <p>Sometimes you just want <b>plain</b> text.</p>
Helpers
Write view helpers as renderable functions and require them as needed.
Here's a helpers file that defines a set of microformats.
# views/microformats.coffee require 'teacup'moment = require 'moment' module.exports = hcalendar: renderable span ".vevent"-> span ".summary"summary text " on " span ".dtstart"momentdateformat"YYYY-MM-DD" text " was in " span ".location"location
And a view that uses one of the helpers.
# views/events.coffee require 'teacup' require './microformats' module.exports = renderable ul -> for event in events li -> hcalendar event
Compiling Templates
Just use the CoffeeScript compiler. Uglify will make em real small.
$ coffee -cl -o build src
FAQ
How's this different from CoffeeCup?
CoffeeCup is the currently maintained fork of
CoffeeKup and is what we were using at Good Eggs before switching to Teacup.
The problem with CoffeeCup is that it uses some eval
magic to put the tag functions in scope. This magic breaks
closure scope so you can't actually write templates using the functional constructs that you'd expect.
Legacy
Markaby begat CoffeeKup begat CoffeeCup and DryKup which begat Teacup.
Contributing
$ git clone https://github.com/goodeggs/teacup && cd teacup
$ npm install
$ npm test