Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

nornj

joe-sky166MIT5.3.5TypeScript support: included

A powerful template engine that can works with React, JSX enhancement or alternative tools.

nornj, nj, template, react, jsx, babel

readme

NornJ

  ____        __   
/\  __ \     /\ \  
\ \ \/\ \   _\_\ \ `<html>
 \ \_\ \_\ /\_____\   Hello World!
  \/_/\/_/ \/_____/ </html>`();

NornJ(pronounced [ˌnɔ:nˈdʒeɪ],abbreviated as nj) is a template engine that can works with React, JSX enhancement or alternative tools.

NPM Version License Travis CI Status Codecov NPM Downloads

English | 简体中文

Documents

Introduction

In React development, the JSX can use almost all the syntax of javascript and it's very flexible. But if we use NornJ with React and JSX, we can do better, because it can gives JSX template engine features:

  • Support control statements:
<Each of={[1, 2, 3]}><i>{item}</i></Each>
  • Support directives:
<img n-show={false} />
  • Support filters:
<button>{n`foo | upperFirst`}</button>
  • Support custom operators:
<input value={n`(1 .. 100).join('-')`} />

NornJ presets the above JSX enhancement syntaxs, and also supports custom extensions of more syntaxs. It provides two kinds of similar API: JSX and Tagged templates, can adapt to the preferences of different users :wink:.

Basic

class App extends Component {
  addTodo = e => {
    const { todos = [] } = this.state;
    this.setState({ todos: todos.concat(`Item ${todos.length}`) });
  };

  render({ page }, { todos = [] }) {
    return (
      <div className="app">
        <style jsx>`
          .app {
            padding: 20px;
            font-size: .75rem;
          }
        `</style>
        <ul>
          <Each of={todos} item="todo">
            <If condition={index > 5}>
              <li>{todo * 2}</li>
              <Elseif condition={index > 10}>
                <li>{todo * 3}</li>
              </Elseif>
            </If>
          </Each>
        </ul>
        <button n-show={todos.length > 0} onClick={this.addTodo}>Add Todo</button>
      </div>
    );
  }
}

For above example, combining with the Babel plugin provided by NornJ, it is possible to write various new enhancement syntaxs in JSX.

const template = html`
  <Container>
    <ul>
      <each of={todos}>
        <if {@index > 5}>
          <li>{@item * 2}</li>
          <elseif {@index > 10}>
            <li>{@item * 3}</li>
          </elseif>
        </if>
      </each>
    </ul>
    <button n-show="{todos.length > 0}" :onClick="addTodo">Add Todo</button>
  </Container>
`;

const Container = styled.div`
  padding: 20px;
  font-size: .75rem;
`;

class App extends Component {
  addTodo = e => {
    const { todos = [] } = this.state;
    this.setState({ todos: todos.concat(`Item ${todos.length}`) });
  };

  render() {
    return template({ components: { Container } }, this.state, this);
  }
}

In the above example, a template function was created using tagged templates API of NornJ. In this way, the template can be separated from the component logic code, and it also supports more concise writing than NornJ JSX API.

Playground

Use it in JSX

Use tagged templates

Install

npm install babel-plugin-nornj-in-jsx  #or yarn add babel-plugin-nornj-in-jsx

Next, add nornj-in-jsx to plugins in your babel configuration:

{
  "plugins": [
    "nornj-in-jsx"
  ]
}

Syntax highlight

License

MIT

changelog

CHANGELOG

[v5.3.4] 2021.05.21

  • 🐞 Fix type for first and last of For tag.

[v5.3.3] 2021.05.13

  • 🐞 Fix type error of If/Each/etc capitalized tags in TSX.

[v5.3.0] 2020.12.30

  • 🌟 Add as tool function.

[v5.2.8] 2020.09.16

  • 🌟 ES module adaptation.

[v5.2.5] 2020.08.06

  • 🐞 Fixed the children nodes types for JSX.IntrinsicAttributes.

[v5.2.4] 2020.07.20

  • 🌟 Fixed types for nj.html and nj.htmlString.

[v5.2.0] 2020.07.08

  • 🌟 mobxFormData tag official released.

[v5.1.1] 2020.02.05

[v5.0.7] 2019.12.31

  • 🔧 Set the default value of ExtensionOption.useExpressionInProps to false.

[v5.0.1] 2019.12.04

  • 🏷️ Added a ElementType type to replace the React.ElementType for compatibility.

[v5.0.0] 2019.12.03

  • 🚩 Support complete typescript type definition.

[v5.0.0-rc.42] 2019.09.12

  • ♻️ Refactoring code using typescript.

[v5.0.0-rc.24] 2019.09.12

  • 🐞 Fixed rendering spaces with text mode.

[v5.0.0-rc.23] 2019.09.06

  • 🐞 Fixed the key prop name of each tag.

[v5.0.0-rc.20] 2019.08.20

  • 🌟 Added nornj/lib/filter/lodash.

[v5.0.0-rc.19] 2019.07.24

  • 🌟 Added sub tag then for if tag.

[v5.0.0-rc.15] 2019.06.17

  • 🌟 Added rawStart and rawEnd parameters for delimiters config.

