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

ejs-electron

v3.0.0

Published

A lightweight module for ejs templating in an electronJS app.

Downloads

2,733

Readme

ejs-electron

npm npm

A mega lightweight, completely flexible module that allows ejs templating in an Electron app.

Makes use of the Electron protocol module to supply a custom handler for the file: protocol. This handler intercepts all file requests, compiles any requested .ejs files, and serves the result.


Installation

Install using npm:

$ npm install ejs-electron

Usage

const ejse = require('ejs-electron')

Method API

Note: All methods, unless otherwise specified, return the ejs-electron api for chaining.

ejse.data()

Get/set the data (context) that will be passed to ejs.render().

Overloads:

  • ejse.data('key') -- Retrieve the value of 'key' in the current data set.
  • ejse.data('key', 'val') -- Set 'key' to 'val' in the current data set.
  • ejse.data({key: 'val'}) -- Replace the current data set with a new one containing {key: 'val'}

Note: The ejs-electron api is injected into the scope of all rendered ejs templates. Access it via the variable ejse, e.g. <% ejse.stopListening() %>.

ejse.options()

Get/set the options that will be passed to ejs.render(). These configure the behavior of ejs itself. See the ejs docs for a list of possible options.

Overloads:

  • ejse.options('key') -- Retrieve the value of 'key' in the current options set.
  • ejse.options('key', 'val') -- Set 'key' to 'val' in the current options set.
  • ejse.options({key: 'val'}) -- Replace the current options set with a new one containing {key: 'val'}

Note: ejs-electron sets the ejs filename option automatically every time it renders a file. This means you can go ahead and use ejs include right out of the box. One less thing you need to worry about :)

ejse.listen()

Start intercepting requests on the 'file:' protocol, looking for '.ejs' files.

Note: It is not necessary to call this function up-front, as ejs-electron starts listening as soon as it's loaded. Use this only to start listening again after calling ejse.stopListening().

ejse.listening()

Returns true if ejs-electron is currently intercepting requests on the file: protocol.

ejse.stopListening()

Stop intercepting file requests, restoring the original file: protocol handler.


Examples

A simple Electron app with ejs-electron could look like this:

main.js
const {app, BrowserWindow} = require('electron')
const ejse = require('ejs-electron')

let mainWindow

ejse.data('username', 'Some Guy')

app.on('ready', () => {
    mainWindow = new BrowserWindow()
    mainWindow.loadURL('file://' + __dirname + '/index.ejs')
})

You can, of course, chain data(), options(), and whatnot to the require() call:

const ejse = require('ejs-electron')
	.data('username', 'Some Guy')
	.options('debug', true)
index.ejs
<h1>Hello, <%= username %></h1> <!-- Outputs: '<h1>Hello, Some Guy</h1>' -->
<% ejse.stopListening() %>

Since you have access to the ejs-electron api in your templates, you can also use the getter overload of ejse.data() to access the root-level scope of your templates. This can be useful for providing constancy in nested ejs includes:

main.js
ejse.data('name', 'Holmes')
profile.ejs
<p>Your name: <%= name %></p>
<%- include('./dog', {name: 'Sparky'}) %>
dog.ejs
<p>The dog's name: <%= name %></p>
<p>This dog belongs to: <%= ejse.data('name')</p>

A heavily contrived example, sure, but here's its output:

<p>Your name: Holmes</p>
<p>The dog's name: Sparky</p>
<p>This dog belongs to: Holmes</p>

This also means that stuff like the following is also a possibility, though I've never yet found a use for it:

<p>The current file is: <%= ejse.options('filename') %></p>

Issues

Issues may be submitted at https://github.com/bowheart/ejs-electron/issues

Thanks to all who have submitted issues. The feedback has been extremely helpful (no, seriously, you guys rock).

Also, of course, feel free to fork and pull request. Happy coding!


License

The MIT License