像传统模板引擎那样工作

NornJ模板引擎虽然可以支持React开发,但其实它同时也是一个传统的js模板引擎,用法和HandlebarsNunjucks等非常类似。下面的是一个使用cdn上umd包的在线可运行实例:

  • 在线Playground(jsfiddle)

source code

安装

npm install nornj
npm install nornj-loader  # webpack环境请一起安装此包

在js文件中使用NornJ模板

NornJ包含传统模板引擎通用的api如compilerender等。例如使用compileapi可以将一段模板编译为一个模板函数:

import nj from 'nornj';

const tmplFn = nj.compile(`
  <div class="hello" style="width:300px;height:200px;">
    <input type="{{inputType}}">
    <select>
      <#each {{[1, 2, 3]}}>
        <#if {{@index > 1}}>
          <option>{{this + 1}}</option>
          <#else><option>{{this}}</option></#else>
        </#if>
      </#each>
    </select>
  </div>
`);

//执行模板函数并传入参数渲染
console.log(tmplFn({ inputType: 'text' }));

更多NornJ模板的语法细节请参考这里

使用ES6+的标签模板字符串来编写模板

NornJ还可以使用ES6+tagged template string语法在js文件中描述模板:

import nj from 'nornj';

//直接使用标签模板字符串渲染
console.log(nj`
  <div class="hello" style="width:300px;height:200px;">
    <input type=${'inputType'}>
    <select>
      <#each ${[1, 2, 3]}>
        <#if {{@index > 1}}>
          <option>{{this + 1}}</option>
          <#else><option>{{this}}</option></#else>
        </#if>
      </#each>
    </select>
  </div>
`());

如上,使用NornJ的标签模板字符串语法,可以更方便地在js文件中编写模板,还能任意插入各种外部js原生变量。

更多关于使用标签模板字符串编写NornJ模板的语法细节请参考这里

在单独的文件中编写NornJ模板

NornJ模板除了可以在js文件中编写之外,还可以编写在单独的模板文件中,用来做展现层与结构层的分离(具体文档请参考这里)。例如编写一个helloWorld.t.html文件:

<template name="helloWorld">
  <div class={{styles.hello}}>
    <select>
      <#each {{[1, 2, 3]}}>
        <#if {{this > 1}}>
          <option>{{this + 1}}</option>
          <#else><option>{{this}}</option></#else>
        </#if>
      </#each>
    </select>
  </div>
</template>

然后可以在js文件中引入后使用:

import tmpls from './helloWorld.t.html';

//执行模板函数生成html字符串
console.log(tmpls.helloWorld());

如上,每个*.t.html文件内都可以定义一个或多个template标签。

这些template标签会在引用它的js文件中通过nornj-loader进行解析,生成一个以template标签的name属性为key的模板函数集合对象,执行这些模板函数就会生成相应的html字符串。

使用NornJ渲染普通文本

NornJ不只可以渲染html,它也同样像Handlebars那样支持渲染普通文本文件,就像vue-cli中使用Handlebars来替换项目模板文件中的参数那样工作。

只需要开启文本解析模式(具体原因请见文档)就可以了:

nj.config({
  textMode: true
});

然后它就可以对文本文件进行渲染了,普通文本中的各种换行符等都会按照原样保留。具体应用场景请见nornj-cli中的项目模板文件

与Node服务器框架结合

NornJ目前针对ExpressKoa两大服务器都提供了相应的插件支持,具体请见:

results matching ""

    No results matching ""