From e230dc47fb12bba54af6994753fa52a1bd471090 Mon Sep 17 00:00:00 2001 From: Lance Pollard Date: Sun, 8 Apr 2012 14:35:14 -0700 Subject: [PATCH] test layouts and views for jade, mustache, eco, ejs, and coffee --- lib/tower/view/rendering.js | 19 +++++++++--- package.json | 4 ++- src/tower/view/rendering.coffee | 17 +++++++++-- test/cases/viewTest.coffee | 30 +++++++++++++++++-- test/test-app/app/views/custom/edit.eco | 1 + test/test-app/app/views/custom/edit.ejs | 1 + test/test-app/app/views/custom/edit.jade | 1 + .../app/views/custom/edit.md.mustache | 3 ++ test/test-app/app/views/custom/edit.mustache | 1 + .../app/views/layouts/application.eco | 1 + .../app/views/layouts/application.ejs | 1 + .../app/views/layouts/application.jade | 1 + .../app/views/layouts/application.mustache | 1 + 13 files changed, 70 insertions(+), 11 deletions(-) create mode 100644 test/test-app/app/views/custom/edit.eco create mode 100644 test/test-app/app/views/custom/edit.ejs create mode 100644 test/test-app/app/views/custom/edit.jade create mode 100644 test/test-app/app/views/custom/edit.md.mustache create mode 100644 test/test-app/app/views/custom/edit.mustache create mode 100644 test/test-app/app/views/layouts/application.eco create mode 100644 test/test-app/app/views/layouts/application.ejs create mode 100644 test/test-app/app/views/layouts/application.jade create mode 100644 test/test-app/app/views/layouts/application.mustache diff --git a/lib/tower/view/rendering.js b/lib/tower/view/rendering.js index 6e3a1a5d..1cc6df4c 100644 --- a/lib/tower/view/rendering.js +++ b/lib/tower/view/rendering.js @@ -1,7 +1,14 @@ Tower.View.Rendering = { render: function(options, callback) { - var _this = this; + var type, + _this = this; + if (!options.type && options.template && !options.inline) { + type = options.template.split('/'); + type = type[type.length - 1].split("."); + type = type.slice(1).join(); + options.type = type !== '' ? type : this.constructor.engine; + } options.type || (options.type = this.constructor.engine); if (!options.hasOwnProperty("layout") && this._context.layout) { options.layout = this._context.layout(); @@ -88,8 +95,12 @@ Tower.View.Rendering = { return callback(e, result); } else if (options.type) { mint = require("mint"); - engine = require("mint").engine(options.type); - return mint[engine](string, options.locals, callback); + engine = mint.engine(options.type); + if (engine.match(/(eco|mustache)/)) { + return mint[engine](string, options, callback); + } else { + return mint[engine](string, options.locals, callback); + } } else { mint = require("mint"); engine = require("mint"); @@ -118,7 +129,7 @@ Tower.View.Rendering = { prefixes: prefixes }); path || (path = "" + (this.constructor.store().loadPaths[0]) + "/" + template); - cachePath = path.replace(/\.\w+$/, ""); + cachePath = path; result = this.constructor.cache[cachePath] || require('fs').readFileSync(path, 'utf-8').toString(); if (!result) throw new Error("Template '" + template + "' was not found."); return result; diff --git a/package.json b/package.json index cb982eed..612ed1b7 100644 --- a/package.json +++ b/package.json @@ -80,7 +80,9 @@ "gzip": ">= 0.1.0", "forever": ">= 0.8.5", "mongodb": ">= 0.9.9", - "eco": ">= 0.1.0" + "eco": ">= 0.1.0", + "mustache": ">= 0.4.0", + "jade": ">= 0.22.0" }, "scripts": { "test": "mocha $(find test -name \"*Test.coffee\")", diff --git a/src/tower/view/rendering.coffee b/src/tower/view/rendering.coffee index 5316938e..5f23925f 100644 --- a/src/tower/view/rendering.coffee +++ b/src/tower/view/rendering.coffee @@ -1,6 +1,12 @@ # @mixin Tower.View.Rendering = render: (options, callback) -> + if !options.type && options.template && !options.inline + type = options.template.split('/') + type = type[type.length - 1].split(".") + type = type[1..-1].join() + options.type = if type != '' then type else @constructor.engine + options.type ||= @constructor.engine options.layout = @_context.layout() if !options.hasOwnProperty("layout") && @_context.layout options.locals = @_renderingContext(options) @@ -74,8 +80,12 @@ Tower.View.Rendering = callback e, result else if options.type mint = require "mint" - engine = require("mint").engine(options.type) - mint[engine](string, options.locals, callback) + engine = mint.engine(options.type) + # need to fix this on mint.js repo + if engine.match(/(eco|mustache)/) + mint[engine] string, options, callback + else + mint[engine](string, options.locals, callback) else mint = require "mint" engine = require("mint") @@ -98,7 +108,8 @@ Tower.View.Rendering = return template unless typeof template == "string" path = @constructor.store().findPath(path: template, ext: ext, prefixes: prefixes) path ||= "#{@constructor.store().loadPaths[0]}/#{template}" - cachePath = path.replace(/\.\w+$/, "") + #cachePath = path.replace(/\.\w+$/, "") + cachePath = path result = @constructor.cache[cachePath] || require('fs').readFileSync(path, 'utf-8').toString() throw new Error("Template '#{template}' was not found.") unless result result diff --git a/test/cases/viewTest.coffee b/test/cases/viewTest.coffee index 9c0d183f..fe14f0ea 100644 --- a/test/cases/viewTest.coffee +++ b/test/cases/viewTest.coffee @@ -29,7 +29,31 @@ describe 'Tower.View', -> path = store.findPath(path: 'edit', ext: 'coffee', prefixes: ['custom2']) assert.equal path, 'test/test-app/app/views/custom2/edit.coffee' + + for engine in ['jade', 'ejs', 'eco', 'mustache'] + do (engine) -> + describe engine, -> + test "findPath(path: 'edit', ext: '#{engine}', prefixes: ['custom'])", -> + path = store.findPath(path: 'edit', ext: engine, prefixes: ['custom']) - #test 'render', (done) -> - # view.render template: 'asdf', -> - # done() \ No newline at end of file + assert.equal path, "test/test-app/app/views/custom/edit.#{engine}" + + test "render(template: 'custom/edit.#{engine}')", (done) -> + view.render template: "custom/edit.#{engine}", locals: ENGINE: engine, (error, body) -> + assert.equal body, "

I'm #{engine}!

" + done() + + test "render(template: 'custom/edit', type: '#{engine}')", (done) -> + view.render template: 'custom/edit', type: engine, locals: ENGINE: engine, (error, body) -> + assert.equal body, "

I'm #{engine}!

" + done() + + test "render(template: 'edit', type: '#{engine}', prefixes: ['custom'])", (done) -> + view.render template: 'edit', type: engine, prefixes: ['custom'], locals: ENGINE: engine, (error, body) -> + assert.equal body, "

I'm #{engine}!

" + done() + + test "render(template: 'custom/edit', type: '#{engine}', layout: 'application')", (done) -> + view.render template: 'custom/edit', type: engine, layout: 'application', locals: ENGINE: engine, (error, body) -> + assert.equal body, "

I'm #{engine}!

" + done() \ No newline at end of file diff --git a/test/test-app/app/views/custom/edit.eco b/test/test-app/app/views/custom/edit.eco new file mode 100644 index 00000000..15d49ad1 --- /dev/null +++ b/test/test-app/app/views/custom/edit.eco @@ -0,0 +1 @@ +

I'm <%= @ENGINE %>!

\ No newline at end of file diff --git a/test/test-app/app/views/custom/edit.ejs b/test/test-app/app/views/custom/edit.ejs new file mode 100644 index 00000000..9f03ea36 --- /dev/null +++ b/test/test-app/app/views/custom/edit.ejs @@ -0,0 +1 @@ +

I'm <%= ENGINE %>!

\ No newline at end of file diff --git a/test/test-app/app/views/custom/edit.jade b/test/test-app/app/views/custom/edit.jade new file mode 100644 index 00000000..7c89d70f --- /dev/null +++ b/test/test-app/app/views/custom/edit.jade @@ -0,0 +1 @@ +h1 I'm #{ENGINE}! \ No newline at end of file diff --git a/test/test-app/app/views/custom/edit.md.mustache b/test/test-app/app/views/custom/edit.md.mustache new file mode 100644 index 00000000..f4590a29 --- /dev/null +++ b/test/test-app/app/views/custom/edit.md.mustache @@ -0,0 +1,3 @@ +## I'm {{ENGINE}}! + +SOON :) \ No newline at end of file diff --git a/test/test-app/app/views/custom/edit.mustache b/test/test-app/app/views/custom/edit.mustache new file mode 100644 index 00000000..5a848dbd --- /dev/null +++ b/test/test-app/app/views/custom/edit.mustache @@ -0,0 +1 @@ +

I'm {{ENGINE}}!

\ No newline at end of file diff --git a/test/test-app/app/views/layouts/application.eco b/test/test-app/app/views/layouts/application.eco new file mode 100644 index 00000000..08abbb34 --- /dev/null +++ b/test/test-app/app/views/layouts/application.eco @@ -0,0 +1 @@ +<%- @body %> \ No newline at end of file diff --git a/test/test-app/app/views/layouts/application.ejs b/test/test-app/app/views/layouts/application.ejs new file mode 100644 index 00000000..59593a0e --- /dev/null +++ b/test/test-app/app/views/layouts/application.ejs @@ -0,0 +1 @@ +<%- body %> \ No newline at end of file diff --git a/test/test-app/app/views/layouts/application.jade b/test/test-app/app/views/layouts/application.jade new file mode 100644 index 00000000..69b9366c --- /dev/null +++ b/test/test-app/app/views/layouts/application.jade @@ -0,0 +1 @@ +!= body \ No newline at end of file diff --git a/test/test-app/app/views/layouts/application.mustache b/test/test-app/app/views/layouts/application.mustache new file mode 100644 index 00000000..4839ad4c --- /dev/null +++ b/test/test-app/app/views/layouts/application.mustache @@ -0,0 +1 @@ +{{{body}}} \ No newline at end of file