当前位置:网站首页>模板引擎art-template
模板引擎art-template
2022-08-08 06:21:00 【Youser511】
模板引擎
之前渲染ul遇到问题
字符串拼接,html标签和js混淆在一起,修改起来非常麻烦
var str = `
<li class="list-group-item">
<span class="badge" style="background-color: orchid;">评论时间:${item.time}</span>
<span class="badge" style="background-color:aquamarine;">评论人:${item.username}</span>
${item.content}
</li>
`
模板引擎网站
http://aui.github.io/art-template/zh-cn/docs/index.html
好处
- 减少了字符串的拼接
- 代码结构更简单
- 代码易于阅读和维护
模板引擎使用
<!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>
<!-- 1 导入模板引擎 -->
<!-- 提供一个全局函数 template('模板的id',需要渲染的数据) -->
<script src="./js/template-web.js"></script>
<script src="./js/jquery.js"></script>
</head>
<body>
<a href="#">百度</a>
<a href="javascript:;">百度</a>
<div id="container"></div>
<!-- 3定义模板 -->
<script type="text/html" id="tpl_user">
<div>
{
{name}}------{
{age}}
<hr/>
{
{if flag === 0}}
flag的值为0
{
{else if flag ===1}}
flag的值为1
{
{/if}}
</div>
<ul>
{
{each hobby}}
<li>索引为{
{$index}}循环的值为{
{$value}}</li>
{
{/each}}
</ul>
<h3>{
{time | dateFormat}}</h3>
</script>
<script>
// 定义处理时间的过滤器
template.defaults.imports.dateFormat = function(date){
var y = date.getFullYear();
var m = date.getMonth() + 1;
var d = date.getDate();
return y + '-' + m + '-' + d;
}
// 2 定义需要渲染的数据
var data = {
name:"zs",
age:12,
test:"<a href='#'>超链接</a>",
flag:1,
hobby:['吃','喝','玩','乐'],
time:new Date()
}
// template
console.log(template)
// 4调用template函数
var htmlStr = template('tpl_user',data);
console.log(htmlStr)
// 5渲染html结构
$("#container").html(htmlStr);
</script>
</body>
</html>
边栏推荐
- tkinter-TinUI-xml combat (7) PDF paging and merging
- uniapp H5 签名横版生成图片
- 【微信小程序】一文学懂小程序的数据绑定和事件绑定
- 结合实践总结docker 安装 mysql5.7
- 2022秋春招/提前批面经分享总结(字节、腾讯、阿里)
- YoloV4训练自己的数据集(二)
- YoloV4训练自己的数据集(五)
- The amount of parameters and calculation of neural network, is the neural network a parametric model?
- 开发两年,作为过来人的建议
- MySQL数据库
猜你喜欢
随机推荐
基于FTP协议的文件上传与下载
C face recognition
string hash hash value
Integer block sample
YoloV4训练自己的数据集(二)
Threads, control, communications
四面拿下字节2-2Offer,入职就是...
500道Golang 常⻅⾯试题⽬解析
Validated plan
postgres 安装 14 版本出现错误提示解决办法
Redis集群
torch.gather() usage interpretation
from sklearn import cross_validation 报错的解决方法
打开deploy和manager图形化界面打不开
leetcode 232. Implement Queue using Stacks 用栈实现队列(简单)
LLVM系列第二十九章:写一个简单的常量加法“消除”工具(Pass)
Key technologies for 4G/5G spectrum resource coordination
State Compression Review
word写论文中的一些方法
tf.train.MonitoredTrainingSession 控制 checkpoint 保存数量









