• Welcome to the world's largest Chinese hacker forum

    Welcome to the world's largest Chinese hacker forum, our forum registration is open! You can now register for technical communication with us, this is a free and open to the world of the BBS, we founded the purpose for the study of network security, please don't release business of black/grey, or on the BBS posts, to seek help hacker if violations, we will permanently frozen your IP and account, thank you for your cooperation. Hacker attack and defense cracking or network Security

    business please click here: Creation Security  From CNHACKTEAM

Recommended Posts

实现步骤:

1.定义模板结构

2.预调用模板引擎

3.封装模板函数

4.导入并使用自定义的模板引擎

定义模板结构:

dttarim0lmi3205.png

预调用模板引擎:

gklduwmehm33206.png

封装模板函数:

je0a2jzi3hv3207.png

导入并使用自定义的模板引擎:

wkgzdrwqw2u3208.png

整体示例代码:

!声明文档类型

html lang='en '

meta charset='UTF-8 '

meta http-equiv=' X-UA-Compatible ' content=' IE=edge '

meta name=' viewport ' content=' width=device-width,initial-scale=1.0 '

标题文档/标题

脚本src='/js文件/模板。js '/脚本

/头

身体

div id='用户框/div

/body

脚本类型='text/html' id='tpl-user '

差异姓名:{{name}}/div

差异年龄:{{age}}/div

差异性别:{{gender}}/div

差异住址:{{address}}/div

/脚本

脚本

//定义数据

定义变量数据={

名称: 'zs ':

年龄: 28,

性别: '男,

地址: '北京顺义马坡'

}

//调用模板函数

var htmlStr=template('tpl-user ',data)

//渲染超文本标记语言结构

文档。getelementbyid(“用户框”).innerHTML=htmlStr

/脚本

/html

js:

函数模板(id,数据){

var str=文档。getelementbyid(id).innerHTML

var pattern=/{ { \ s *(a-zA-Z))\ s * } }/

var a=null

while((a=pattern.exec(str))){

str=str.replace(a[0],data[a[1]])

}

返回字符串

}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now