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

mejs

v0.9.1

Published

Moduled and Embedded JavaScript templates, run in node.js and all browsers

Downloads

50

Readme

mejs

Moduled and Embedded JavaScript templates, run in node.js and all browsers.

NPM version Build Status Downloads

Features

  • Run in node.js and all browsers
  • Custom delimiters (e.g., use '' instead of '<% %>')
  • Full support for include
  • Full support for import(Moduled)
  • Support layout in server side
  • Support express, koa, toa ...

Tags

  • <%: 'Scriptlet' tag, for control-flow, no output
  • <%_: 'Whitespace Slurping' Scriptlet tag, strips all whitespace before it
  • <%=: Outputs the value into the template (escaped)
  • <%-: Outputs the unescaped value into the template
  • <%#: Comment tag, no execution, no output
  • <%%: Outputs a literal '<%'
  • %%>: Outputs a literal '%>'
  • %>: Plain ending tag
  • -%>: Trim-mode ('newline slurp') tag, trims following newline
  • _%>: 'Whitespace Slurping' ending tag, removes all whitespace after it

Implementations:

Demo

Installation

npm install mejs

API

const mejsCompile = require('mejs')

mejsCompile(patternOrMejsfile[, options]) => Mejs Class

Compile ejs templates to a Mejs Class.

  • patternOrMejsfile: Glob pattern to read template files. Or mejs file object.
  • options.glob: Glob options
  • options.base: Everything before a glob (same as tplName) starts.
  • options.delimiter: Character to use with angle brackets for open/close, default is %.
  • options.rmWhitespace: Remove all safe-to-remove whitespace, including leading and trailing whitespace in compiling. It also enables a safer version of -%> line slurping for all scriptlet tags (it does not strip new lines of tags in the middle of a line).
  • options.rmComment: Remove comment before compiling (/<!--([\s\S]*?)-->/g).
  • options.rmLinefeed: Remove linefeed and trailing whitespace before compiling (/\n+[\s]*/g).
const Mejs = mejsCompile('views/**/*.html') // options.base == 'views/'

mejsCompile.initMejs(pattern[, options]) => mejs object

mejs object have renderEx method that support layout, it is useful in server side.

  • pattern: pattern is same as above.
  • options: options is same as above, and have options.locals, options.layout and options.sandbox apply for Mejs class.

mejsCompile.initView(pattern[, options]) => View class

It is implemented for express. arguments is same as mejsCompile.initMejs.

const app = express()
app.set('view', mejs.initView('views/**/*.ejs', {
  layout: 'layout',
  locals: app.locals
}))

//... render with layout
res.render('index', {user: req.user})

//... disable layout for 'login' view
res.render('login', {layout: false})

mejsCompile.precompile(files[, options]) => mejs file object

Precompile ejs templates to a file object, then you can write it to a JS file.

  • files: Template files array, the file in array must have path and contents.
  • options: options is same as above. but one more:
    • options.mini: Precompile a minimum templates module, it is not a Mejs class, should be imported to Mejs class by Mejs.import
const mejsSource = mejsCompile.precompile([{
    path: 'index.html',
    contents: 'index content...'
  }, {
    path: 'layout.html',
    contents: 'layout content...'
  }, {
    path: 'lib/index',
    contents: 'lib index content...'
  }
], {base: 'views'})

mejsCompile.precompileFromGlob(pattern[, options]) => mejs file object

Precompile ejs teamplates to a file object, then you can write it to a JS file.

  • pattern: glob pattern.
  • options: options is same as above.
const mejsSource = mejsCompile.precompileFromGlob('views/**/*.js', {base: 'views'})

mejsCompile.Template(text[, options])

Ejs template engine.

mejsCompile.File(contents[, options][, base])

mejs file Class. It is similar to vinyl, AKA gulp file

new Mejs(locals) => mejs object

  • locals: global locals object, default is {}.
// add config, moment and node-i18n to global locals
const mejs = new Mejs({
  config: {
    host: 'www.mejs.com',
    apiHost: 'www.mejs.com/api'
  },
  moment: moment,
  locale: function() {
    return this.locale
  },
  __: function() {
    return this.__.apply(this, arguments)
  }
})

Class Method: Mejs.import(templates)

Import templates to global from a templates module.

const Mejs = require('Mejs')
const tplsA = require('tplsA')
const tplsB = require('tplsB')

Mejs.import(tplsA).import(tplsB)

mejs.render(tplName, data) => filled view string

Render a template with data

  • tplName: a full template name
  • data: data object filled to template
mejs.render('index', userObj)
mejs.render('global/header', headerDate)
//...

mejs.import([namespace], mejsX)

import another mejs object object to mejs, then mejs will have the templates that from mejsX.

  • namespace: namespace string
  • mejsX: mejs object for import
mejs.import('common', mejsA)

mejs.add(tplName, tplFn)

Add a template function to current mejs.

mejs.get(tplName)

Get a template function from current mejs.

mejs.remove(tplName)

Remove a template function from current mejs.

mejs.resolve(from, to)

Resolve template path.

mejs.escape(string)

escape function for templates function. You can overwrite it.