RandomEtc / ejs-locals

Express 3.x layout, partial and block template functions for the EJS template engine.
298 stars 63 forks source link

string is not a function #28

Closed samholmes closed 11 years ago

samholmes commented 11 years ago

Stupid error:

TypeError: /Users/holmes/Code/Bleep/app/views/secrets/secrets.ejs:1
 >> 1| <? layout('layout.ejs') ?>
    2| <?
    3| var feedback = req.session.feedback;
    4| ?>

string is not a function
    at eval (eval at <anonymous> (/Users/holmes/Code/Bleep/app/node_modules/ejs-locals/node_modules/ejs/lib/ejs.js:234:12))
    at exports.compile (/Users/holmes/Code/Bleep/app/node_modules/ejs-locals/node_modules/ejs/lib/ejs.js:239:15)
    at Object.exports.render (/Users/holmes/Code/Bleep/app/node_modules/ejs-locals/node_modules/ejs/lib/ejs.js:277:13)
    at Object.exports.renderFile (/Users/holmes/Code/Bleep/app/node_modules/ejs-locals/node_modules/ejs/lib/ejs.js:303:22)
    at View.module.exports [as engine] (/Users/holmes/Code/Bleep/app/node_modules/ejs-locals/index.js:85:7)
    at View.render (/Users/holmes/Code/Bleep/app/node_modules/express/lib/view.js:75:8)
    at Function.app.render (/Users/holmes/Code/Bleep/app/node_modules/express/lib/application.js:504:10)
    at ServerResponse.res.render (/Users/holmes/Code/Bleep/app/node_modules/express/lib/response.js:753:7)
    at Function.controller.secrets (/Users/holmes/Code/Bleep/app/controllers/secrets.js:45:7)
    at /Users/holmes/Code/Bleep/app/node_modules/yarn/yarn.js:5:41

Child template:

<? layout('layout.ejs') ?>
<?
var feedback = req.session.feedback;
?>
<div ...

Part of the app.js file:

// App Configuration

app.configure(function(){
    app.engine('ejs', require('ejs-locals'));
    app.set('views', __dirname + '/views');
    app.set('view engine', 'ejs');

    app.locals.layout = 'layout.ejs'
    app.locals.open = '<?';
    app.locals.close = '?>';

What else do I need to post that would help with this problem? I'm following the instructions on how to use this library, but it doesn't work.

samholmes commented 11 years ago

app.locals.layout = 'layout.ejs'; was the culprit

RandomEtc commented 11 years ago

Yep. If you do app.locals.layout = 'foo.ejs' then it overwrites the layout helper and you can't call it from templates.

See https://github.com/RandomEtc/ejs-locals/#backwards-compatibility - the short answer is use app.locals._layoutFile = 'foo.ejs' if you want to set the layout from your app code, and use layout('foo') if you want to set it from your template.