Permalink
Browse files

Better example and fix to use EJS's own include.

Recent versions of EJS have an include directive for composing
templates so the one I wrote for ejs-locals was no longer working.

This commit simply removes the broken include and rewrites the
tests and examples for ejs-locals to use the built-in include from
EJS itself.

Closes #17 and maybe #10.
  • Loading branch information...
1 parent 053f764 commit 9cd8bb3429139a0c8fd276b953bb669d6757002f @RandomEtc committed Nov 12, 2012
View
@@ -10,7 +10,15 @@ app.set('view engine', 'ejs'); // so you can render('index')
// render 'index' into 'boilerplate':
app.get('/',function(req,res,next){
- res.render('index', { what: 'best', who: 'me' });
+ res.render('index', { what: 'best', who: 'me', muppets: [ 'Kermit', 'Fozzie', 'Gonzo' ] });
});
+app.get('/foo.js', function(req,res,next){
+ res.sendfile('foo.js');
+})
+
+app.get('/foo.css', function(req,res,next){
+ res.sendfile('foo.css');
+})
+
app.listen(3000);
View
@@ -0,0 +1,7 @@
+body {
+ font: normal 16px/1.5 sans-serif;
+ margin: 50px;
+}
+.better-than-dead {
+ color: red;
+}
View
@@ -0,0 +1,7 @@
+
+window.onload = function() {
+ var p = document.createElement('p')
+ p.innerText = 'P.S. foo.js was successfully loaded';
+ document.body.appendChild(p);
+}
+
View
@@ -0,0 +1,6 @@
+<p>It's not often we iterate over the Muppets twice, but in this case it seems prudent to try from 'extras.ejs':
+<ul>
+<%-partial('muppet',muppets) %>
+<ul>
+</p>
+
View
@@ -2,5 +2,10 @@
<% script('foo.js') -%>
<% stylesheet('foo.css') -%>
<h1>I am the <%=what%> template</h1>
+<p class="better-than-dead">I'm red if foo.css was loaded.</p>
+<p>Here are some muppets we know about:<ul>
+ <%-partial('muppet',muppets) %>
+</ul></p>
+<% include extra %>
<% block('header', "<p>I'm in the header.</p>") -%>
<% block('footer', "<p>I'm in the footer.</p>") -%>
View
@@ -0,0 +1 @@
+<li><%=muppet%></li>
View
@@ -80,7 +80,6 @@ var renderFile = module.exports = function(file, options, fn){
}
// override locals for layout/include/partial bound to current options
options.locals.layout = layout.bind(options);
- options.locals.include = include.bind(options);
options.locals.partial = partial.bind(options);
ejs.renderFile(file, options, function(err, html) {
@@ -391,37 +390,6 @@ function layout(view){
this.locals._layoutFile = view;
}
-/**
- * Apply the current `options` to the given `view` to be included
- * in the current template at call time.
- *
- * `options` are bound in renderFile, you just call `include('myview')`
- *
- * @param {String} view
- * @api private
- */
-function include(view) {
-
- // find view, relative to this filename (this == original options)
- // (FIXME: options.filename is set by ejs engine, other engines may need more help)
- var root = dirname(this.filename)
- , engine = this.settings['view engine'] || 'ejs'
- , desiredExt = '.' + engine
- , ext = extname(view)
- , file = join(root, view + (ext ? '' : desiredExt))
- , key = file + ':string';
-
- // read view
- var source = this.cache
- ? cache[key] || (cache[key] = fs.readFileSync(file, 'utf8'))
- : fs.readFileSync(file, 'utf8');
-
- this.filename = file;
-
- // TODO Support other templates (but it's sync now...)
- return ejs.render(source, this);
-}
-
function Block() {
this.html = [];
}
@@ -1 +1 @@
-<%-include('include-chain-2')%>
+<% include include-chain-2 %>
@@ -1 +1 @@
-<%-include('../parent-include-chain')%>
+<% include ../parent-include-chain %>
@@ -1 +1 @@
-<%- include('subitem')%>
+<% include subitem %>
@@ -1 +1 @@
-<html><head><title>ejs-locals-abs</title></head><body><%-include('/locals')%></body></html>
+<html><head><title>ejs-locals-abs</title></head><body><% include /locals %></body></html>
@@ -1 +1 @@
-<html><head><title>ejs-locals-abs-sub</title></head><body><%-include('/subfolder/sublocals')%></body></html>
+<html><head><title>ejs-locals-abs-sub</title></head><body><% include /subfolder/sublocals %></body></html>
@@ -1 +1 @@
-<html><head><title>ejs-locals-include-sub</title></head><body><%-include('subfolder/sub-include-chain')%></body></html>
+<html><head><title>ejs-locals-include-sub</title></head><body><% include subfolder/sub-include-chain %></body></html>
@@ -1 +1 @@
-<html><head><title>ejs-locals-include</title></head><body><%-include('include-chain')%></body></html>
+<html><head><title>ejs-locals-include</title></head><body><% include include-chain %></body></html>
@@ -1 +1 @@
-<html><head><title>ejs-locals</title></head><body><%-include('locals')%></body></html>
+<html><head><title>ejs-locals</title></head><body><% include locals %></body></html>
@@ -1 +1 @@
-<html><head><title>ejs-locals-two-includes</title></head><body><%-include('locals')%><%-include('subfolder/sublocals')%></body></html>
+<html><head><title>ejs-locals-two-includes</title></head><body><% include locals %><% include subfolder/sublocals %></body></html>
View
@@ -128,6 +128,8 @@ app.get('/filters-custom',function(req,res,next){
// override the default error handler so it doesn't log to console:
app.use(function(err,req,res,next) {
res.send(500, err.stack);
+ // console.log(err.stack);
+ // next();
})
describe('app',function(){

0 comments on commit 9cd8bb3

Please sign in to comment.