JS模板編譯的實現(xiàn)詳情
前言
編譯是一種格式變成另一種格式的過程。編譯會導(dǎo)致好的結(jié)果,比如書寫簡單的代碼,編譯出來復(fù)雜的代碼;或者提高代碼的使用性能。
這里只聊聊模板編譯。
模板編譯的簡單實現(xiàn)
寫一個最簡單的模板
<p>Hello, {{name}}!</p>這個模板用數(shù)據(jù){name: "world"}渲染后的結(jié)果是:
<p>Hello, world!</p>
解決方法:最簡單的方案,正則替換就行了
const compile = function(template, data) {
return template.replace(/{{(.+?)}}/g, (match, key) => data[key])
}
const template = "<p>Hello, I'm {{name}}! {{age}} years old!</p>"
const data = {
name: "hayes",
age: 18
}
const result = compile(template, data)
console.log(result); // <p>Hello, I'm hayes! 18 years old!</p>缺點很明顯,除了正則替換字段,其他啥都干不了,
來看看簡單的嵌套需求:
模板:
<p>Hello, I'm {{user.name}}! {{user.age}} years old!</p>渲染數(shù)據(jù);
const data = {
user: {
name: "hayes",
age: 18
}
}現(xiàn)在再使用上面的方法,就失效了。還用正則的話,會很難做。因為需要做語法/詞法分析,來看看大括號內(nèi)寫的是什么了。
模板編譯
其實對于上述的模板,也可以使用如下方式來寫:
const compile = function(data) {
return `<p>Hello, I'm ${data.name}! ${data.age} years old!</p>`
}好處:只需一次編譯,之后再使用就只需要直接填充數(shù)據(jù)即可。而且也方便支持data.user.name這種形式。
工具:使用new Function生成函數(shù)
生成一個函數(shù),傳入x和 y,執(zhí)行return x + y來獲得求和的功能
const fn = new Function("x", "y", "return x + y");打印fn,可以看到輸出的內(nèi)容如下:
? anonymous(x,y) {
return x + y
}1、構(gòu)建模板生成函數(shù)
傳入模板字符串,通過new Function方式返回一個新函數(shù)。新函數(shù)接收一個obj對象
const compile = function(template) {
// 模板字符串
let result = "";
// ...
return new Function("obj", result);
}2、正則替換
把{{xxx}}找出來,替換為obj.xxx
const compile2 = function(template) {
// 模板字符串
let result = template.replace(/{{(.+?)}}/g, (match, key) => {
return `obj.${key}`
});
result = `return "${result}"`;
return new Function("obj", result);
}
const template2 = "<p>Hello, I'm {{user.name}}! {{user.age}} years old!</p>"
const render2 = compile2(template2)
console.log(render2);此時,函數(shù)打印如下:
? anonymous(obj
) {
return "<p>Hello, I'm obj.user.name! obj.user.age years old!</p>"
}我們需要把字符串中的obj.user.name和obj.user.age變成動態(tài)的。
修改一下正則
const compile2 = function(template) {
// 模板字符串
let result = template.replace(/{{(.+?)}}/g, (match, key) => {
return `" + obj.${key} + "` // 前后添上加號
});
result = `return "${result}"`;
return new Function("obj", result);
}
const template2 = "<p>Hello, I'm {{user.name}}! {{user.age}} years old!</p>"
const render2 = compile2(template2)
console.log(render2);再來看看函數(shù)的打印:
? anonymous(obj
) {
return "<p>Hello, I'm " + obj.user.name + "! " + obj.user.age + " years old!</p>"
}最終代碼:
const compile = function(template) {
// 模板字符串
let result = template.replace(/{{(.+?)}}/g, (match, key) => {
return `" + obj.${key} + "`
});
result = `return "${result}"`;
return new Function("obj", result);
}
const template = "<p>Hello, I'm {{user.name}}! {{user.age}} years old!</p>"
const render = compile(template)
const data = {
user: {
name: "hayes",
age: 18
}
}
const result = render(data)
console.log(result); // <p>Hello, I'm hayes! 18 years old!</p>渲染結(jié)果:
"<p>Hello, I'm hayes! 18 years old!</p>"
到此這篇關(guān)于JS模板編譯的實現(xiàn)詳情的文章就介紹到這了,更多相關(guān)JS模板編譯 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
前端本地數(shù)據(jù)存儲的幾種常見方式總結(jié)
在前端開發(fā)中,本地數(shù)據(jù)存儲是實現(xiàn)客戶端數(shù)據(jù)持久化的關(guān)鍵技術(shù),以下是幾種常見的前端本地數(shù)據(jù)存儲方式,通過代碼示例講解的非常詳細,需要的朋友可以參考下2025-01-01
JavaScript 中 avalon綁定屬性總結(jié)
avalon是前端MVVM框架,在js中經(jīng)常會用到。這篇文章主要介紹了JavaScript 中 avalon綁定屬性總結(jié)的相關(guān)資料,需要的朋友可以參考下2016-10-10
關(guān)于javaScript注冊click事件傳遞參數(shù)的不成功問題
在javaScript中給一個html元素注冊click事件處理函數(shù)時,比如給該處理函數(shù)傳3個參數(shù)。可是不管是使用下面那種方式都不能給事件處理函數(shù)傳遞參數(shù)2014-07-07

