New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

medium-templater

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

medium-templater

smarter than your average tiny-templater

latest
Source
npmnpm
Version
1.1.0
Version published
Maintainers
1
Created
Source

medium-templater

Build Status

It's like a tiny-templater but with a little dash of logic.

usage

var language = require('medium-templater')

var template_string
  , template
  , compile
  
template_string =
    '<ul>' +
    '{% for item in items %}' +
    '<li>{% if item.okay %}okay{% else %}not okay{% endif %}</li>' +
    '{% endfor %}' +
    '</ul>' +
    '{{ message }}'

compile = language()

template = compile(template_string)

var result = template({
    items: [{okay: true}, {okay: false}]
  , message: 'hello world'
})

console.log(result) // <ul><li>okay</li><li>not okay</li></ul>hello world

extending

Though you get if and for for free, you can also provide your own tags (or override the defaults if you don't like how they work!) like so:

index.js

var language = require('medium-templater')
  , reverse = require('./lib/reverse')

language({
    reverse: reverse
})

./lib/reverse.js

module.exports = reverse

function reverse(parser, tag_options) {
  var contents = ''

  parser.parse({'endreverse': end_reverse})

  return function(context) {
    // context is what is passed to the template, irrelevant in this example.
    return contents().split('').reverse().join('')
  }

  function end_reverse(tpl) {
    contents = tpl
  }
}

now you can:

{% reverse %}
!sredner etalpmet eht nehw thgir eb lliw txet siht
{% endreverse %}

further options

Specifying reversed at the end of a for statement (ie for item in items reversed) will parse the items in reverse. Specifying an {% empty %} tag after the {% for ... %} will return that chunk in the event of no items.

license

MIT

Keywords

templater

FAQs

Package last updated on 01 Apr 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts