Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add query options for nunjucks.configure #1

Open
tapthoppe opened this issue Mar 22, 2016 · 4 comments
Open

add query options for nunjucks.configure #1

tapthoppe opened this issue Mar 22, 2016 · 4 comments

Comments

@tapthoppe
Copy link

need a bit more config options, such as autoescape, trimBlocks, tags
https://mozilla.github.io/nunjucks/api.html#configure

@ryanhornberger
Copy link
Owner

@Ertle hey man, at the moment I'm not putting much work into this but if you have a change you'd like to submit you are welcome to. The LOC on this is pretty small.

@jeremyzahner
Copy link
Collaborator

@Ertle I might have some time to invest in the future. For now we'd happily accept a PR containing the missing features.

@goldfinch
Copy link

Hey guys, thanks for the nice work on this loader.

I was needing to include some custom data from json file to be able to use it in nunjucks templates.
Someone might find it useful for own purposes as well. I created a pull request to achieve this goal in case if you find it useful to have.

warmrobot referenced this issue in warmrobot/nunjucks-html-loader Dec 27, 2017
@HermannBjorgvin
Copy link

HermannBjorgvin commented Apr 24, 2019

I created a local patch for this issue that seems to be working for me.

This is what the loader looks like set up.

/* Nunjucks loader */
const nunjucksOptions = {
  configure: function(env){
    markdown.register(env, marked);
  },
  searchPaths: basePath + '/src/html',
  context: {
    config: {
      'env': 'development',
      'image_src': '../../assets/images/'
    }
  }
};

config.module
    .rule('nunjucks')
    .test(/\.(njk|nunjucks)$/)
    .use('html-loader')
    .loader('html-loader')
    .end()
    .use('nunjucks-html-loader')
    .loader('nunjucks-html-loader')
    .options(nunjucksOptions);

I added two lines into index.js to support this. This will probably fault with a type error if you skip the configuration option. So it's probably best to enclose the nunjucksConfiguration function in an if typeof statement.

module.exports = function(content) {
	this.cacheable();
  
	var callback = this.async();
	var opt = utils.parseQuery(this.query);

	var nunjucksSearchPaths = opt.searchPaths;
	var nunjucksContext = opt.context;
	var nunjucksConfiguration = opt.configure;

	var loader = new NunjucksLoader(nunjucksSearchPaths, function(path) {
		this.addDependency(path);
	}.bind(this));

	var nunjEnv = new nunjucks.Environment(loader);
	nunjucks.configure(null, { watch: false });

	nunjucksConfiguration(nunjEnv);
	
	var template = nunjucks.compile(content, nunjEnv);
	html = template.render(nunjucksContext);

	callback(null, html);
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants