Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use html templates in CouchDB

I've been searching everywhere trying to figure this one out. I'm trying to generate html pages from couchdb show and list functions. I'd like to leverage underscore.js's template solution. The part I'm getting stuck on is how to include html templates in my show and list functions.

Where do I store them? As attachments? And then how do I reference them in my show and list functions. I assume !json and !code macros are not being used, and I can't figure out how to use require() from common js to do it.

Any help would rock!

Thanks!

Extra Info: I'm using Kanso to push my apps, not CouchApp.

like image 208
Costa Michailidis Avatar asked Jan 18 '23 00:01

Costa Michailidis


1 Answers

CouchDB attachments are, by definition, not accessible in show and list functions.

Show and list functions support CommonJS. So you simply need to include any libraries in the design doc.

{ "_id": "_design/example"
, "say_hi": "module.exports = function(person) { return 'Hello, ' + person }"
, "shows":
  { "hello": "function(doc, req) { var hi = require('say_hi'); return hi(req.query.me) }"
  }
}

This view would look like this

GET /my_db/_design/example/_show/hello?me=Jason

HTTP/1.1 200 OK
Server: CouchDB/1.2.0 (Erlang OTP/R15B)
Date: Fri, 06 Apr 2012 11:02:33 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 12


Hello, Jason
like image 177
JasonSmith Avatar answered Jan 29 '23 02:01

JasonSmith