You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Michael Lenahan <mi...@gmail.com> on 2010/08/16 18:04:39 UTC

Mustache? CommonJS?

Hi everyone

I'm trying to get my head around CouchApp, by going back to first principles
and trying to write an application for myself *without* CouchApp - just so
as I can understand the building blocks that CouchDB itself provides.

I wrote this blog piece to try to explain some of it to myself.

http://klena02.wordpress.com/2010/08/16/three-steps-to-couchdb-heaven/

I got as far as this:
http://mick.couchone.com/universities/_design/default/_list/defaultlist/defaultview

Ultimately I want to serve a georss feed of all the locations. I achieved
something similar before using couchapp + sofa, but now I'm trying to do
just out of CouchDB itself, as a learning exercise.

Here are some questions I have:

1. I wasn't able to work out how templating languages like mustache fit in.
Are there any ways to use mustache inside a "show" to make authoring the
show more easy?
For example if you see the googlemap show in this design document: , I'd
like to use a templating language to add doc.latitude and doc.longitude to
the show.

I read this: http://blog.couch.io/post/622014913/mustache-js but I'm still
missing how to fit it in to a CouchDB show.

2. What is CommonJS and why does it matter. I worked through the example
here http://wiki.apache.org/couchdb/CommonJS_Modules but I can't make
practical sense of it.

Thanks for your help.

Michael

Re: Mustache? CommonJS?

Posted by Michael Lenahan <mi...@gmail.com>.
Thanks Jason, that's very helpful. I'll post my experiences back to the
list.

On 18 August 2010 14:24, Jason Smith <jh...@couch.io> wrote:

> Hi, Michael. Glad some of this is making some sense!
>
> On Wed, Aug 18, 2010 at 02:19, Michael Lenahan <michael.lenahan@gmail.com
> >wrote:
>
> > When you say "<insert mustache.js CommonJS file here>" what do you mean?
> >
>
> You can literally copy and paste the mustache.js file that you download.
> There is some confusion because the **project** is "mustache dot jay ess"
> but there is also a source code file called mustache.js which is not the
> CommonJS version, it is just the core code.
>
> The trick to making sure that you have a CommonJS file is because it will
> have the word `exports` somewhere in the file.
>
> Where do you get mustache.js in CommonJS format? Good question.
>
> I just spoke to Jan and apparently you must download the code from GitHub
> and run:
>
>    rake commonjs
>
> That will produce the mustache.js file in the commonjs folder.
>
>
> --
> Jason Smith
> Couchio Hosting
>

Re: Mustache? CommonJS?

Posted by Jason Smith <jh...@couch.io>.
Hi, Michael. Glad some of this is making some sense!

On Wed, Aug 18, 2010 at 02:19, Michael Lenahan <mi...@gmail.com>wrote:

> When you say "<insert mustache.js CommonJS file here>" what do you mean?
>

You can literally copy and paste the mustache.js file that you download.
There is some confusion because the **project** is "mustache dot jay ess"
but there is also a source code file called mustache.js which is not the
CommonJS version, it is just the core code.

The trick to making sure that you have a CommonJS file is because it will
have the word `exports` somewhere in the file.

Where do you get mustache.js in CommonJS format? Good question.

I just spoke to Jan and apparently you must download the code from GitHub
and run:

    rake commonjs

That will produce the mustache.js file in the commonjs folder.


-- 
Jason Smith
Couchio Hosting

Re: Mustache? CommonJS?

Posted by Michael Lenahan <mi...@gmail.com>.
Thanks Jason - sorry, another question.

When you say "<insert mustache.js CommonJS file here>" what do you mean?

I found this:
http://github.com/janl/mustache.js/tree/master/mustache-commonjs but I'm
unsure what I need to copy into the design doc.

On 16 August 2010 19:35, Jason Smith <jh...@couch.io> wrote:

> On Mon, Aug 16, 2010 at 23:04, Michael Lenahan <michael.lenahan@gmail.com
> >wrote:
>
> > 1. I wasn't able to work out how templating languages like mustache fit
> in.
> > Are there any ways to use mustache inside a "show" to make authoring the
> > show more easy?
> >
>
> In a show function, the `this` variable is assigned to the Javascript
> object
> of the design doc. Therefore if your design doc is
>
>    { "_id": "_design/default",
>      "mustache_lib": "<insert mustache.js CommonJS file here>",
>      "an_html_template": "This is a web page called {{name}}",
>      etc.
>    }
>
> Then you can load mustache via CommonJS in the show function.
>
>    function(doc, req) {
>        var mustache = require("mustache_lib");
>        var template = this.an_html_template;
>        return {body: mustache.to_html(template, {name: "Hi!"});
>    }
>
> That is the idea, note I did not test this code just yet but I got it from
> working code I use.
>
> --
> Jason Smith
> Couchio Hosting
>

Re: Mustache? CommonJS?

Posted by Jason Smith <jh...@couch.io>.
On Mon, Aug 16, 2010 at 23:04, Michael Lenahan <mi...@gmail.com>wrote:

> 1. I wasn't able to work out how templating languages like mustache fit in.
> Are there any ways to use mustache inside a "show" to make authoring the
> show more easy?
>

In a show function, the `this` variable is assigned to the Javascript object
of the design doc. Therefore if your design doc is

    { "_id": "_design/default",
      "mustache_lib": "<insert mustache.js CommonJS file here>",
      "an_html_template": "This is a web page called {{name}}",
      etc.
    }

Then you can load mustache via CommonJS in the show function.

    function(doc, req) {
        var mustache = require("mustache_lib");
        var template = this.an_html_template;
        return {body: mustache.to_html(template, {name: "Hi!"});
    }

That is the idea, note I did not test this code just yet but I got it from
working code I use.

-- 
Jason Smith
Couchio Hosting