• Stars
    star
    191
  • Rank 202,877 (Top 4 %)
  • Language
    CoffeeScript
  • License
    MIT License
  • Created almost 13 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

keeping the project alive with this clone of mauricemach/coffeekup

CoffeeCup <☕/>

Markup as CoffeeScript

Build Status

This is a clone of @mauricemach CoffeeCup.

I am renaming and trying to keep this project alive.

Fork CoffeeCup on Github.

CoffeeCup is a templating engine for node.js and browsers that lets you to write your HTML templates in 100% pure CoffeeScript.

It was created in celebration of whyday, as an application of the concept used in Markaby ("Markup as Ruby", by Tim Fletcher and why the lucky stiff) to CoffeeScript.

Here's what a template written for CoffeeCup looks like:

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}
    '''

    comment 'Stylus is supported as well'

    stylus '''
      body
        margin: 0
    '''
             
    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!'

Interactive demo at coffeekup.org.

_why?

  • One language to rule them all. JavaScript is everywhere, thus so is CoffeeScript. Servers, browsers, even databases. If extending this to rendering logic and UI structure (server and client side) is desirable to you, CoffeeCup is your friend.

  • More specifically, one outstanding language. CoffeeScript is one hell of a clean, expressive, flexible and powerful language. It's hard to find such combination, especially if you need it to run in the browser too.

  • Not yet another specialized language to learn. Transferable knowledge FTW.

  • Embed your templates in CoffeeScript nicely. Templates are just functions, so they don't lose syntax highlighting and syntax checking when embedded in CoffeeScript apps.

  • Embed CoffeeScript in your templates nicely. In the same manner, you can write the contents of <script> blocks in CoffeeScript, and keep the highlighting. Perhaps more significantly, the CoffeeScript compiler doesn't have to be called just to convert these blocks to JS, as in other templating engines.

  • Extensive editor support. You benefit from the already existing list of excellent CoffeeScript text editor plugins.

  • Client-server consistency. The same template language and implementation in node.js or the browser.

  • Easily extendable into a higher level "DSL". Since all elements are just functions, it's very easy to define your own custom "tags", which will work and look the same as "native" ones.

  • HTML 5 ready. Boring legacy doctypes and elements also available.

  • Optional auto-escaping. You can also use the h helper on a case-by-case basis.

  • Optional formatting, with line breaks and indentation.

  • Pick your poison. Works with both CoffeeScript and JavaScript apps.

Why not?

CoffeeCup may not be your best choice in those cases:

  • You're after the cleanest syntax possible, above all. In this regard a specialized language such as Jade just can't be beaten.

  • You use divs and/or classes for everything. While in CoffeeCup you can do div '#id.class.class', specialized languages often have an even shorter syntax for that.

  • You want CoffeeScript for rendering logic, but you'd rather stick with HTML for markup. Then you're looking for Eco.

  • For your specific project/team/preferences, you think a limited and/or separate language for templating is actually beneficial.

Installing

Just grab node.js and npm and you're set:

npm install coffeecup

To get the coffeecup command, install it globally:

npm install coffeecup -g

Or to use the latest version:

git clone [email protected]:gradus/coffeecup.git && cd coffeecup
cake build
npm link
cd ~/myproject
npm link coffeecup

Using

cc = require 'coffeecup'

cc.render -> h1 "You can feed me templates as functions."
cc.render "h1 'Or strings. I am not too picky.'"

Defining variables:

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

cc.render(template, title: 'Log In', hardcode: helpers)

Precompiling to functions:

template = cc.compile(template, locals: yes, hardcode: {zig: 'zag'})

template(foo: 'bar', locals: {ping: 'pong'})

With express:

app.set('view engine', 'coffee')
app.engine 'coffee', require('coffeecup').__express

app.get '/', (req, res) ->
  # Will render views/index.coffee:
  res.render 'index', foo: 'bar'

With zappa:

get '/': ->
  @franks = ['miller', 'oz', 'sinatra', 'zappa']
  render 'index'

view index: ->
  for name in @franks
    a href: "http://en.wikipedia.org/wiki/Frank_#{name}", -> name

With meryl:

coffeekup = require 'coffeecup'

meryl.get '/', (req, resp) ->
  people = ['bob', 'alice', 'meryl']
  resp.render 'layout', content: 'index', context: {people: people}

meryl.run
  templateExt: '.coffee'
  templateFunc: coffeecup.adapters.meryl

On the browser:

<script src="template.js"></script>
<script>
  $('body').append(templates.template({foo: 'bar'}));
</script>

This is one of many browser deployment possibilities, pre-compiling your template on the server to a standalone function. To see all serving suggestions, check out regular, decaf and crème.

Command-line:

$ coffeecup -h

Usage:
  coffeecup [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 CoffeeCup version
  -h, --help         display this help message

See /examples for complete versions (you have to run cake build first).

Please note that even though all examples are given in CoffeeScript, you can also use their plain JavaScript counterparts just fine.

Resources

Tools

Related projects

  • ck - "a smaller, faster coffeekup": Alternative, barebones implementation.

  • ckup - "Markup as Coco": Similar engine but for Coco ("Unfancy CoffeeScript").

  • Eco - "Embedded CoffeeScript templates": "EJS/ERB" for CoffeeScript.

  • timbits - "Widget framework based on Express and CoffeeScript".

  • coffee-css - "More CSS for CoffeeScript".

  • ccss - "CoffeeScript CSS".

  • black-coffee - Flatiron and Coffee-Script Template.

  • iron-coffee - Flatiron and Coffee-Script Template.

  • teacup - Descendant that preserves locals in lexical scope.

Compatibility

Latest version tested with node 0.8.17 and CoffeeScript 1.4.0.

Special thanks

More Repositories

1

jsoncsv

takes json and converts to csv
CoffeeScript
22
star
2

black-coffee

Pure CoffeeScript boilerplate using CoffeeCup
CoffeeScript
6
star
3

node-chess

chess game built with nodejs
JavaScript
3
star
4

mindmap

just playing around with nodejitsu and flatiron for nodejs.
CoffeeScript
3
star
5

flatiron-example

Example CRUD web app for creating gists using @flatironjs
JavaScript
3
star
6

surf-scoreboard

playing with event-driven coffeescript pattern for rewriting surf scoring application with nodeJS
JavaScript
3
star
7

saucer

Keep your eyes peeled. Intuition tells me this will be good
CoffeeScript
2
star
8

stress

8 ball of good advice when things get tense
C
2
star
9

dolphin-habits

tracking silly dolphin habits for fun with resourceful
JavaScript
2
star
10

mysql-backup

MySQL bakup Shell script
Shell
2
star
11

klop

demo app with real time chess game and chat using nodejs, coffee-script, coffeeKup, Meryl, and nowJS
JavaScript
2
star
12

roots-surf

static site for the Roots Surf Team generated with Bam
JavaScript
2
star
13

coffeecake

a site generated with @twilson63 cupcake and coffeecup
CoffeeScript
1
star
14

drgradus

Static Personal Website generated with Bam https://github.com/beautifulnode/bam
JavaScript
1
star
15

saucer-example-app

Example for Saucer usage
JavaScript
1
star
16

node-blug

blog demo
JavaScript
1
star
17

bam-test-site

playing with BAM, The easiest static site generator on the planet.
JavaScript
1
star
18

chsjs

work in progress for a CHS JS Group site
CoffeeScript
1
star
19

blue_ridge_demo

demo for JS Unit Testing
Ruby
1
star
20

growing-robot

Chat app in Clojure (WIP)
Clojure
1
star
21

johnny-tremain

Example apprentice app
CoffeeScript
1
star
22

node-sms-coffee

text forwarding with smsified written in coffeescript
CoffeeScript
1
star
23

union-example

Simple example of a union app with socket.io
JavaScript
1
star
24

coffeecup-docs

documentation for CoffeeCup generated with @twilson63 BAM
JavaScript
1
star
25

pub-med-search

a simple php search using pub_med api
PHP
1
star
26

simple-guestbook

WIP guestbook in nodeJS
JavaScript
1
star
27

nan

spree store for not a ninja shirts and hats
Ruby
1
star
28

zen

The Zen of Programming
C
1
star
29

cottage_wp_theme

PHP
1
star
30

equip-example

An example using jesusabdullah/node-equip
JavaScript
1
star
31

node-equip

Equip connect middlewares to your flatiron stack.
JavaScript
1
star
32

resourceful-test

testing issues with flatiron/resourceful
JavaScript
1
star
33

cupcake-demo

NodeJS Chess game generated with Cupcake, written in coffee-script
CoffeeScript
1
star
34

easy-doc

Documentation site generated with BAM and twitter bootstrap
JavaScript
1
star
35

blacksmith-blog

static blog site generated with blacksmith http://blacksmith.jit.su/all-pages
JavaScript
1
star
36

coffee-with-cream

flatiron and coffeecup example with creamer. thx to @twilson63 for adding this idea to the CoffeeCup examples
CoffeeScript
1
star
37

tako-macho

A Tako, CoffeeCup boilerplate written in CoffeeScript with Skeleton layout
CoffeeScript
1
star
38

chsruby

New Site for Charleston Ruby Users Group
Ruby
1
star
39

bamtest

2nd take with new BAM version, The easiest static site generator on the planet.
JavaScript
1
star