简体   繁体   中英

Loading a Nunjucks template with Electron.js

I would like to use a nunjucks render as my HTML for a window in my electron app but I can't find a way, is it possible? From what I have seen there are 2 ways to load HTML in window:

  // Load a remote URL
  win.loadURL('https://github.com')

  // Or load a local HTML file
  win.loadURL(`file://${__dirname}/app/index.html`)

And when I render my nunjucks template I have a string stored in a javascript variable:

render = nunjucks.render('./template/Template.html', data);

How can I use that string as an html for my window?

Many thanks everyone

You can pack your HTML into a data URI and pass that to win.loadURL:

const html = "<html><body><h1>Hello, world!</h1></body></html>";
win.loadURL("data:text/html;charset=utf-8," + encodeURI(html));

With your nunjucks render:

const render = nunjucks.render('./template/Template.html', data);
win.loadURL("data:text/html;charset=utf-8," + encodeURI(render));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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