繁体   English   中英

如何在 nodejs 中编写 HTML 文件

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM