[英]How to write a HTML file in nodejs
所以我生成了一些 HTML 並且我有一個包含名為 html 的 HTML 的對象,我想將它寫入一個新文件,但現在它不起作用,因為我保存的文件中只有 NaN 。 這是我到目前為止
import mjml2html from 'mjml'
import Handlebars from 'handlebars'
import fs from 'fs'
const template = Handlebars.compile(`
<mjml>
<mj-body>
<mj-section background-color="#F0F0F0" padding-bottom="0">
<mj-column padding-left="70px" width="250px">
<mj-text font-style="italic" font-size="22px" color="#626262">watFriends</mj-text>
</mj-column>
<mj-column width="170px">
<mj-image width="30px" src={{logo}} />
</mj-column>
</mj-section>
<mj-section background-color="#FAFAFA">
<mj-column width="400px">
<mj-text font-style="italic" font-size="15px" font-family="Helvetica Neue" color="#626262">
Dear {{firstName}},
</mj-text>
<mj-text color="#525252">{{message}}
</mj-text>
</mj-column>
</mj-section>
</mj-body>
</mjml>
`)
const context = {
firstName: '',
message: 'hello',
logo: 'logo.png',
}
const mjml = template(context)
const html = mjml2html(mjml)
console.log(html)
fs.writeFile('new.html', html.toString(), { encoding: 'utf8' }, function (err) {
if (err) {
return console.log(err)
}
console.log('The file was saved!')
})
mjml2html 返回一個像{html: ' ...html here..', json: {}, errors: []}
的對象,這個對象的 toString() 是你正在寫入文件的字符串"[Object object]"
. 改變
const html = mjml2html(mjml)
到
const {html} = mjml2html(mjml)
一切都會好的
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.