2
votes

I am currently struggling a bit with CouchDB's difference between view and show functions. I am using CodeIgniter to query the CouchDB via curl. I have no attached templates in _design documents. Each template is loaded via CI's view method as plain HTML. With the help of Mustache.js I want to be able to make use of Mustache's pseudo-variables.

As I understood from the documentation, view results are always presented in key:value pairs.

Let's say I have the following result from http://127.0.0.1:5984/blog/_design/entries/_view/all

{"total_rows":2,"offset":0,"rows":[
{"key":"56a13b96ea3492ef8c7e554f67000952","value":{title:"First post"}},
{"key":"56a13b96ea3492ef8c7e554f670015a9","value":{title:"Second post"}}
]}

Following Mustache's methodology I would be able to do something like this:

{{#rows}}
  <div class="entry">
    <h1>{{#value}}{{title}}{{/value}}</h1>
  </div>
{{/rows}}

This is acceptable but I would rather do something like this:

{{#entries}}
  <div class="entry">
    <h1>{{title}}</h1>
  </div>
{{/entries}}

How am I able to do this? Is there something that needs to be altered in the view function or can this simply not be done through a view function? Do I need show functions here? If yes, what is the easiest way to create one? Questions, questions, questions...

1

1 Answers

1
votes

If I understand correctly, you want to be able to call CouchDB from CodeIgniter and receive HTML as response instead of JSON. First I'm not sure why you want to do that, because you already have PHP and all that goes with it at your disposal. Templating within CouchDB mostly has sense with applications served directly from it.

Anyway if that's what you want, what you need is a list function. You can call a list function with _design/<doc>/_list/<listname>/<viewname>?vew_params. That way list function will receive a result of your view and transform it in whatever way.

Further if you want to format it with mustache, doing so by hand isn't quite straightforward and is usually done with help of CouchApp. Reason is that from list (nor show/map/reduce) function you can't call other functions defined elsewhere. So your list function must include entire mustache.js library and templates so that it could call it.