Skip to content

Commit

Permalink
Added support for globals (eg: helpers) in views
Browse files Browse the repository at this point in the history
 - Added export.Globals in plugins/view.js
 - Added two specs (one for a simple template, one for a template with a layout) with a dummy i18n helper
 - Updated README features list
  • Loading branch information
rauchg committed May 13, 2010
1 parent 8ef6a0b commit 216cb1e
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 3 deletions.
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* Route passing
* View support (ejs, haml, sass, etc)
* View partials
* View globals/helpers support
* Full test coverage
* Logger plugin with several formats
* Upload size restrictions
Expand Down
11 changes: 8 additions & 3 deletions lib/express/plugins/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ var engines = {}
* View cache.
*/

var cache = { views: {}, partials: {}}
var cache = { views: {}, partials: {} }

// View globals.

var Globals = exports.Globals = {}

/**
* Cache view files where _type_ is
Expand Down Expand Up @@ -153,6 +157,8 @@ exports.View = Plugin.extend({
layout = options.layout === undefined
? 'layout'
: options.layout
options.locals = options.locals || {}
Object.merge(options.locals, Globals)
options.filename = path
if (set('cache view contents'))
options.cache = true
Expand All @@ -178,5 +184,4 @@ exports.View = Plugin.extend({
})
}
}
})

})
1 change: 1 addition & 0 deletions spec/fixtures/helpers-layout.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Testing
1 change: 1 addition & 0 deletions spec/fixtures/helpers.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
%span= _('Hello world')
1 change: 1 addition & 0 deletions spec/fixtures/layout-helper.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
%span= _(body)
22 changes: 22 additions & 0 deletions spec/spec.plugins.view.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

ejs = require('ejs')
Globals = require('express/plugins/view').Globals

describe 'Express'
before_each
Expand Down Expand Up @@ -213,6 +214,27 @@ describe 'Express'
end
end
describe 'when a global is set'
it 'should include the helper and translate the text in the view'
Globals._ = function(text){
if (text == 'Hello world') return 'Hola mundo'
}
get('/', function(){
this.render('helpers.html.haml', { layout: false })
})
get('/').body.should.include 'Hola mundo'
end
it 'should include the helper and translate the text in the layout'
Globals._ = function(text){
if (text == 'Testing') return 'Probando'
}
get('/', function(){
this.render('helpers-layout.html.haml', { layout: 'layout-helper' })
})
get('/').body.should.include 'Probando'
end
end

describe 'when engine cannot be found'
it 'should throw an error'
get('/', function(){
Expand Down

0 comments on commit 216cb1e

Please sign in to comment.