Skip to content

Commit

Permalink
Tweaks to #2250
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Dec 14, 2022
1 parent 562b2a7 commit 0807bf2
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 47 deletions.
14 changes: 2 additions & 12 deletions src/Engines/Handlebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,10 @@ class Handlebars extends TemplateEngine {
this.handlebarsLib.registerHelper(name, callback);
}

static wrapHelper(callback) {
return function () {
const newThis = {
...this,
ctx: this,
// page: this.page
};
return callback.call(newThis, ...arguments);
};
}

addHelpers(helpers) {
for (let name in helpers) {
this.addHelper(name, Handlebars.wrapHelper(helpers[name]));
// We don’t need to wrap helpers for `page` or `eleventy`, this is provided for free by Handlebars
this.addHelper(name, helpers[name]);
}
}

Expand Down
28 changes: 13 additions & 15 deletions src/Engines/JavaScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class JavaScript extends TemplateEngine {
return getJavaScriptData(inst, inputPath);
}

getJavaScriptFunctions(inst, data) {
getJavaScriptFunctions(inst) {
let fns = {};
let configFns = this.config.javascriptFunctions;

Expand All @@ -109,24 +109,22 @@ class JavaScript extends TemplateEngine {
// do nothing
} else {
// note: wrapping creates a new function
fns[key] = JavaScript.wrapJavaScriptFunction(
inst,
data,
configFns[key]
);
fns[key] = JavaScript.wrapJavaScriptFunction(inst, configFns[key]);
}
}
return fns;
}

static wrapJavaScriptFunction(inst, data, fn) {
return function () {
const newThis = {
...this,
ctx: data,
page: inst.page,
};
return fn.call(newThis, ...arguments);
static wrapJavaScriptFunction(inst, fn) {
return function (...args) {
if (inst && inst.page) {
this.page = inst.page;
}
if (inst && inst.eleventy) {
this.eleventy = inst.eleventy;
}

return fn.call(this, ...args);
};
}

Expand All @@ -145,7 +143,7 @@ class JavaScript extends TemplateEngine {
if (!inst.page || inst.page.url) {
inst.page = data.page;
}
Object.assign(inst, this.getJavaScriptFunctions(inst, data));
Object.assign(inst, this.getJavaScriptFunctions(inst));

return this.normalize(inst.render.call(inst, data));
}.bind(this);
Expand Down
17 changes: 8 additions & 9 deletions src/Engines/Liquid.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,14 @@ class Liquid extends TemplateEngine {
this.liquidLib.registerFilter(name, Liquid.wrapFilter(filter));
}

static wrapFilter(filter) {
return function () {
let ctx = this.context.environments;
const newThis = {
...this,
ctx: ctx,
page: ctx.page,
};
return filter.call(newThis, ...arguments);
static wrapFilter(fn) {
return function (...args) {
if (this.context && "get" in this.context) {
this.page = this.context.get(["page"]);
this.eleventy = this.context.get(["eleventy"]);
}

return fn.call(this, ...args);
};
}

Expand Down
24 changes: 13 additions & 11 deletions src/Engines/Nunjucks.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,22 @@ class Nunjucks extends TemplateEngine {
this.addGlobals(this.config.nunjucksGlobals);
}

addFilters(helpers, isAsync) {
for (let name in helpers) {
this.njkEnv.addFilter(name, Nunjucks.wrapFilter(helpers[name]), isAsync);
addFilters(filters, isAsync) {
for (let name in filters) {
this.njkEnv.addFilter(name, Nunjucks.wrapFilter(filters[name]), isAsync);
}
}

static wrapFilter(filter) {
return function () {
const newThis = {
...this,
// ctx: this.ctx,
page: this.ctx.page,
};
return filter.call(newThis, ...arguments);
static wrapFilter(fn) {
return function (...args) {
if (this.ctx && this.ctx.page) {
this.page = this.ctx.page;
}
if (this.ctx && this.ctx.eleventy) {
this.eleventy = this.ctx.eleventy;
}

return fn.call(this, ...args);
};
}

Expand Down

0 comments on commit 0807bf2

Please sign in to comment.