npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

electron-view-renderer

v1.2.5

Published

View rendering engine for electron framework.

Downloads

6

Readme

Electron View Renderer

npm npm node electron

This package is intended to work with the electron framework for building desktop apps. It allows developers to parse templates on the fly vs creating static HTML files, or pre-rendering any templates.

In addition this package exposes a way to add additional renderer types beyond the ones that are added by default.

Currently the following are added by default:

['ejs']

Features

  • Simplified view templating
  • Custom view render engines
  • Custom asset protocol

Install

npm install --save electron electron-view-renderer

Usage

This package accomplishes its goal by registering custom protocols. This means that rather than listening to a protocol such as 'file://' or 'https://' it listens to the specified protocol (default is "view"). This matters because it allows us to do our template rendering without having to take over an existing protocol such as "file://" and yet still be able to treat it as such.

ElectronViewRenderer can be instantiated at any time, even before app is ready, but will ultimately wait for the app to be ready before registering new protocols. The new protocols will then be available throughout the lifetime of the process.

// require ElectronViewRenderer
const ElectronViewRenderer = require('electron-view-renderer')

// instantiate ElectronViewRenderer with
const viewRenderer = new ElectronViewRenderer({
  viewPath: 'views',            // default 'views'
  viewProtcolName: 'view',      // default 'view'
  useAssets: true               // default false
  assetsPath: 'assets'          // default 'assets'
  assetsProtocolName: 'asset'   // default 'asset'
})

// use favorite renderer (currently preloaded with ejs, but more coming
// and custom renderers can be plugged in)
viewRenderer.use('ejs')

...

// when instantiating a browser window
const window = new BrowserWindow({
  backgroundColor: '#fff',
  center: true,
  height: 600,
  width: 600,
})

viewRenderer.load(window, 'index', {myLocalVar: "value"})

Adding custom renderers

The best example given could be with how ejs is added internally. The main requirement is that there is a rendererAction and that its callback is called with the rendered HTML:

viewRenderer.add('ejs', {
  extension: '.ejs',
  viewPath: 'views',
  rendererAction: (filePath, viewData, callback) => {
    ejs.renderFile(filePath, viewData, {}, (error, html) => {
      if (error) {
        if (error.file) error.message += `\n\nERROR @(${error.file}:${error.line}:${error.column})`
        throw new Error(error)
      }

      callback(html)
    })
  }
})

Example app

You can run a basic example by switching directory into ./example and then running the following:

npm install
npm start

The example is broken down as follows, given a basic file structure:

project/
├── assets/
│   └── css/
│       └── main.css
├── views/
│   └── index.ejs
├── main.js
└── package.json

project/main.js:

const {app, BrowserWindow} = require('electron')
const ElectronViewRenderer = require('electron-view-renderer')
const path = require('path')
const pug = require('pug')

const viewRenderer = new ElectronViewRenderer({
  viewPath: 'views',
  viewProtcolName: 'view',
  useAssets: true,
  assetsPath: 'assets',
  assetsProtocolName: 'asset',
})

viewRenderer.use('ejs')

// Or register custom renderers:
//
// viewRenderer.add('pug', {
//   extension: '.pug',
//   viewPath: 'views',
//   rendererAction: (filePath, viewData, callback) => {
//     pug.renderFile(filePath, viewData, (error, html) => {
//       if (error) {
//         if (error.file) error.message += `\n\nERROR @(${error.file}:${error.line}:${error.column})`
//         throw new Error(error)
//       }
//
//       callback(html)
//     })
//   }
// })
//
// viewRenderer.use('pug')

let mainWindow

function createWindow () {
  mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js')
    }
  })

  // NOTE: instead of loading a url as the Quick Start example shows, we are
  //       going to use the viewRenderer helper
  const viewOptions = {name: "Bob"}
  viewRenderer.load(mainWindow, 'index', viewOptions)

  mainWindow.webContents.openDevTools()
  mainWindow.on('closed', () => { mainWindow = null })
}

app.on('ready', createWindow)
app.on('window-all-closed', () => { if (process.platform !== 'darwin') app.quit() })
app.on('activate', () => { if (mainWindow === null) createWindow() })

project/views/index.ejs:

<!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Hello From <%= name %></title>
    <link href="asset://css/main.css" rel="stylesheet" type="text/css" />
  </head>
  <body>
    <h1>Hello From <%= name %>!</h1>
    We are using node <script>document.write(process.versions.node)</script>,
    Chrome <script>document.write(process.versions.chrome)</script>,
    and Electron <script>document.write(process.versions.electron)</script>.
  </body>
</html>

project/assets/css/main.css:

body {
  background-color: #bada55;
}

Dependencies

NOTE: this package does not officialy support electron-prebuilt. Please see https://stackoverflow.com/questions/41574586/what-is-the-difference-between-electron-and-electron-prebuilt

Package | Version --- |:---: electron | ^6.0.10 ejs | ^2.7.1 haml | ^0.4.3 pug | ^2.0.4 captains-log | ^2.0.3

Author

Dennis Baskin [email protected] http://github.com/dennisbaskin/electron-view-renderer