Home Web Front-end JS Tutorial How to use javascript to write template engine code ideas and detailed examples

How to use javascript to write template engine code ideas and detailed examples

Jul 22, 2017 pm 04:01 PM
javascript js engine

AbsurdJS itself is mainly released as a module for NodeJS, but it also releases a client version. With this in mind, I can't directly use existing engines because most of them run on NodeJS and cannot run on the browser.

The initial idea is like this:


var TemplateEngine = function(tpl, data) {
  // magic here ...
}
var template = &#39;<p>Hello, my name is <%name%>. I\&#39;m <%age%> years old.</p>&#39;;
console.log(TemplateEngine(template, {
  name: "Krasimir",
  age: 29
}));
Copy after login

A simple function, the input is our template and data object, You can probably easily think of the output, like this:

<p>Hello, my name is Krasimir. I&#39;m 29 years old.</p>
Copy after login

The first step is to find the template parameters inside, and then replace them with the specific data passed to the engine. I decided to use regular expressions to accomplish this step. But I'm not the best at this, so feel free to comment if your writing isn't good.


var re = /<%([^%>]+)?%>/g;
Copy after login

This regular expression will capture all fragments starting with <% and ending with %>. The parameter g (global) at the end means that not only one is matched, but all matching fragments are matched. There are many ways to use regular expressions in Javascript. What we need is to output an array containing all strings based on the regular expression. This is exactly what exec does.


var re = /<%([^%>]+)?%>/g;
var match = re.exec(tpl);
Copy after login

If we use console.log to print out the variable match, we will see:


[
  "<%name%>",
  " name ", 
  index: 21,
  input: 
  "<p>Hello, my name is <%name%>. I\&#39;m <%age%> years old.</p>"
]
Copy after login

But we As you can see, the returned array only contains the first match. We need to wrap the above logic with a while loop so that we can get all matches.


##

var re = /<%([^%>]+)?%>/g;
while(match = re.exec(tpl)) {
  console.log(match);
}
Copy after login

If you run the above code, you will see that <%name%> and <%age%> are both printed.

Now comes the interesting part. After identifying the matches in the template, we need to replace them with the actual data passed to the function. The simplest way is to use the replace function. We could write it like this:


var TemplateEngine = function(tpl, data) {
  var re = /<%([^%>]+)?%>/g;
  while(match = re.exec(tpl)) {
    tpl = tpl.replace(match[0], data[match[1]])
  }
  return tpl;
}
Copy after login

Okay, so it runs, but it's not good enough. Here we use a simple object to pass data in the form of data["property"], but in actual situations we are likely to need more complex nested objects. So we slightly modified the data object:


{
  name: "Krasimir Tsonev",
  profile: { age: 29 }
}
Copy after login

However, if we write it directly like this, it still won’t run, because if <%profile.age%> is used in the template , the code will be replaced by data['profile.age'], and the result is undefined. In this way we cannot simply use the replace function, but must use other methods. It would be best if you could use Javascript code directly between <% and %>, so that the incoming data can be evaluated directly, like the following:

var template = &#39;<p>Hello, my name is <%this.name%>. I\&#39;m <%this.profile.age%> years old.</p>&#39;;
Copy after login

You may be curious, this How is it achieved? Here John uses the new Function syntax to create a function based on a string. Let's take a look at an example:


var fn = new Function("arg", "console.log(arg + 1);");
fn(2); // outputs 3
Copy after login

fn is a genuine function. It accepts one parameter, and the function body is console.log(arg + 1);. The above code is equivalent to the following code:


var fn = function(arg) {
  console.log(arg + 1);
}
fn(2); // outputs 3
Copy after login

With this method, we can construct a function from a string, including its parameters and function body. Isn’t this exactly what we want! But don't worry, before constructing the function, let's take a look at what the function body looks like. According to the previous idea, the final return of this template engine should be a compiled template. Still using the previous template string as an example, the returned content should be similar to:


return
"<p>Hello, my name is " + 
this.name + 
". I\&#39;m " + 
this.profile.age + 
" years old.</p>";
Copy after login

