当前位置:网站首页>art-template 模板引擎
art-template 模板引擎
2022-04-23 09:51:00 【MyDreamingCode】
一、简介
说明:由于用字符串形式手动写入DOM元素,比较麻烦,于是引用模板。
总共提供两个东西:1.数据 2.模板 ps:外加一个js文件
template-web.js : 提供template函数 参数为 id, data
注意:在模板时,另用一个script标签,type="text/html":
1. 引入js文件
<script src="jquery.js"></script>
<script src="template-web.js"></script>
2. 提供数据
<script>
var data = {
name:'zhangsan',
age:18,
hobby:[
'足球',
'篮球',
'手工'
]
}
</script>
3. 提供模板
<script type="text/html" id="content-template">
<p>我的名字叫{
{name}}</p>
<p>今年{
{age}}岁</p>
<p>{
{if age >= 18}}可以上网{
{else if age < 18}}不允许上网{
{/if}}</p>
<p>我的爱好有:</p>
<ul>
{
{each hobby}}
<li>{
{$index}}:{
{$value}}</li>
{
{/each}}
</ul>
{
{@text}}
</script>
二、完整代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="content"></div>
<script src="jquery.js"></script>
<script src="template-web.js"></script>
<script type="text/html" id="content-template">
<p>我的名字叫{
{name}}</p>
<p>今年{
{age}}岁</p>
<p>{
{if age >= 18}}可以上网{
{else if age < 18}}不允许上网{
{/if}}</p>
<p>我的爱好有:</p>
<ul>
{
{each hobby}}
<li>{
{$index}}:{
{$value}}</li>
{
{/each}}
</ul>
{
{@text}}
<div>注册时间:{
{time|dateFormat}}</div>
</script>
<script>
var data = {
name:'zhangsan',
age:18,
hobby:[
'足球',
'篮球',
'手工'
],
text:'<a href="javascript:;">Happy</a>',
time:new Date()
}
template.defaults.imports.dateFormat = function(date) {
var y = date.getFullYear();
var m = date.getMonth() + 1;
var d = date.getDate();
return y + '-' + m + '-' + d;
}
var htmlStr = template("content-template",data);
$("#content").html(htmlStr);
</script>
</body>
</html>
版权声明
本文为[MyDreamingCode]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_61706112/article/details/124280270
边栏推荐
- PHP notes (I): development environment configuration
- Es aggregation aggregation analysis
- Two methods of building Yum source warehouse locally
- 最长公共前串
- Acquisition of DOM learning elements JS
- P1390 sum of common divisor (Mobius inversion)
- JS what is an event? Event three elements and operation elements
- SAP debug debug for in, reduce and other complex statements
- DVWA range practice
- 从知识传播的维度对比分析元宇宙
猜你喜欢
面试官:说几个PHP常用函数,幸好我面试之前看到了这篇文章
DVWA range practice
Interviewer: let's talk about some commonly used PHP functions. Fortunately, I saw this article before the interview
Computer network security experiment II DNS protocol vulnerability utilization experiment
JS DOM event
MapReduce计算流程详解
Career planning and implementation in the era of meta universe
MySQL of database -- overview and installation
kernel-pwn学习(4)--Double Fetch&&0CTF2018-baby
Alibaba cloud architects interpret the four mainstream game architectures
随机推荐
LeetCode 1249. Minimum Remove to Make Valid Parentheses - FB高频题1
The sap export excel file opens and shows that the file format and extension of "XXX" do not match. The file may be damaged or unsafe. Do not open it unless you trust its source. Do you still want to
PHP笔记(一):开发环境配置
理解作用域
Skill point digging
MapReduce压缩
Golang force buckle leetcode 396 Rotation function
SAP excel has completed file level validation and repair. Some parts of this workbook may have been repaired or discarded.
《谷雨系列》空投
Alibaba cloud architects interpret the four mainstream game architectures
Go语言实践模式 - 函数选项模式(Functional Options Pattern)
Kernel PWN learning (3) -- ret2user & kernel ROP & qwb2018 core
Explanation of order and primitive root of number theory
Chinese Remainder Theorem and extended Chinese remainder theorem that can be understood by Aunt Baojie
Pyqt5 and communication
[codeforces - 208e] blood cousins
Code source daily question div1 (701-707)
JS what is an event? Event three elements and operation elements
GCD of p2257 YY (Mobius inversion)
php 二维数组指定元素相等后相加否则新增