mustache.js一个零依赖的模板系统实现

简介: mustache.js一个零依赖的模板系统实现

多种语言实现:http://mustache.github.io/

js版本文档:https://github.com/janl/mustache.js


简介:

mustache.js is a zero-dependency implementation of the mustache template system in JavaScript.

Node 环境使用

安装

$ npm install mustache --save

基本语法

变量: {
        {name}}

列表:{ {#list}}{ {.}}{ {/list}}
对象:{ {obj.name}} - { {obj.age}}

代码示例

const Mustache = require("Mustache");

var data = {
name: "Tom",
age: 23,
};

var template = "my name is { {name}}, and { {age}} years old";
var result = Mustache.render(template, data);
console.log(result);
// my name is Tom, and 23 years old

浏览器中使用

最终效果是把id=template 的内容渲染后替换到id=target

<script src="https://unpkg.com/mustache@;latest"></script>

<div id="target">Loading...</div>

<script id="template" type="x-tmpl-mustache">
hello { {name}}
</script>

<script>
(function () {
var template = document.getElementById("template").innerHTML;
var rendered = Mustache.render(template, { name: "Tom" });
document.getElementById("target").innerHTML = rendered;
})();
</script>

异步加载模板

模板文件 template.html

hello {
        {name}}

异步渲染

<script src="https://unpkg.com/mustache@;latest"></script>

<div id="target">Loading...</div>

<script>
(function () {
fetch("./template.html")
.then((response) => response.text())
.then((template) => {
var rendered = Mustache.render(template, { name: "Tom" });
document.getElementById("target").innerHTML = rendered;
});
})();
</script>


            </div>
目录
相关文章
|
6天前
|
数据采集 人工智能 安全
|
15天前
|
云安全 监控 安全
|
2天前
|
存储 SQL 大数据
删库跑路?别慌!Time Travel 带你穿回昨天的数据世界
删库跑路?别慌!Time Travel 带你穿回昨天的数据世界
245 156
|
9天前
|
SQL 自然语言处理 调度
Agent Skills 的一次工程实践
**本文采用 Agent Skills 实现整体智能体**,开发框架采用 AgentScope,模型使用 **qwen3-max**。Agent Skills 是 Anthropic 新推出的一种有别于mcp server的一种开发方式,用于为 AI **引入可共享的专业技能**。经验封装到**可发现、可复用的能力单元**中,每个技能以文件夹形式存在,包含特定任务的指导性说明(SKILL.md 文件)、脚本代码和资源等 。大模型可以根据需要动态加载这些技能,从而扩展自身的功能。目前不少国内外的一些框架也开始支持此种的开发方式,详细介绍如下。
655 5
|
12天前
|
人工智能 自然语言处理 API
一句话生成拓扑图!AI+Draw.io 封神开源组合,工具让你的效率爆炸
一句话生成拓扑图!next-ai-draw-io 结合 AI 与 Draw.io,通过自然语言秒出架构图,支持私有部署、免费大模型接口,彻底解放生产力,绘图效率直接爆炸。
792 152
|
20天前
|
机器学习/深度学习 人工智能 自然语言处理
Z-Image:冲击体验上限的下一代图像生成模型
通义实验室推出全新文生图模型Z-Image,以6B参数实现“快、稳、轻、准”突破。Turbo版本仅需8步亚秒级生成,支持16GB显存设备,中英双语理解与文字渲染尤为出色,真实感和美学表现媲美国际顶尖模型,被誉为“最值得关注的开源生图模型之一”。
1904 9
|
3天前
|
机器学习/深度学习 人工智能 监控
别把模型当宠物养:从 CI/CD 到 MLOps 的工程化“成人礼”
别把模型当宠物养:从 CI/CD 到 MLOps 的工程化“成人礼”
224 163