• Stars
    star
    435
  • Rank 100,085 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 11 years ago
  • Updated over 8 years ago

Reviews

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

Repository Details

An internal DSL for generating HTML in JavaScript

Pithy.js

An internal DSL for generating HTML in JavaScript.

Examples

Basic elements

html.div('#main', [
    html.h1(null, 'Hello, world!'),
    html.img({src: 'foo.jpg'})
]);
<div id="main">
    <h1>Hello, world!</h1>
    <img src="foo.jpg"/>
</div>

Loops etc.

Using Underscore.js or similar:

function todoItem(item) {
    return html.li({rel: item.id}, [
        html.div('.title', item.title),
        html.button('.destroy', 'delete')
    ]);
}

function todoList(list) {
    return html.ul('.todo-list', _.map(list, todoItem));
}

todoList([
    {id: 1, title: 'item one'},
    {id: 2, title: 'item two'},
    {id: 3, title: 'item three'}
]);
<ul class="todo-list">
    <li rel="1">
        <div class="title">item one</div>
        <button class="destroy">delete</button>
    </li>
    <li rel="2">
        <div class="title">item two</div>
        <button class="destroy">delete</button>
    </li>
    <li rel="3">
        <div class="title">item three</div>
        <button class="destroy">delete</button>
    </li>
</div>

Why use an internal DSL?

  • It's a more convenient and safer alternative to string contatenation
  • Very flexible, you can use all the power of JavaScript functions and control structures
  • For small bits of HTML you might not want to switch contexts from code to a template
  • Easier to debug than a templating engine
  • You get full tool-chain support:
    • editor support: syntax highlighting, code tools etc etc
    • code analyzers: jslint, jshint
    • testing/coverage tools

When to use?

  • Consider using where you might currently use string concatenation
  • Avoid using for large HTML documents or in places where speed is critical
  • Good for small snippets used for client-side page updates
  • Bad for generating huge amounts of HTML on the server

Usage

I like to alias the 'pithy' library as 'html':

var html = require('pithy');

You can then just use html.tagname as a function to create the appropriate element. Please note, you actually get a html.SafeString object back, not a native JavaScript String. This might mess up your isString() tests. If you have a workaround please send a pull-request.

There is also a html.escape() function for escaping HTML (returns a html.SafeString). It will not escape a value that is already a html.SafeString object.

More Repositories

1

async

Async utilities for node and the browser
JavaScript
28,095
star
2

highland

High-level streams library for Node.js and the browser
JavaScript
3,427
star
3

nodeunit

Easy unit testing in node.js and the browser, based on the assert module.
JavaScript
1,899
star
4

jam

JavaScript package manager - using a browser-focused and RequireJS compatible repository
JavaScript
1,497
star
5

forms

An easy way to create, parse and validate forms in node.js
JavaScript
1,010
star
6

nimble

A really tiny functional JavaScript and async flow-control library
JavaScript
329
star
7

petrify

A flexible static site generator for node.js
JavaScript
259
star
8

cookie-sessions

Secure cookie-based session middleware for Connect
JavaScript
97
star
9

quip

A chainable API for response objects in node
JavaScript
92
star
10

dispatch

A regular expression URL dispatcher for Connect
JavaScript
84
star
11

magery

JavaScript templates for progressive enhancement
JavaScript
56
star
12

copykitten

Tiny immutable JSON data structures
JavaScript
40
star
13

jquery.notify.js

Ubuntu-style notifications within a web browser
JavaScript
25
star
14

couchr

Lightweight CouchDB request library for Node and the browser
JavaScript
22
star
15

hoodie-drawing

Hoodie drawing demo app
JavaScript
20
star
16

cpm

CouchDB Package Manager - a tool for the management of CouchApps and associated libraries
JavaScript
16
star
17

wmd

wanton markdown - a markdown parser based on showdown
JavaScript
15
star
18

blog

CouchApp code used to run my blog
JavaScript
15
star
19

tamawiki

I'm learning Rust!
Rust
13
star
20

chicken-toml

A TOML parser for CHICKEN Scheme built with comparse
Scheme
11
star
21

docker-hoodie

Basic docker file for trying out hoodie
Shell
10
star
22

bootstrap-less

Kanso package providing uncompiled less files for bootstrap with images and js as attachments
JavaScript
10
star
23

behaviors

A (really) simple way to check a modules export's in node.js. Inspired by behaviors in Erlang.
JavaScript
9
star
24

snowy

HTTP server for CHICKEN Scheme
C
8
star
25

events

Browser port of the node.js events module
JavaScript
8
star
26

lolcorp

Intentionally insecure example Node.js applications
JavaScript
7
star
27

raspberry-pi-gpio

Raspberry Pi GPIO library for CHICKEN Scheme
Scheme
6
star
28

scrawl

Dumb comment parsing
JavaScript
6
star
29

pithy2

A Typescript-friendly and human-friendly API for building DOM elements
TypeScript
5
star
30

snake

Snake game for the BBC micro:bit
Python
5
star
31

erlmpd

An Erlang client module for MPD
Erlang
5
star
32

chicken-leveldb

CHICKEN Scheme bindings to LevelDB
Scheme
5
star
33

python-magery

Python implementation of Magery templates
Python
5
star
34

highland-couchr

Highland streams style API to CouchDB, using couchr under the hood
JavaScript
4
star
35

mutiny

Rust
4
star
36

chicken-sodium

CHICKEN Scheme bindings to libsodium crypto library
Scheme
3
star
37

0

3
star
38

ghstats

allows you to compare github users followers and project watchers
JavaScript
3
star
39

couchr-browser

Lightweight library for making CouchDB requests, based on jQuery
JavaScript
2
star
40

couchi

CouchDB command-line interface
JavaScript
2
star
41

magery-tests

Test suite for server implementations of Magery templates
HTML
2
star
42

chat

Kanso chat app
JavaScript
2
star
43

lmdb-lolevel

Low-level CHICKEN Scheme bindings to LMDB, closely following the C API
Scheme
2
star
44

condor

C O N D O R
JavaScript
2
star
45

chicken-level

abstract API layer for chicken-leveldb
Scheme
2
star
46

gamejam

First Play Sheffield Game Jam!
JavaScript
2
star
47

chicken-ldap-bind

Implements LDAP bind for authentication using OpenLDAP
Scheme
2
star
48

library

CouchApp for storing templates
JavaScript
2
star
49

chicken-gumbo

C binding to the Gumbo HTML parser for CHICKEN Scheme
Scheme
2
star
50

dust

P2P event streams
Scheme
2
star
51

users-core

Core resources for the users app, must be used with a theme (eg, users-default)
JavaScript
2
star
52

gh-label-dashboard

An example of using the github search API to query labels across all repositories in an organisation
1
star
53

validatejs

JavaScript
1
star
54

webhook-test

1
star
55

users-default

Default bootstrap theme for users app
1
star
56

chicken-sublevel

Namespaced access to leveldb implementations
Scheme
1
star
57

chicken-level-sexp

Automatically read and write s-expressions to leveldb
Scheme
1
star
58

chicken-fnmatch

Test filenames against shell wildcard patterns using fnmatch
Scheme
1
star
59

jambot

IRC bot for #jamjs
JavaScript
1
star
60

mochiproxy

Easily proxy mochiweb requests, based on the CouchDB proxy module
Erlang
1
star
61

highland-couchdb-alldocs

Provides Highland streams for reading all keys/docs in a CouchDB database
JavaScript
1
star