Skip to content

Commit

Permalink
test layouts and views for jade, mustache, eco, ejs, and coffee
Browse files Browse the repository at this point in the history
  • Loading branch information
lancejpollard committed Apr 8, 2012
1 parent 8ff3483 commit e230dc4
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 11 deletions.
19 changes: 15 additions & 4 deletions 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();
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -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\")",
Expand Down
17 changes: 14 additions & 3 deletions 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)
Expand Down Expand Up @@ -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")
Expand All @@ -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
Expand Down
30 changes: 27 additions & 3 deletions test/cases/viewTest.coffee
Expand Up @@ -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()
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, "<h1>I'm #{engine}!</h1>"
done()

test "render(template: 'custom/edit', type: '#{engine}')", (done) ->
view.render template: 'custom/edit', type: engine, locals: ENGINE: engine, (error, body) ->
assert.equal body, "<h1>I'm #{engine}!</h1>"
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, "<h1>I'm #{engine}!</h1>"
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, "<h1>I'm #{engine}!</h1>"
done()
1 change: 1 addition & 0 deletions test/test-app/app/views/custom/edit.eco
@@ -0,0 +1 @@
<h1>I'm <%= @ENGINE %>!</h1>
1 change: 1 addition & 0 deletions test/test-app/app/views/custom/edit.ejs
@@ -0,0 +1 @@
<h1>I'm <%= ENGINE %>!</h1>
1 change: 1 addition & 0 deletions test/test-app/app/views/custom/edit.jade
@@ -0,0 +1 @@
h1 I'm #{ENGINE}!
3 changes: 3 additions & 0 deletions test/test-app/app/views/custom/edit.md.mustache
@@ -0,0 +1,3 @@
## I'm {{ENGINE}}!

SOON :)
1 change: 1 addition & 0 deletions test/test-app/app/views/custom/edit.mustache
@@ -0,0 +1 @@
<h1>I'm {{ENGINE}}!</h1>
1 change: 1 addition & 0 deletions test/test-app/app/views/layouts/application.eco
@@ -0,0 +1 @@
<%- @body %>
1 change: 1 addition & 0 deletions test/test-app/app/views/layouts/application.ejs
@@ -0,0 +1 @@
<%- body %>
1 change: 1 addition & 0 deletions test/test-app/app/views/layouts/application.jade
@@ -0,0 +1 @@
!= body
1 change: 1 addition & 0 deletions test/test-app/app/views/layouts/application.mustache
@@ -0,0 +1 @@
{{{body}}}

0 comments on commit e230dc4

Please sign in to comment.