Home

Awesome

mejs

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

NPM version Build Status Downloads

Features

Tags

Implementations:

Demo

Installation

npm install mejs

API

const mejsCompile = require('mejs')

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

Compile ejs templates to a Mejs Class.

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.

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.

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.

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

// 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

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.

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.