English | 简体中文

Precompiled

To make the template code to run faster, more economical memory in browser, and we need to be more easily manage the template file, the template code we usually saved as a separate file (default extension is .tpl, according to their needs adjustment), and then precompiled during the development phase.

File is no longer template code, it is an executable .js file, which the browser can naturally very quickly identify and run it.

After compiling a template, you can use nodetpl.get() or require() method references.

Installation

Precompiled tools need node.js environmental support, if you have not installed node.js, please install it: https://nodejs.org/.

After installing node.js, enter the following statement on the command line to complete the installation:

npm install nodetpl -g

Grammar

nodetpl <path> --extname <extname> --encoding <encoding> --nostrict --watch

Example

Compiling a Single File

First, our template file is saved in a directory (usually tpls), the template file extension is .tpl, execute the following command:

nodetpl /usr/local/testproject/tpls/demo/abc.tpl

The result:

<? Hello, nodetpl is working... ?>

>> dist: auto
>> extname: .tpl
>> watch mode: false
>> from: /usr/local/testproject/tpls/demo/abc.tpl
>> [1]  file: /usr/local/testproject/tpls/demo/abc.tpl

We can see that in the template file under the same path, generated a .js file of the same name, the file is compiled executable file.

Mass compile the specified directory multiple template files

The <path> set to the directory where the template, you can get mass compile all template files in that directory:

nodetpl /usr/local/testproject/tpls

The result:

<? Hello, nodetpl is working... ?>

>> dist: auto
>> extname: .tpl
>> watch mode: false
>> from: /usr/local/testproject/tpls
>> [1]  file: /usr/local/testproject/tpls/demo/abc.tpl
>> [2]  file: /usr/local/testproject/tpls/demo/def.tpl

Advanced Configuration

compile the template with encoding gbk

nodetpl /usr/local/testproject/tpls --encoding gbk

Monitor file changes automatically compile templates

nodetpl /usr/local/testproject/tpls --watch

The result:

<? Hello, nodetpl is working... ?>

>> dist: auto
>> extname: .tpl
>> watch mode: false
>> from: /usr/local/testproject/tpls
>> [1]  file: /usr/local/testproject/tpls/demo/abc.tpl
>> [2]  file: /usr/local/testproject/tpls/demo/def.tpl
[listening...]
>> [3]  file: /usr/local/testproject/tpls/demo/abc.tpl
        Mon Nov 23 2015 15:46:20 GMT+0800 (CST)

When you modify and save a template file, the file will be automatically compiled.

Communication