-
-
Notifications
You must be signed in to change notification settings - Fork 519
Begin process of decoupling all templating languages #3747
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
Draft
boehs
wants to merge
6
commits into
11ty:main
Choose a base branch
from
boehs:seperation-of-concerns
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6a23f66
nunjucks filter poc
boehs 52f1cde
liquid filters to universal
boehs de2497a
remove javascript shortcodes, paired shortcodes, and filters (unused)
boehs 5d253ff
remove long deprecated code
boehs e6732a6
a few bug fixes
boehs 76cd065
a few tests
boehs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,6 @@ import { TemplatePath } from "@11ty/eleventy-utils"; | |
import TemplateEngine from "./TemplateEngine.js"; | ||
import EleventyBaseError from "../Errors/EleventyBaseError.js"; | ||
import { augmentObject } from "./Util/ContextAugmenter.js"; | ||
import { withResolvers } from "../Util/PromiseUtil.js"; | ||
|
||
const debug = debugUtil("Eleventy:Nunjucks"); | ||
|
||
|
@@ -20,6 +19,9 @@ export default class Nunjucks extends TemplateEngine { | |
this.nunjucksPrecompiledTemplates = this.config.nunjucksPrecompiledTemplates || {}; | ||
this._usingPrecompiled = Object.keys(this.nunjucksPrecompiledTemplates).length > 0; | ||
|
||
this.asyncFilters = eleventyConfig.config.__theCodeCriesInPain.nunjucks.asyncFilters || {}; | ||
this.filters = eleventyConfig.config.__theCodeCriesInPain.nunjucks.filters | ||
|
||
this.setLibrary(this.config.libraryOverrides.njk); | ||
|
||
// v3.1.0-alpha.1 we’ve moved to use Nunjucks’ internal cache instead of Eleventy’s | ||
|
@@ -28,7 +30,7 @@ export default class Nunjucks extends TemplateEngine { | |
|
||
#getFileSystemDirs() { | ||
let paths = new Set(); | ||
paths.add(super.getIncludesDir()); | ||
paths.add(super.includesDir); | ||
paths.add(TemplatePath.getWorkingDir()); | ||
|
||
// Filter out undefined paths | ||
|
@@ -100,9 +102,8 @@ export default class Nunjucks extends TemplateEngine { | |
}); | ||
|
||
this.setEngineLib(this.njkEnv); | ||
|
||
this.addFilters(this.config.nunjucksFilters); | ||
this.addFilters(this.config.nunjucksAsyncFilters, true); | ||
this.addFilters(this.filters); | ||
this.addFilters(this.asyncFilters, true); | ||
|
||
// TODO these all go to the same place (addTag), add warnings for overwrites | ||
// TODO(zachleat): variableName should work with quotes or without quotes (same as {% set %}) | ||
|
@@ -121,7 +122,35 @@ export default class Nunjucks extends TemplateEngine { | |
|
||
addFilters(filters, isAsync) { | ||
for (let name in filters) { | ||
this.njkEnv.addFilter(name, Nunjucks.wrapFilter(name, filters[name]), isAsync); | ||
let callback = filters[name]; | ||
if (!isAsync) { | ||
/** @this {any} */ | ||
callback = function (...args) { | ||
// Note that `callback` is already a function as the `#add` method throws an error if not. | ||
let ret = filters[name].call(this, ...args); | ||
if (ret instanceof Promise) { | ||
throw new Error( | ||
`Nunjucks *is* async-friendly with \`addFilter("${name}", async function() {})\` but you need to supply an \`async function\`. You returned a promise from \`addFilter("${name}", function() {})\`. Alternatively, use the \`addAsyncFilter("${name}")\` configuration API method.`, | ||
); | ||
} | ||
return ret; | ||
} | ||
} | ||
else if (isAsync) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I could be wrong but it doesn't look like you need an |
||
// we need to wrap the async function in a nunjucks compatible callback | ||
/** @this {any} */ | ||
callback = async function (...args) { | ||
let cb = args.pop(); | ||
// Note that `callback` is already a function as the `#add` method throws an error if not. | ||
let ret = await filters[name].call(this, ...args); | ||
cb(null, ret); | ||
} | ||
} | ||
this.njkEnv.addFilter( | ||
name, | ||
Nunjucks.wrapFilter(name, callback), | ||
isAsync, | ||
); | ||
} | ||
} | ||
|
||
|
@@ -132,7 +161,6 @@ export default class Nunjucks extends TemplateEngine { | |
source: this.ctx, | ||
lazy: false, // context.env?.opts.throwOnUndefined, | ||
}); | ||
|
||
return fn.call(this, ...args); | ||
} catch (e) { | ||
throw new EleventyNunjucksError( | ||
|
@@ -458,17 +486,15 @@ export default class Nunjucks extends TemplateEngine { | |
} | ||
|
||
return function (data) { | ||
let { promise, resolve, reject } = withResolvers(); | ||
|
||
tmpl.render(data, (error, result) => { | ||
if (error) { | ||
reject(error); | ||
} else { | ||
resolve(result); | ||
} | ||
return new Promise((resolve, reject) => { | ||
tmpl.render(data, (error, result) => { | ||
if (error) { | ||
reject(error); | ||
} else { | ||
resolve(result); | ||
} | ||
}) | ||
}); | ||
|
||
return promise; | ||
}; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is this supposed to be catched?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It’s not. This is the same code addFilter added to nunjucks. It has been relocated to the nunjucks file for the purpose of decoupling