couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <>
Subject [Couchdb Wiki] Update of "Generating HTML from Javascript shows and lists" by RogerBinns
Date Fri, 04 Dec 2009 06:02:32 GMT
Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Couchdb Wiki" for change notification.

The "Generating HTML from Javascript shows and lists" page has been changed by RogerBinns.
The comment on this change is: Info about Javascript templating.


New page:
= Generating HTML from Javascript shows and lists =
You can generate output from [[|shows]]
and [[|lists]].  Typically this would
be HTML intended for a browser but any format can be generated. CouchDB already includes [[|Javascript
support]] for XML derived formats (eg Atom feeds). It is impractical to generate HTML directly
so some sort of templating is recommended.

== Best Practise ==
Generate clear concise simple HTML from your show/list functions.  The resulting HTML interface
should be usable from constrained devices (eg cell phones, set top boxes) as well as being
accessible (eg for screen readers for blind people) and easy to index for search engines.
 This is also easier to automatically test.  You can then run Javascript in the browser (if
the browser supports Javascript and it is turned on) to enhance what is being displayed (eg
add extra information, tooltips, icons, previews of next/previous content, enhanced menus
and interaction etc).

== Constraints ==
The Javascript view server and the environment the code run in mean that some existing Javascript
templating libraries will not work.

 * There is no network/file access so templates cannot be loaded over the network or from
a file.  Instead they must be strings already included into your Javascript code.  (See the
!json directive of couchapp which does this for you).  They must also return strings.
 * There is no [[|DOM]] available (templating
libraries often assume that they are running in a browser working on the currently displayed
 * Some work on complete documents whereas your show and especially list functions are often
working on multiple strings and template fragments
 * Some only do HTML - this is good if they ensure the result is correct HTML
 * Some do any form of templating (eg plain text) which means your resulting HTML can be invalid
 * It is a very good idea to use a library that automatically escapes values (eg replacing
< with ampersand lt semicolon) otherwise your application will be prone to [[|cross
site scripting attacks]].  It should also provide a way of disabling the escaping when you
are intentionally providing raw HTML.
 * Size can be a problem.  Some templating libraries are rather large and depend on other
libraries. They can create many layersof intermediary functions and caching making it hard
to debug what is happening.

== Solutions ==
The solutions listed below are known to work with CouchDB show and list functions, generating
HTML and working with CouchDB deployment conventions (ie !json string templates and !code
inclusion into the show/list functions).

=== John Resig's micro-templating ===
This engine is a screenful of code and can be downloaded at
 You can read about using it in the [[|CouchDB
book]].  Example usage can be found in the [[|Sofa blog application]].
 It does not do HTML escaping so you will need to be very careful.  The templating is not
HTML specific so you can generate other formats.  (The tags are HTML syntax though.)

This is an example of how to do conditionals:

<% if ( { %>
    Foo is true-ish
<% } else { %>
    Foo is not true-ish
<% } %>

View raw message