[v5.0.0-rc.14] 2019.06.14

  • 🌟 The switch tag should be prefixed by default when it begin in lowercase.

[v5.0.0-rc.10] 2019.05.20

  • 🌟 Support spread props in extension tags.

[v5.0.0-rc.8] 2019.05.08

  • 🌟 Support key variable for each tag in jsx.

[v5.0.0-rc.7] 2019.05.05

  • 🌟 Changed the default template rule of tags from <#extag> to <n-extag>. If you need to adapt version 0.4, you can use nj.config to modify the rules.

[v5.0.0-rc.5] 2019.04.14

  • 🌟 Support nj<Extag></Extag> auto transform to nj<n-extag></n-extag>.

[v5.0.0-rc.1] 2019.04.02

  • 🌟 扩展函数中的options.result改为options.childrenoptions.value

[v5.0.0-beta.3] 2019.03.13

  • 🌟 增加nj.copyComponentConfig方法。

[v0.4.18] 2019.01.25

  • 🌟 存储nj.componentConfig的类型改为使用Map
  • 🐞 修复指令在react-native中打包 release 版时的 bug。

[v0.4.17] 2018.12.20

  • 🐞 修复#show指令可在react-native中支持组件的style参数为数组的情况。

[v0.4.16] 2018.12.14

  • 🐞 tools/metroTransformer适配react-native v0.56+

[v0.4.15] 2018.12.12

  • 🌟 新增nj.buildRendernj.buildRenderH方法,用于预编译各tagged template literal功能时使用。
  • 🐞 修复 runtime 包缺少nj.getComponentConfig方法。

[v0.4.14] 2018.12.05

  • 🐞 修复for标签 bug。

[v0.4.13] 2018.10.17

  • 🌟 改进for标签语法。
  • 🌟 改进扩展标签配置信息。

[v0.4.12] 2018.09.05

  • 🌟 扩展标签函数的options参数中增加attrs参数。
  • 🌟 改进precompile方法,更好地配合babel-plugin-nornj-in-jsx做预编译。

[v0.4.11] 2018.08.17

  • 🐞 修复渲染<br style="color: #fff">时的 bug。#17

[v0.4.10] 2018.08.15

  • 🐞 改进nj.registerComponent将同一组件注册多次时的策略。

[v0.4.9] 2018.08.10

  • 🌟 扩展标签函数的options参数中增加tagName参数。
  • 🌟 nj.registerComponent支持传入组件配置属性。
  • 🌟 新增 APInj.getComponentConfignj.expression
  • 🐞 修复 <input #mobx-model={value}> bug。
  • 🐞 修复扩展属性不能添加多个的 bug。

[v0.4.8] 2018.07.22

  • 🌟 支持配合webpack使用时,直接在模板中使用require方法引入图片等资源。查看文档

[v0.4.7] 2018.05.27

[v0.4.6] 2018.05.25

  • 🐞 修复 nj.mustache bug。

[v0.4.5] 2018.05.23

[v0.4.4] 2018.05.07

  • 🌟 表达式支持{{!a.b.c}}
  • 🌟 表达式支持放在最前面的括号,如{{(a.b.c)}}
  • 🌟 新增?:%%过滤器,未来逐步替代?//

[v0.4.3] 2018.04.28

  • 🌟 <#each>标签增加@item参数。
  • 🌟 增加新 API nj.template查看文档

[v0.4.2] 2018.04.11

  • 🌟 增加<nj-noWs>标签,用于输出无多余空白的 html 字符串。
  • 🌟 错误提示信息优化。

[v0.4.2-rc.38] 2018.03.29

  • 🌟 扩展标签函数的options参数中增加nameparentName参数。
  • 🌟 支持赋值语法,如{{ set a.c = c }}
  • 🐞 修复在标签的属性名和字符串类型值完全相等时,编译时会认为只传了属性名的问题,如<input name="name" />

[v0.4.2-rc.36] 2018.03.21

  • 🌟 支持<div :#show="1 < 2">语法。
  • 🌟 支持在nj标签模板字符串语法中写<#include>标签。
  • 🌟 nj.createTaggedTmpl方法支持传入fileName参数。

[v0.4.2-rc.35] 2018.03.19

  • 🌟 支持构建es module包。
  • 🌟 增加@root@context插值变量。
  • 🌟 once扩展标签增加name属性。

[v0.4.2-rc.34] 2018.03.12

  • 🌟 支持构建runtime包。

[v0.4.2-rc.33] 2018.03.05

  • 🌟 增加表达式语法错误提示。
  • 🌟 在 React 开发中支持插值变量的{}{{}}语法共存。相关文档

[v0.4.2-rc.31] 2018.02.27

  • 🌟 表达式支持编写嵌套对象字面量,如{{ { a: { b: 1 } }.a.b }}
  • 🌟 插值变量中任何形式的链式语法如其中有undefined也不会出现错误,而是返回一个空值。如{{ a.b['c'].d }},a、b、c 各为 null 时都不会报错。

[v0.4.2-rc.28] 2018.02.13

  • 🌟 为减小代码体积,使用rollup重新构建dist目录下各文件。