Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

trying to render html files using jade but it still adresses it as a jade

i have read theses two posts regrading my issue:

    http://stackoverflow.com/questions/4529586/render-basic-html-view-in-node-js-express
http://stackoverflow.com/questions/12046421/how-to-configure-express-js-jade-to-process-html-files

and my code is as follows:

app.engine('.html', require('jade').__express);
app.set('views', __dirname + '/app/views');
app.set('view engine', 'html');

it is clear that for some reason it is trying to read index as if it was still a jade file and thus i am getting the error. i believe i configured it correct to server a .html file. what is the problem here? i am getting lost...

i have tried to reorder the lines but still same error.

but for some reason i get this error:

Error: ....\views\index.html:4
    2| <html lang="en" ng-app="squashApp">
    3| <head>
  > 4|   <meta charset="utf-8">
    5|   <title>Squash Organizer</title>
    6|   <link rel="stylesheet" href="css/app.css"/>
    7|   <link rel="stylesheet" href="css/bootstrap.css"/>

unexpected token "indent"
    at Object.Parser.parseExpr (C:\Users\workspace\squash\node_modules\jade\lib\parser.js:241:15)
    at Object.Parser.parse (C:\Users\workspace\squash\node_modules\jade\lib\parser.js:133:25)
    at parse (C:\Users\workspace\squash\node_modules\jade\lib\jade.js:93:62)
    at Object.exports.compile (C:\Users\workspace\squash\node_modules\jade\lib\jade.js:156:9)
    at Object.exports.render (C:\Users\workspace\squash\node_modules\jade\lib\jade.js:210:15)
    at Object.exports.renderFile (C:\Users\workspace\squash\node_modules\jade\lib\jade.js:247:18)
    at View.exports.renderFile [as engine] (C:\Users\workspace\squash\node_modules\jade\lib\jade.js:232:21)
    at View.render (C:\Users\workspace\squash\node_modules\express\lib\view.js:76:8)
    at Function.app.render (C:\Users\workspace\squash\node_modules\express\lib\application.js:505:10)
    at ServerResponse.res.render (C:\Users\workspace\squash\node_modules\express\lib\response.js:756:7)

thanks.

like image 930
lobengula3rd Avatar asked Oct 19 '25 01:10

lobengula3rd


1 Answers

With this line:

app.engine('.html', require('jade').__express);

You told express to render templates ending with .html using jade.

With this one:

app.set('view engine', 'html');

you told express that it should interpret template names without extension as ones ending with html.

So my guess is that you're trying to render 'index', express interprets it as index.html and then passes to jade as it was instructed to do.

It's better to map jade to it's own extentension (.jade is an obvious candidate). And render your index.html using it's full name.

  1. Add consolidate.js to your project:

    var engines = require('consolidate');
    
  2. Tell jade to render stuff ending with .jade.

    app.engine('jade', require('jade').__express);
    
  3. Register simple html renderer for stuff ending with .html:

    app.engine('html', function(str, options) {
       return function(locals) {
           return str;
       };
    });
    
  4. Tell express to render templates with no extension as jade:

    app.set('view engine', 'jade');
    
  5. Use this to render your index:

    res.render('index.html');
    

    and this:

    res.render('view-name-without-extension'); 
    

    to render jade template.

like image 120
soulcheck Avatar answered Oct 21 '25 15:10

soulcheck