jstransformers / jstransformer-nunjucks

Nunjucks support for JSTransformers.
http://npm.im/jstransformer-nunjucks
MIT License
6 stars 6 forks source link

Include only works for file at the same level or below #10

Closed ismay closed 6 years ago

ismay commented 8 years ago

See this test repo: https://github.com/ismay/nunjucks-include

Running node index.js:

// Deps
var nunjucks = require('jstransformer')(require('jstransformer-nunjucks'));
var fs = require('fs');
var path = require('path');

// Render
function render (file) {
  var filename = path.join(__dirname, file);
  var contents = fs.readFileSync(filename).toString();
  var locals = null;
  var options = { filename: filename };

  return nunjucks.render(contents, options, locals).body;
}

// Outcome
console.log(render('src/include-sibling.nunjucks'));
console.log(render('src/include-child.nunjucks'));
console.log(render('src/include-parent.nunjucks'));

Demonstrates how includes work for files on the same level, or below it, but fail for includes of files located above the parent. It fails with the following error:

Template render error: (/Users/ismay/Projects/nunjucks/src/include-parent.nunjucks)
  Template render error: (/Users/ismay/Projects/nunjucks/src/include-parent.nunjucks)
  Error: template not found: /Users/ismay/Projects/nunjucks/lib/include.html
    at Object.exports.prettifyError (/Users/ismay/Projects/nunjucks/node_modules/jstransformer-nunjucks/node_modules/nunjucks/src/lib.js:34:15)
    at /Users/ismay/Projects/nunjucks/node_modules/jstransformer-nunjucks/node_modules/nunjucks/src/environment.js:485:31
    at root [as rootRenderFunc] (eval at <anonymous> (/Users/ismay/Projects/nunjucks/node_modules/jstransformer-nunjucks/node_modules/nunjucks/src/environment.js:564:24), <anonymous>:21:3)
    at Obj.extend.render (/Users/ismay/Projects/nunjucks/node_modules/jstransformer-nunjucks/node_modules/nunjucks/src/environment.js:478:15)
    at Transformer.render (/Users/ismay/Projects/nunjucks/node_modules/jstransformer/index.js:289:25)
    at render (/Users/ismay/Projects/nunjucks/index.js:13:19)
    at Object.<anonymous> (/Users/ismay/Projects/nunjucks/index.js:19:13)
    at Module._compile (module.js:435:26)
    at Object.Module._extensions..js (module.js:442:10)
    at Module.load (module.js:356:32)

Could not find anything in the nunjucks' documentation indicating that this should not work. Any idea whether this is related to this lib, or is it nunjucks related?

The weird thing is that the path in the error Error: template not found: /Users/ismay/Projects/nunjucks/lib/include.html exists..

RobLoach commented 8 years ago

:+1:

Strange that var template = nunjucks.compile(str, env, opts.filename || null, true); isn't applying the path correctly. https://mozilla.github.io/nunjucks/api.html#compile

ismay commented 8 years ago

Yeah, it seems like it should be working.

ismay commented 7 years ago

Hey @RobLoach, just wondering if there's any news on this bug. Had someone run into it again: https://github.com/superwolff/metalsmith-in-place/issues/69

RobLoach commented 7 years ago

Found that you can use .configure(path) to somehow set a root directory. We may be able to have options.root become that base directory for templates.

samuelgoldenbaum commented 7 years ago

Any progress on this issue? Seeing the same with extends

{% extends "../layouts/layout.njk" %}

RobLoach commented 7 years ago

Attempted over at https://github.com/jstransformers/jstransformer-nunjucks/pull/16 .

saneef commented 7 years ago

Any luck with this issue?

RobLoach commented 7 years ago

https://github.com/jstransformers/jstransformer-nunjucks/blob/master/index.js#L16

Think we should fix up the path detection a bit better? Any thoughts? Did some work on this, but it could likely be better.

stephenwf commented 6 years ago

This configuration works for me (in metalsmith):

const metalsmith = require('metalsmith');
const inPlace = require('metalsmith-in-place');

metalsmith(__dirname)
    .source(`${__dirname}/src/views`)
    .destination(`${__dirname}/dist`)
    .use(inPlace({
      engineOptions: {
        root: `${__dirname}/src/views`
      }
    }))
    .build(function(err){
      if (err) throw err;
    });

So I guess you just have to explicitly pass the root