Giter VIP home page Giter VIP logo

mus's Introduction

Mus

NPM version Build Status Coverage Status

A server-side javascript template library like nunjucks.

quick start

npm install node-mus
const Mus = require('node-mus');
const mus = new Mus();
mus.renderString('{{ mus }}', { mus: 'hello mus' }); // hello mus;

or you can see the test example

support

  • variable
  • comment
  • for
  • if else
  • set
  • raw
  • macro
  • filter
  • extends
  • block
  • include

options

  • blockStart default: {%
  • blockEnd default: %}
  • variableStart default: {{
  • variableEnd default: }}
  • noCache default: false
const mus = new Mus({
   blockStart: '<%',
   blockEnd: '%>',
   variableStart: '<%=',
   variableEnd: '%>'
});
const template = '<% if test %><div><%= test %></div><% endif %>';
mus.renderString(template, { test: '123' });
// '<div>123</div>'

apis

render(path[, args])

render template file to html

renderString(html[, args])

render template string to html

setFilter(name, cb)

create self-defined filter

feature

variable

mus.renderString('{{ obj.hello }}{{ obj.mus }}', {
  obj: {
    hello: 'hello',
    mus: 'mus'  
  }
}); // hello mus;

expression

mus.renderString('{{ test > 0 ? text : "nothing" }}', {
  test: 1,
  text: 'hello mus',
}); // hello mus;

filter

mus.renderString('{{ text | nl2br | safe }}', {
  text: 'hello \n mus',
}); // hello <br/> mus;

comment

mus.renderString('11{# {{ test }} #}', {
  test: 'hello mus',
}); // 11;

tags

for

mus.renderString('{% for item in list %}({{ loop.index0 }}:{{ item }}){% endfor %}', {
    list: [1, 2],
}); // (0:1)(1:2)

if

mus.renderString('{% if test > 1 %}{{ test }}{% endif %}', {
    test: 2
}); // 2
const tpl = '{% if test > 2 %}{{ test }}{% elseif test === 2 %}111{% else %}333{% endif %}';
mus.renderString(tpl, {
    test: 2
}); // 111

set

mus.renderString('{% set test = test2 %}{{ test }}', {
    test2: 2,
}); // 2

raw

mus.renderString('{% raw %}{{ test }}{% endraw %}', {
    test: 2
}); // {{ test }}

macro

mus.renderString('{% macro test %}123{% endmacro %}{{ test() }}'); // 123

extends & block

template 1: test.tpl

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>{{ title }}</title>
</head>
<body>
  {% block main %}
    test.tpl content
  {% endblock %}
</body>
</html>

template 2: test2.tpl

{% extends './test.tpl' %}

{% block main %}
  test2.tpl content
{% endblock %}

render

mus.render('test2.tpl'); 
// <!doctype html> ... test2.tpl content ...

include

template 1: test.tpl

{% include './test2.tpl' %}

template 2: test2.tpl

hello mus

render:

mus.render('test.tpl'); 
// hello mus

test

npm test

coverage

npm run cov

License

MIT

mus's People

Contributors

whxaxes avatar

Watchers

Seeker avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.