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

ecto handlebars with improved partial support #156

Merged
merged 1 commit into from
Jan 31, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions src/engines/handlebars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import type {EngineInterface} from '../engine-interface.js';

export class Handlebars extends BaseEngine implements EngineInterface {
public partialsPath = '/partials';
public partialsPath = ['partials', 'includes', 'templates'];

constructor(options?: Record<string, unknown>) {
super();
Expand All @@ -20,7 +20,7 @@
async render(source: string, data?: Record<string, unknown>): Promise<string> {
// Register partials
if (this.rootTemplatePath) {
this.registerPartials(this.rootTemplatePath + this.partialsPath);
this.initPartials();
}

// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
Expand All @@ -36,7 +36,7 @@
renderSync(source: string, data?: Record<string, unknown>): string {
// Register partials
if (this.rootTemplatePath) {
this.registerPartials(this.rootTemplatePath + this.partialsPath);
this.initPartials();
}

// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
Expand All @@ -49,17 +49,36 @@
return result;
}

initPartials(): void {
for (const path of this.partialsPath) {
const fullPath = `${this.rootTemplatePath}/${path}`;
this.registerPartials(fullPath);
}
}

registerPartials(partialsPath: string): boolean {
let result = false;
if (fs.pathExistsSync(partialsPath)) {
const partials = fs.readdirSync(partialsPath);

for (const p of partials) {
const source = fs.readFileSync(partialsPath + '/' + p).toString();
const name = p.split('.')[0];

if (handlebars.partials[name] === undefined) {
handlebars.registerPartial(name, handlebars.compile(source));
if (fs.statSync(partialsPath + '/' + p).isDirectory()) {
const dirPartials = fs.readdirSync(partialsPath + '/' + p);
for (const dp of dirPartials) {
const source = fs.readFileSync(partialsPath + '/' + p + '/' + dp).toString();
const name = p + '/' + dp.split('.')[0];
console.log(name);
if (handlebars.partials[name] === undefined) {

Check warning on line 71 in src/engines/handlebars.ts

View workflow job for this annotation

GitHub Actions / build (16)

Blocks are nested too deeply (5). Maximum allowed is 4.

Check warning on line 71 in src/engines/handlebars.ts

View workflow job for this annotation

GitHub Actions / build (18)

Blocks are nested too deeply (5). Maximum allowed is 4.

Check warning on line 71 in src/engines/handlebars.ts

View workflow job for this annotation

GitHub Actions / build (20)

Blocks are nested too deeply (5). Maximum allowed is 4.
handlebars.registerPartial(name, handlebars.compile(source));
}
}
} else {
const source = fs.readFileSync(partialsPath + '/' + p).toString();
const name = p.split('.')[0];

if (handlebars.partials[name] === undefined) {
handlebars.registerPartial(name, handlebars.compile(source));
}
}
}

Expand Down
1 change: 1 addition & 0 deletions test/data/handlebars/example1.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@

{{> foo }}

{{> layout/ux }}
1 change: 1 addition & 0 deletions test/data/handlebars/includes/blah.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Blah
1 change: 1 addition & 0 deletions test/data/handlebars/includes/layout/ux.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ux layout
1 change: 1 addition & 0 deletions test/ecto.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ it('Render from Template - Handlebars', async () => {
const source = await ecto.renderFromFile(testRootDir + '/handlebars/example1.hbs', handlebarsExampleData, testRootDir + '/handlebars');

expect(source).toContain('<title>Alan O\'Connor - Header Title </title>');
expect(source).toContain('Foo!');
});

it('Find Template without Extension', async () => {
Expand Down
6 changes: 4 additions & 2 deletions test/engines/handlebars.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@ it('Handlebars - Rendering with Partials', async () => {
const engine = new Handlebars();
const source = await fs.readFile(testTemplateDir + '/example1.hbs', 'utf8');
engine.rootTemplatePath = testTemplateDir;
const result = await engine.render(source, exampleData1);

expect(await engine.render(source, exampleData1)).toContain('Alan O\'Connor');
expect(await engine.render(source, exampleData1)).toContain('Foo!');
expect(result).toContain('Alan O\'Connor');
expect(result).toContain('Foo!');
expect(result).toContain('ux layout');
});

it('Handlebars - Render Sync with Partials', () => {
Expand Down
Loading