-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1147 from otonielguajardo/liquidjs-adapter
Support liquidjs templates
- Loading branch information
Showing
2 changed files
with
41 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -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); | ||
}); | ||
} | ||
} |
This file contains 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