CouchDB
  1. CouchDB
  2. COUCHDB-693

require function - add support for requiring plain html/xml files in addition to only javascript

    Details

    • Type: Improvement Improvement
    • Status: Resolved
    • Priority: Trivial Trivial
    • Resolution: Fixed
    • Affects Version/s: 0.11
    • Fix Version/s: None
    • Component/s: None
    • Environment:

      MacOS X 10.6.2

      Description

      It seems that for now every require function on the main.js it is created an empty exports object which is returned after the call.
      I would suggest that instead of creating one empty exports object:

      var require = function(name, parent) {
      var exports = {};
      var resolved = resolveModule(name.split('/'), parent, ddoc);
      var source = resolved[0];
      parent = resolved[1];
      ...

      that one pre-populated object could be created:

      var require = function(name, parent) {
      var resolved = resolveModule(name.split('/'), parent, ddoc);
      var source = resolved[0];
      var exports =

      {"source" : source}

      ; /* <-- this would populate the exports with an embedded source */
      parent = resolved[1];

      this done, users would be able to require plain plain html/xml files directly without need to declare any javascript variable or exports...
      this is very nice for templating specifically because javascript support xml syntax without any problem and also it's possible declare javascript variables inside the xml like <p>Hello there,

      {name}

      </p>

      so it would be possible to require something like this
      templates/master.html -->
      <html>
      <head>
      <title>title</title>
      </head>
      <body>
      <p>That's my content</p>
      </body>
      </html>


      and then simply require it using:

      var template = require("templates/master.html");

      send(template.source);

      I'm still trying to adjust it to be possible for user to just user plain text files without quotes which would increase the possibilities for users to create their own view engines such as HAML and SASS.

      In case the user is using just regular javascript he can easily overwrite the source variable with exports.source ...

        Activity

        Hide
        Chris Anderson added a comment -

        Thanks for the report.

        Sorry I didn't see it earlier. The proper way to do this in _show _list etc function (where require is available, is to use 'this' which refers to the design document. So you can load templates like:

        function(doc, req)

        { var template = this.templates.entry; var Mustache = require("lib/mustache"); return Mustache.to_html(template, doc); }

        For more examples see Sofa:

        http://github.com/jchris/sofa/blob/master/shows/edit.js

        Chris

        Show
        Chris Anderson added a comment - Thanks for the report. Sorry I didn't see it earlier. The proper way to do this in _show _list etc function (where require is available, is to use 'this' which refers to the design document. So you can load templates like: function(doc, req) { var template = this.templates.entry; var Mustache = require("lib/mustache"); return Mustache.to_html(template, doc); } For more examples see Sofa: http://github.com/jchris/sofa/blob/master/shows/edit.js Chris

          People

          • Assignee:
            Unassigned
            Reporter:
            Marcos Zanona
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development