Of course, in the actual template engine, we will split the template into Small paragraphs of text and meaningful Javascript code. Earlier you may have seen me using simple string concatenation to achieve the desired effect, but this is not 100% in line with our requirements. Since the user is likely to pass more complex Javascript code, we need another loop here, as follows:



var template = 
&#39;My skills:&#39; + 
&#39;<%for(var index in this.skills) {%>&#39; + 
&#39;<a href=""><%this.skills[index]%></a>&#39; +
&#39;<%}%>&#39;;
Copy after login

If string concatenation is used, the code It should look like the following:



return
&#39;My skills:&#39; + 
for(var index in this.skills) { +
&#39;<a href="">&#39; + 
this.skills[index] +
&#39;</a>&#39; +
}
Copy after login

Of course, this code cannot be run directly, otherwise an error will occur. So I used the logic written in John's article, put all the strings in an array, and spliced ​​them together at the end of the program.



var r = [];
r.push(&#39;My skills:&#39;); 
for(var index in this.skills) {
r.push(&#39;<a href="">&#39;);
r.push(this.skills[index]);
r.push(&#39;</a>&#39;);
}
return r.join(&#39;&#39;);
Copy after login

The next step is to collect different lines of code in the template to generate functions. Through the method introduced earlier, we can know which placeholders (Translator's Note: or regular expression matches) are in the template and their positions. Therefore, relying on an auxiliary variable (cursor, cursor), we can get the desired results.



var TemplateEngine = function(tpl, data) {
  var re = /<%([^%>]+)?%>/g,
    code = 'var r=[];\n',
    cursor = 0;
  var add = function(line) {
    code += 'r.push("' + line.replace(/"/g, '\\"') + '");\n';
  }
  while(match = re.exec(tpl)) {
    add(tpl.slice(cursor, match.index));
    add(match[1]);
    cursor = match.index + match[0].length;
  }
  add(tpl.substr(cursor, tpl.length - cursor));
  code += 'return r.join("");'; // <-- return the result
  console.log(code);
  return tpl;
}
var template = &#39;<p>Hello, my name is <%this.name%>. I\&#39;m <%this.profile.age%> years old.</p>&#39;;
console.log(TemplateEngine(template, {
  name: "Krasimir Tsonev",
  profile: { age: 29 }
}));
Copy after login

  上述代码中的变量code保存了函数体。开头的部分定义了一个数组。游标cursor告诉我们当前解析到了模板中的哪个位置。我们需要依靠它来遍历整个模板字符串。此外还有个函数add,它负责把解析出来的代码行添加到变量code中去。有一个地方需要特别注意,那就是需要把code包含的双引号字符进行转义(escape)。否则生成的函数代码会出错。如果我们运行上面的代码,我们会在控制台里面看见如下的内容:


var r=[];
r.push("<p>Hello, my name is ");
r.push("this.name");
r.push(". I&#39;m ");
r.push("this.profile.age");
return r.join("");
Copy after login

  等等,貌似不太对啊,this.name和this.profile.age不应该有引号啊,再来改改。


var add = function(line, js) {
  js? code += &#39;r.push(&#39; + line + &#39;);\n&#39; :
    code += &#39;r.push("&#39; + line.replace(/"/g, &#39;\\"&#39;) + &#39;");\n&#39;;
}
while(match = re.exec(tpl)) {
  add(tpl.slice(cursor, match.index));
  add(match[1], true); // <-- say that this is actually valid js
  cursor = match.index + match[0].length;
}
Copy after login

  占位符的内容和一个布尔值一起作为参数传给add函数,用作区分。这样就能生成我们想要的函数体了。


var r=[];
r.push("<p>Hello, my name is ");
r.push(this.name);
r.push(". I&#39;m ");
r.push(this.profile.age);
return r.join("");
Copy after login

  剩下来要做的就是创建函数并且执行它。因此,在模板引擎的最后,把原本返回模板字符串的语句替换成如下的内容:

return new Function(code.replace(/[\r\t\n]/g, &#39;&#39;)).apply(data);
Copy after login

  我们甚至不需要显式地传参数给这个函数。我们使用apply方法来调用它。它会自动设定函数执行的上下文。这就是为什么我们能在函数里面使用this.name。这里this指向data对象。

  模板引擎接近完成了,不过还有一点,我们需要支持更多复杂的语句,比如条件判断和循环。我们接着上面的例子继续写。


var template = 
&#39;My skills:&#39; + 
&#39;<%for(var index in this.skills) {%>&#39; + 
&#39;<a href="#"><%this.skills[index]%></a>&#39; +
&#39;<%}%>&#39;;
console.log(TemplateEngine(template, {
  skills: ["js", "html", "css"]
}));
Copy after login

  这里会产生一个异常,Uncaught SyntaxError: Unexpected token for。如果我们调试一下,把code变量打印出来,我们就能发现问题所在。


var r=[];
r.push("My skills:");
r.push(for(var index in this.skills) {);
r.push("<a href=\"\">");
r.push(this.skills[index]);
r.push("</a>");
r.push(});
r.push("");
return r.join("");
Copy after login

  带有for循环的那一行不应该被直接放到数组里面,而是应该作为脚本的一部分直接运行。所以我们在把内容添加到code变量之前还要多做一个判断。


var re = /<%([^%>]+)?%>/g,
  reExp = /(^( )?(if|for|else|switch|case|break|{|}))(.*)?/g,
  code = &#39;var r=[];\n&#39;,
  cursor = 0;
var add = function(line, js) {
  js? code += line.match(reExp) ? line + &#39;\n&#39; : &#39;r.push(&#39; + line + &#39;);\n&#39; :
    code += &#39;r.push("&#39; + line.replace(/"/g, &#39;\\"&#39;) + &#39;");\n&#39;;
}
Copy after login

  这里我们新增加了一个正则表达式。它会判断代码中是否包含if、for、else等等关键字。如果有的话就直接添加到脚本代码中去,否则就添加到数组中去。运行结果如下:


var r=[];
r.push("My skills:");
for(var index in this.skills) {
r.push("<a href=\"#\">");
r.push(this.skills[index]);
r.push("</a>");
}
r.push("");
return r.join("");
Copy after login

  当然,编译出来的结果也是对的。

My skills:<a href="#">js</a><a href="#">html</a><a href="#">css</a>
Copy after login

  最后一个改进可以使我们的模板引擎更为强大。我们可以直接在模板中使用复杂逻辑,例如:


var template = 
&#39;My skills:&#39; + 
&#39;<%if(this.showSkills) {%>&#39; +
  &#39;<%for(var index in this.skills) {%>&#39; + 
  &#39;<a href="#"><%this.skills[index]%></a>&#39; +
  &#39;<%}%>&#39; +
&#39;<%} else {%>&#39; +
  &#39;<p>none</p>&#39; +
&#39;<%}%>&#39;;
console.log(TemplateEngine(template, {
  skills: ["js", "html", "css"],
  showSkills: true
}));
Copy after login

  除了上面说的改进,我还对代码本身做了些优化,最终版本如下:


var TemplateEngine = function(html, options) {
  var re = /<%([^%>]+)?%>/g, reExp = /(^( )?(if|for|else|switch|case|break|{|}))(.*)?/g, code = &#39;var r=[];\n&#39;, cursor = 0;
  var add = function(line, js) {
    js? (code += line.match(reExp) ? line + &#39;\n&#39; : &#39;r.push(&#39; + line + &#39;);\n&#39;) :
      (code += line != &#39;&#39; ? &#39;r.push("&#39; + line.replace(/"/g, &#39;\\"&#39;) + &#39;");\n&#39; : &#39;&#39;);
    return add;
  }
  while(match = re.exec(html)) {
    add(html.slice(cursor, match.index))(match[1], true);
    cursor = match.index + match[0].length;
  }
  add(html.substr(cursor, html.length - cursor));
  code += &#39;return r.join("");&#39;;
  return new Function(code.replace(/[\r\t\n]/g, &#39;&#39;)).apply(options);
}
Copy after login

The above is the detailed content of How to use javascript to write template engine code ideas and detailed examples. 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 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)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages ​​and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Dec 17, 2023 pm 06:55 PM

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts Dec 18, 2023 pm 03:39 PM

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

See all articles