Teacup is templates in CoffeeScript.
One of the great things about CoffeeScript is it's ability to support native DSLs. Teacup is a native CoffeeScript DSL for producing HTML. Use composition and functional constructs, import helpers just as you would any other dependency.
To use in Node, either for templates rendered on the server or for templates compiled with connect-assets:
$ npm install teacup
If you're interested in using Teacup with Rails, Teacup::Rails makes Teacup available to the asset pipeline in Rails 3.1+.
require 'teacup'render ->div '#sample'->h1 -> 'Hello, world'
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.coffeerequire 'teacup'module.exports = renderablediv '#example'->h1 "Hello, "
You can use Teacup templates even if your Express app is not using CoffeeScript.
If you are using connect-assets to compile your CoffeeScript in
an asset pipeline. You can use the Teacup middleware which registers the connect-assets
css helpers with Teacup.
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.
To use for client-side rendering, all you need is teacup.js from this project. Since Teacup is all about CoffeeScript, it only makes sense to use if you are writing your views in CoffeeScript. Use it with an asset pipeline like in Rails or connect-assets (see above) or compile your templates as part of your build process.
In the browser, Teacup exports window.teacup. In the examples below, simply replace
require 'teacup' with
teacuptemplate = renderableul ->li item for item in itemsconsolelog template'One''Two'
You can write your template in the same file a Backbone View and call your template from the view's
render method like so:
teacuptemplate = renderablediv ->h1 "Welcome to our tea party"p "We have a few kids at the table..."ul ->kidseachli kidget 'name'form ->input placeholder: 'Add another':@kids = kidssuper: ->@$elhtml template@kids@$'form input'focus@
Check out teacup-backbone-example for a complete Backbone + Express app.
render helper to render a template 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>
renderable helper to create a function that can be called to render the template at a later time.
require 'teacup'template = renderableul ->li item for item in itemsconsolelog template'One''Two'# Outputs <ul><li>One</li><li>Two</li></ul>
Teacup escapes input by default. To disable escaping, use the
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>
Write your view helpers as renderable functions and require them as needed.
Here's a helpers file that defines a set of microformats.
# views/microformats.coffeerequire 'teacup'moment = require 'moment'module.exports =hcalendar: renderablespan ".vevent"->span ".summary"summarytext " on "span ".dtstart"momentdateformat"YYYY-MM-DD"text " was in "span ".location"location
And a view that uses one of the helpers.
# views/events.coffeerequire 'teacup'require './microformats'module.exports = renderableul ->for event in eventsli ->hcalendar event
Just use the CoffeeScript compiler.
$ coffee -cl -o build src
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.
$ git clone https://github.com/goodeggs/teacup && cd teacup $ npm install $ npm test