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

feat: Add partialsDir.rename option #151

Merged
merged 2 commits into from Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -353,6 +353,7 @@ The string path to the directory where the partials templates reside or object w
* `dir`: The string path to the directory where the partials templates reside.
* `namespace`: Optional string namespace to prefix the partial names.
* `templates`: Optional collection (or promise of a collection) of templates in the form: `{filename: template}`.
* `rename(filePath, namespace)`: Optional function to rename the partials. Takes two arguments: `filePath`, e.g., `partials/some/path/template.handlebars` and `namespace`.

**Note:** If you configure Express to look for views in a custom location (e.g., `app.set('views', 'some/path/')`), and if your `partialsDir` is not relative to `express settings.view` + `partials/`, you will need to reflect that by passing an updated path as the `partialsDir` property in your configuration.

Expand Down
11 changes: 9 additions & 2 deletions lib/express-handlebars.js
Expand Up @@ -59,6 +59,7 @@ class ExpressHandlebars {
let dirPath;
let dirTemplates;
let dirNamespace;
let dirRename;

// Support `partialsDir` collection with object entries that contain a
// templates promise and a namespace.
Expand All @@ -67,6 +68,7 @@ class ExpressHandlebars {
} else if (typeof dir === "object") {
dirTemplates = dir.templates;
dirNamespace = dir.namespace;
dirRename = dir.rename;
dirPath = dir.dir;
}

Expand All @@ -80,17 +82,22 @@ class ExpressHandlebars {
return {
templates,
namespace: dirNamespace,
rename: dirRename,
};
}));

const partials = {};

for (const dir of dirs) {
const { templates, namespace } = dir;
const { templates, namespace, rename } = dir;
const filePaths = Object.keys(templates);

const getTemplateNameFn = typeof rename === "function"
? rename
: this._getTemplateName.bind(this);

for (const filePath of filePaths) {
const partialName = this._getTemplateName(filePath, namespace);
const partialName = getTemplateNameFn(filePath, namespace);
partials[partialName] = templates[filePath];
}
}
Expand Down
18 changes: 18 additions & 0 deletions spec/express-handlebars.test.js
Expand Up @@ -71,6 +71,24 @@ describe("express-handlebars", () => {
});
});

test("should return renamed partials with rename function", async () => {
const fn = jest.fn();
const exphbs = expressHandlebars.create({
partialsDir: {
templates: { "partial/template": fn },
namespace: "partial namespace",
dir: fixturePath("partials"),
rename: (filePath, namespace) => {
return `${namespace}/${filePath.split("/")[0]}`;
},
},
});
const partials = await exphbs.getPartials();
expect(partials).toEqual({
"partial namespace/partial": fn,
});
});

test("should return partials on path relative to cwd", async () => {
const exphbs = expressHandlebars.create({ partialsDir: "spec/fixtures/partials" });
const partials = await exphbs.getPartials();
Expand Down