Skip to content

Commit

Permalink
Merge pull request #1147 from otonielguajardo/liquidjs-adapter
Browse files Browse the repository at this point in the history
Support liquidjs templates
  • Loading branch information
juandav authored Apr 25, 2024
2 parents aa49b7f + 89547c1 commit b9960b8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
39 changes: 39 additions & 0 deletions lib/adapters/liquid.adapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/** Dependencies **/
import * as path from 'path';
import { get } from 'lodash';

/** Interfaces **/
import { MailerOptions } from '../interfaces/mailer-options.interface';
import { TemplateAdapter } from '../interfaces/template-adapter.interface';
import { Liquid } from 'liquidjs';

export class LiquidAdapter implements TemplateAdapter {
private config: Partial<Liquid['options']>;

constructor(config?: Partial<Liquid['options']>) {
Object.assign(this.config, config);
}

public compile(mail: any, callback: any, mailerOptions: MailerOptions): void {
const { context, template } = mail.data;

const templateExt = path.extname(template) || '.liquid';
const templateName = path.basename(template, path.extname(template));
const templateDir = path.isAbsolute(template)
? path.dirname(template)
: path.join(get(mailerOptions, 'template.dir', ''), path.dirname(template));

const engine = new Liquid({
extname: templateExt,
root: templateDir,
...this.config.globals
});

engine.renderFile(templateName, context).then((body) => {
mail.data.html = body;
return callback();
}).catch((err) => {
return callback(err);
});
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
"handlebars": ">=4.7.6",
"mjml": ">=4.15.3",
"nodemailer": ">=6.4.6",
"pug": ">=3.0.1"
"pug": ">=3.0.1",
"liquidjs": "^10.8.2"
}
}

0 comments on commit b9960b8

Please sign in to comment.