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

vmplate

v1.1.2

Published

Simple templates for node programs

Downloads

21

Readme

vmplate

Simple templates for node programs.

Build Status

vmplate templates depend on the vm module to create a reusable template context. They're great for command-line programs or templating files, but they don't do any escaping and they won't work in the browser, so they're a bad choice for HTML.

Install

$ npm install vmplate

Usage

var vmplate = require('vmplate');

var render = vmplate('hello <%= name %>!')

console.log(render({ name: 'world' })); // hello world!

vmplate(string[, locals[, filename]])

Compiles a new vmplate function for string.

If you pass in a locals hash, this will be used as the render function's locals hash. Otherwise, it's an empty hash that you can modify after the fact.

var render = vmplate('hello <%= name %>!', { name: 'world' });

console.log(render()); // hello world!
console.log(render({ name: 'beer' })); // hello beer!

render.locals.name = 'nasty';

console.log(render()); // hello nasty!

If you provide a filename, it will be used as the filename of the vm script and only really shows up in error messages.

templating

vmplate uses ejs-style delimiters, but there are only two supported operations.

<%= foo %> (note the =) will add the value of foo to the buffer. The program will throw if foo is not defined.

<% var foo = "bar"; %> will evaluate the expression but will not add it to the buffer. This makes any expression that's also valid javascript usable in your template.

For example, here's how to iterate over an array:

var render = subject('<% for(var i = 0; i < arr.length; i++){ %><%= arr[i] %><% } %>');

console.log(render({ arr: [1, 2, 3] })); // 123

The program will throw if there is a syntax error in your javascript.

vmplate.locals → render.locals → locals

There are four ways to provide locals to your vmplate:

The locals hash on vmplate itself:

var vmplate = require('vmplate');
var render = vmplate('<%= foo %>');

vmplate.locals.foo = 'yay';

console.log(render()); // yay

The locals hash on the vmplate instance:

var render = vmplate('<%= foo %>');

render.locals.foo = 'yay';

console.log(render()); // yay

The locals hash passed to the vmplate factory:

var render = vmplate('<%= foo %>', { foo: 'yay' });

console.log(render()); // yay

Or the locals hash passed to the render function:

var render = vmplate('<%= foo %>');

console.log(render({ foo: 'yay' })); // yay

Render locals inherit from instance locals, which in turn inherit from the vmplate factory locals.

License

ISC License