Coffeekup是基於CoffeeScript的HTML模板引擎。它可讓你用100%純CoffeeScript編寫HTML模板。node.js和瀏覽器均可以使用。javascript
doctype 5 html -> head -> meta charset: 'utf-8' title "#{@title or 'Untitled'} | A completely plausible website" meta(name: 'description', content: @description) if @description? link rel: 'stylesheet', href: '/css/app.css' style ''' body {font-family: sans-serif} header, nav, section, footer {display: block} ''' script src: '/js/jquery.js' coffeescript -> $(document).ready -> alert 'Alerts suck!' body -> header -> h1 @title or 'Untitled' nav -> ul -> (li -> a href: '/', -> 'Home') unless @path is '/' li -> a href: '/chunky', -> 'Bacon!' switch @user.role when 'owner', 'admin' li -> a href: '/admin', -> 'Secret Stuff' when 'vip' li -> a href: '/vip', -> 'Exclusive Stuff' else li -> a href: '/commoners', -> 'Just Stuff' div '#myid.myclass.anotherclass', style: 'position: fixed', -> p 'Divitis kills! Inline styling too.' section -> # A helper function you built and included. breadcrumb separator: '>', clickable: yes h2 "Let's count to 10:" p i for i in [1..10] # Another hypothetical helper. form_to @post, -> textbox '#title', label: 'Title:' textbox '#author', label: 'Author:' submit 'Save' footer -> # CoffeeScript comments. Not visible in the output document. comment 'HTML comments.' p 'Bye!'
怎麼樣?沒騙你吧?真的是100%的純咖啡哦!css
doctypes
等特性。h
幫助函數。如下狀況,CoffeeKup可能不是一個好選擇:html
div
和類表達全部東西。雖然 CoffeeKup中你可使用 div '#id.class.class'
,可是別的模板語言一般有更簡短的寫法。npm install coffeekup
全局安裝:java
npm install coffeekup -g
使用最新版:node
git clone git@github.com:mauricemach/coffeekup.git && cd coffeekup cake build npm link cd ~/myproject npm link coffeekup
ck = require 'coffeekup' ck.render -> h1 "You can feed me templates as functions." ck.render "h1 'Or strings. I am not too picky.'"
定義變量:jquery
template = -> h1 @title form method: 'post', action: 'login', -> textbox id: 'username' textbox id: 'password' button @title helpers = textbox: (attrs) -> attrs.type = 'text' attrs.name = attrs.id input attrs ck.render(template, title: 'Log In', hardcode: helpers)
編譯函數:git
template = ck.compile(template, locals: yes, hardcode: {zig: 'zag'}) template(foo: 'bar', locals: {ping: 'pong'})
配合express:github
app.set 'view engine', 'coffee' app.register '.coffee', require('coffeekup').adapters.express app.get '/', (req, res) -> # Will render views/index.coffee: res.render 'index', foo: 'bar'
配合zappa:web
get '/': -> @franks = ['miller', 'oz', 'sinatra', 'zappa'] render 'index' view index: -> for name in @franks a href: "http://en.wikipedia.org/wiki/Frank_#{name}", -> name
配合meryl:數據庫
coffeekup = require 'coffeekup' meryl.get '/', (req, resp) -> people = ['bob', 'alice', 'meryl'] resp.render 'layout', content: 'index', context: {people: people} meryl.run templateExt: '.coffee' templateFunc: coffeekup.adapters.meryl
在瀏覽器中使用:
<script src="template.js"></script> <script> $('body').append(templates.template({foo: 'bar'})); </script>
命令行:
; coffeekup -h Usage: coffeekup [options] path/to/template.coffee --js compile template to js function -n, --namespace global object holding the templates (default: "templates") -w, --watch watch templates for changes, and recompile -o, --output set the directory for compiled html -p, --print print the compiled html to stdout -f, --format apply line breaks and indentation to html output -u, --utils add helper locals (currently only "render") -v, --version display CoffeeKup version -h, --help display this help message
注意,雖然咱們的例子都用了CoffeeScript,可是你徹底可使用javascript。
想了解CoffeeKup的內部實現?請看[Inside CoffeeKup]系列: