A simple HTML template engine
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>
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.
<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<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>
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>
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>
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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

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.

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

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

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

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

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

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