Table of Contents
Download and use" >Download and use
Features" >Features
var Leopard = require('leopard-template')
var leo = new Leopard()

var template = '<% if (isOk) { %>' +
 '<span class=\"nickname\"><%= nickname | capitalize %></span>' +
 '<% } else { %>' +
 '<span class=\"realname\"><%= realname | capitalize %></span>' +
 '<% } %>'

var html = leo.compile(conditions, {
 isOk: false,
 nickname: 'leo',
 realname: 'leopard'
})

// html就是最终编译成功的的html了,可以直接通过document的方法渲染到页面上
Copy after login
" >
var Leopard = require('leopard-template')
var leo = new Leopard()

var template = '<% if (isOk) { %>' +
 '<span class=\"nickname\"><%= nickname | capitalize %></span>' +
 '<% } else { %>' +
 '<span class=\"realname\"><%= realname | capitalize %></span>' +
 '<% } %>'

var html = leo.compile(conditions, {
 isOk: false,
 nickname: 'leo',
 realname: 'leopard'
})

// html就是最终编译成功的的html了,可以直接通过document的方法渲染到页面上
Copy after login
Home Web Front-end JS Tutorial A simple HTML template engine

A simple HTML template engine

Feb 10, 2018 pm 03:00 PM
html engine template

The templates I have used before are ejs and jade (later renamed pug). The former is designed to be easy to use, and its syntax is relatively close to HTML. The latter is daunting, and if I remember correctly, jade has strict requirements on indentation, because it judges the hierarchical relationship of tags based on indentation. This design makes it almost impossible to write. Writing Python was like walking on thin ice (where is my vernier caliper???), so I still used ejs for development at that time.

So, this time I roughly followed the syntax specifications of ejs to implement Leopard.

Download and use

This is the github address. You are welcome to make suggestions and bugs in the issue after reading it. PRs are also welcome. .

You can also download it through npmLeopard

<span style="font-size: 14px;">$ npm install leopard-template<br></span>
Copy after login

Features

Currently, Leopard has implemented the following functional points:

  • ##Interpolation: including text interpolation and HTML interpolation

  • ##Logical judgment:

    if<span style="font-size: 14px;"></span> and else <span style="font-size: 14px;"></span>

  • Loop:

    for<span style="font-size: 14px;"></span> loop, which can be used to loop the output template <span style="font-size: 14px;"></span>

  • Filter: Supports adding filters to interpolation, and filters can be used in series. The engine has two built-in filters,

    capitalize<span style="font-size: 14px;"></span> and reverse<span style="font-size: 14px;"></span>. Leopard also supports custom filters. You can use Leopard.filter(filter, handler)<span style="font-size: 14px;"></span> to register a filter globally. In terms of filters, Leopard may be different from ejs, but more similar to Vue.

For example

<span style="font-size: 14px;">var Leopard = require('leopard-template')<br>var leo = new Leopard()<br><br>var template = '<% if (isOk) { %>' +<br>  '<span class=\"nickname\"><%= nickname | capitalize %></span>' +<br>  '<% } else { %>' +<br>  '<span class=\"realname\"><%= realname | capitalize %></span>' +<br>  '<% } %>'<br><br>var html = leo.compile(conditions, {<br>  isOk: false,<br>  nickname: 'leo',<br>  realname: 'leopard'<br>})<br><br>// html就是最终编译成功的的html了,可以直接通过document的方法渲染到页面上<br></span>
Copy after login

Performance

In fact, string Everyone knows the performance of the template engine. Under current hardware conditions, it can almost be said to be very fast. (The child who suffered from the inability to improve the rendering performance of the virtual DOM server cried and fainted in the toilet. My company project is stuck here and cannot be online)

I made a simple For benchmark, the time it takes to output 50,000

li<span style="font-size: 14px;"></span> in a loop is about 60ms. Of course, Leopard currently only supports parsing and compiling template string into HTML string, so the loop output here refers to the string compilation step .

<span style="font-size: 14px;"># benchmark<br>$ npm run benchmark<br></span>
Copy after login

Open Source

Although it is a wheel-making project, and it looks almost the same as

ejs, Therefore, it is unlikely to be put into use in a production environment (besides, the MVVM framework is now used to develop projects), but I still hope to develop Leopard in accordance with the specifications of open source projects. I wrote test cases with 100% coverage for Leopard. Every time I submitted a commit, I ran the test and passed it before submitting it. I also hoped that this project would not be too watery.

<span style="font-size: 14px;"># unit test<br>$ npm run test<br><br># coverage<br>$ npm run coverage<br></span>
Copy after login
Related recommendations:

Node.js template engine jade example explanation

A simple example of php implementing the template engine function

PHP design pattern container deployment framework based on template engine

The above is the detailed content of A simple HTML template engine. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Clair Obscur: Expedition 33 - How To Get Perfect Chroma Catalysts
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1677
14
PHP Tutorial
1278
29
C# Tutorial
1257
24
Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles