English | 简体中文

Basic Introduction

Version: v4.5.0

Project: https://github.com/pillys/nodetpl (Thanks for your star)

BUG feedback: https://github.com/pillys/nodetpl/issues

Quick Start

Import file

Download and unzip the nodetpl package.

General reference, link the nodetpl file in header tag (package: ./lib/nodetpl.min.js):

<script src="./js/nodetpl.min.js"></script>

AMD/CMD/CommonJs/ES6:

var nodetpl = require('nodetpl');

Writing Templates

The most simple, we can create a suffix with .tpl for the TPL file, write our template code in the file.

tpl/test.tpl:

<h1><?=@title?></h1>
<ul>
  <?for(var i=0; i<@favor.length; i++){?>
    <li><?=i?>:<?=@favor[i]?></li>
  <?}?>
</ul>

Perform rendering

var data = {
  "title": "Favor",
  "favor": [
    "Football",
    "Basketball",
    "Table tennis",
    "Glass ball"
  ]
};
nodetpl.get('tpls/test.tpl', data, function(d){
  console.log(d);
});

Result

<h1>Favor</h1>
<ul>
  <li>0: Football</li>
  <li>1: Basketball</li>
  <li>2: Table tennis</li>
  <li>3: Glass ball</li>
</ul>

Have a tryView demo

Keywords

nodetpl will involve the following keywords will be contacted individually next:

Communication