1

I want to include a HTML file in a jade template. The path and name of the file is a variable/parameter which is sent by the server after the user request.

Snippet of index.js

app.get('/material/*', function(req, res){
  var lpath =  "../static/show/" + req.name;  // req.name looks like newSite.html
  res.render("newView.jade", {lpath: lpath});
});

code of newView.jade

include !{lpath}

but then the browser shows

Error: E:\Git\GitHub\chat-example\views\lecture.jade:1
  > 1| include !{lpath} 
    2| 
ENOENT: no such file or directory, open 'E:\newSite\views\!{lpath}.jade'
 at Error (native)
 at Object.fs.openSync (fs.js:584:18)

I tested whether lpath is correct and it is. If I add the lpath i that way

include ../static/show/newSite.html

everything is fine. I've also tried the pipe (see Use a variable in a Jade include)

|!{lpath}

Thanks for any ideas and hints.

Community
  • 1
  • 1
JoV
  • 105
  • 1
  • 11

1 Answers1

0

Passing lpath to render() will not work. I also had the same problem what you will need to do is compile the files manually and then concatinate then something like

var ejs = require('ejs');
var fs = require('fs');
var path = require('path');

app.get('/material/*', function(req, res){
  var htmlPart = ejs.compile(
      fs.readFileSync(path.join("../static/show/",req.name), 'utf8')); 
  var ejsPart = ejs.compile(
      fs.readFileSync(path.join("../relative_path_to_newView.jade","req.name"), 'utf8'));
  var resBody = htmlPart + ejsPart;
  res.send(resBody);
    });

you may use asynchronous functions also hope it helps

FastTurtle
  • 1,747
  • 11
  • 15
  • Thanks for the quick answer. It works fine now. But there is a small error in your code. See comments in your snippet. – JoV Mar 11 '16 at 13:41