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.

0

1 Answer 1

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

1
  • 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, 2016 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.