{"id":18588136,"url":"https://github.com/github1586/arttemplate","last_synced_at":"2025-10-23T22:59:13.524Z","repository":{"id":85607448,"uuid":"78518988","full_name":"github1586/artTemplate","owner":"github1586","description":null,"archived":false,"fork":false,"pushed_at":"2024-03-22T08:23:01.000Z","size":293,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-04-11T01:41:33.299Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/github1586.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-01-10T09:40:30.000Z","updated_at":"2024-03-22T08:22:57.000Z","dependencies_parsed_at":null,"dependency_job_id":"fd34970c-20de-46c0-a730-c606dcdc9f30","html_url":"https://github.com/github1586/artTemplate","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/github1586/artTemplate","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/github1586%2FartTemplate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/github1586%2FartTemplate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/github1586%2FartTemplate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/github1586%2FartTemplate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/github1586","download_url":"https://codeload.github.com/github1586/artTemplate/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/github1586%2FartTemplate/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":280706838,"owners_count":26376991,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-23T02:00:06.710Z","response_time":142,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-07T00:44:50.216Z","updated_at":"2025-10-23T22:59:13.510Z","avatar_url":"https://github.com/github1586.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# artTemplate-3.0\n\n新一代 javascript 模板引擎\n\n##      \t目录\n\n*\t[特性](#特性)\n*\t[快速上手](#快速上手)\n*\t[模板语法](#模板语法)\n*\t[下载](#下载)\n*\t[方法](#方法)\n*\t[NodeJS](#nodejs)\n*\t[使用预编译](#使用预编译)\n*\t[更新日志](#更新日志)\n*\t[授权协议](#授权协议)\n\n##\t特性\n\n1.\t性能卓越，执行速度通常是 Mustache 与 tmpl 的 20 多倍（[性能测试](http://aui.github.com/artTemplate/test/test-speed.html)）\n2.\t支持运行时调试，可精确定位异常模板所在语句（[演示](http://aui.github.io/artTemplate/demo/debug.html)）\n3.\t对 NodeJS Express 友好支持\n4.\t安全，默认对输出进行转义、在沙箱中运行编译后的代码（Node版本可以安全执行用户上传的模板）\n5.\t支持``include``语句\n6.\t可在浏览器端实现按路径加载模板（[详情](#使用预编译)）\n7.\t支持预编译，可将模板转换成为非常精简的 js 文件\n8.\t模板语句简洁，无需前缀引用数据，有简洁版本与原生语法版本可选\n9.\t支持所有流行的浏览器\n\n## 快速上手\n\n### 编写模板\n\n使用一个``type=\"text/html\"``的``script``标签存放模板：\n\t\n\t\u003cscript id=\"test\" type=\"text/html\"\u003e\n\t\u003ch1\u003e{{title}}\u003c/h1\u003e\n\t\u003cul\u003e\n\t    {{each list as value i}}\n\t        \u003cli\u003e索引 {{i + 1}} ：{{value}}\u003c/li\u003e\n\t    {{/each}}\n\t\u003c/ul\u003e\n\t\u003c/script\u003e\n\n### 渲染模板\n\t\n\tvar data = {\n\t\ttitle: '标签',\n\t\tlist: ['文艺', '博客', '摄影', '电影', '民谣', '旅行', '吉他']\n\t};\n\tvar html = template('test', data);\n\tdocument.getElementById('content').innerHTML = html;\n\n\n[演示](http://aui.github.com/artTemplate/demo/basic.html)\n\n##\t模板语法\n\n有两个版本的模板语法可以选择。\n\n###\t简洁语法\n\n推荐使用，语法简单实用，利于读写。\n\n\t{{if admin}}\n\t\t{{include 'admin_content'}}\n\t\t\n\t\t{{each list}}\n\t\t\t\u003cdiv\u003e{{$index}}. {{$value.user}}\u003c/div\u003e\n\t\t{{/each}}\n\t{{/if}}\n\t\n[查看语法与演示](https://github.com/aui/artTemplate/wiki/syntax:simple)\n\n###\t原生语法\n\t\n\t\u003c%if (admin){%\u003e\n\t\t\u003c%include('admin_content')%\u003e\n\t\n\t\t\u003c%for (var i=0;i\u003clist.length;i++) {%\u003e\n\t\t\t\u003cdiv\u003e\u003c%=i%\u003e. \u003c%=list[i].user%\u003e\u003c/div\u003e\n\t\t\u003c%}%\u003e\n\t\u003c%}%\u003e\n\n[查看语法与演示](https://github.com/aui/artTemplate/wiki/syntax:native)\n\n##\t下载\n\n* [template.js](https://raw.github.com/aui/artTemplate/master/dist/template.js) *(简洁语法版, 2.7kb)* \n* [template-native.js](https://raw.github.com/aui/artTemplate/master/dist/template-native.js) *(原生语法版, 2.3kb)*\n\n## 方法\n\n###\ttemplate(id, data)\n\n根据 id 渲染模板。内部会根据``document.getElementById(id)``查找模板。\n\n如果没有 data 参数，那么将返回一渲染函数。\n\n###\ttemplate.``compile``(source, options)\n\n将返回一个渲染函数。[演示](http://aui.github.com/artTemplate/demo/compile.html)\n\n###\ttemplate.``render``(source, options)\n\n将返回渲染结果。\n\n###\ttemplate.``helper``(name, callback)\n\n添加辅助方法。\n\n例如时间格式器：[演示](http://aui.github.com/artTemplate/demo/helper.html)\n\n###\ttemplate.``config``(name, value)\n\n更改引擎的默认配置。\n\n字段 | 类型 | 默认值| 说明\n------------ | ------------- | ------------ | ------------\nopenTag | String | ``'{{'`` | 逻辑语法开始标签\ncloseTag | String | ``\"}}\"`` | 逻辑语法结束标签\nescape | Boolean | ``true`` | 是否编码输出 HTML 字符\ncache | Boolean | ``true`` | 是否开启缓存（依赖 options 的 filename 字段）\ncompress | Boolean | ``false`` | 是否压缩 HTML 多余空白字符\n\t\n##\t使用预编译 \n\n可突破浏览器限制，让前端模板拥有后端模板一样的同步“文件”加载能力：\n\n一、**按文件与目录组织模板**\n\n```\ntemplate('tpl/home/main', data)\n```\n\n二、**模板支持引入子模板**\n\n\n\t{{include '../public/header'}}\n\n###\t基于预编译：\n\n*\t可将模板转换成为非常精简的 js 文件（不依赖引擎）\n*\t使用同步模板加载接口\n*\t支持多种 js 模块输出：AMD、CMD、CommonJS\n*\t支持作为 GruntJS 插件构建\n*\t前端模板可共享给 NodeJS 执行\n*\t自动压缩打包模板\n\n预编译工具：[TmodJS](http://github.com/aui/tmodjs/)\n\n##\tNodeJS\n\n###\t安装\n\n\tnpm install art-template\n\t\n###\t使用\n\n\tvar template = require('art-template');\n\tvar data = {list: [\"aui\", \"test\"]};\n\t\n\tvar html = template(__dirname + '/index/main', data);\n\n###\t配置\n\nNodeJS 版本新增了如下默认配置：\n\t\n字段 | 类型 | 默认值| 说明\n------------ | ------------- | ------------ | ------------\nbase | String | ``''`` | 指定模板目录\nextname | String | ``'.html'`` | 指定模板后缀名\nencoding | String | ``'utf-8'`` | 指定模板编码\n\t\n配置``base``指定模板目录可以缩短模板的路径，并且能够避免``include``语句越级访问任意路径引发安全隐患，例如：\n\t\n\ttemplate.config('base', __dirname);\n\tvar html = template('index/main', data)\n\t\n###\tNodeJS + Express\n\n\tvar template = require('art-template');\n\ttemplate.config('base', '');\n\ttemplate.config('extname', '.html');\n\tapp.engine('.html', template.__express);\n\tapp.set('view engine', 'html');\n\t//app.set('views', __dirname + '/views');\n\t\n运行 demo:\n\n\tnode demo/node-template-express.js\n\t\n\u003e 若使用 js 原生语法作为模板语法，请改用 ``require('art-template/node/template-native.js')``\n\n##\t升级参考\n\n为了适配 NodeJS express，artTemplate v3.0.0 接口有调整。\n\n###\t接口变更\n\n1.\t默认使用简洁语法\n2. ``template.render()``方法的第一个参数不再是 id，而是模板字符串\n3. 使用新的配置接口``template.config()``并且字段名有修改\n4. ``template.compile()``方法不支持 id 参数\n5. helper 方法不再强制原文输出，是否编码取决于模板语句\n6. ``template.helpers`` 中的``$string``、``$escape``、``$each``已迁移到``template.utils``中\n7. ``template()``方法不支持传入模板直接编译\n\n###\t升级方法\n\n1. 如果想继续使用 js 原生语法作为模板语言，请使用 [template-native.js](https://raw.github.com/aui/artTemplate/master/dist/template-native.js)\n2. 查找项目```template.render```替换为```template```\n3. 使用``template.config(name, value)``来替换以前的配置\n4. ``template()``方法直接传入的模板改用``template.compile()``（v2初期版本）\n\n## 更新日志\n\n###\tv3.0.3\n\n1. 解决``template.helper()``方法传入的数据被转成字符串的问题 #96\n2. 解决``{{value || value2}}``被识别为管道语句的问题 #105 \u003chttps://github.com/aui/tmodjs/issues/48\u003e\n\n###\tv3.0.2\n\n1.\t~~解决管道语法必须使用空格分隔的问题~~\n\n### v3.0.1\n\n1.\t适配 express3.x 与 4.x，修复路径 BUG\n\n### v3.0.0\n\n1. 提供 NodeJS 专属版本，支持使用路径加载模板，并且模板的``include``语句也支持相对路径\n2. 适配 [express](http://expressjs.com) 框架\n3. 内置``print``语句支持传入多个参数\n4. 支持全局缓存配置\n5. 简洁语法版支持管道风格的 helper 调用，例如：``{{time | dateFormat:'yyyy年 MM月 dd日 hh:mm:ss'}}``\n\n当前版本接口有调整，请阅读 [升级参考](#升级参考)\n\n\u003e artTemplate 预编译工具 [TmodJS](https://github.com/aui/tmodjs) 已更新\n\n###\tv2.0.4\n\n1.\t修复低版本安卓浏览器编译后可能产生语法错误的问题（因为此版本浏览器 js 引擎存在 BUG）\n\n###\tv2.0.3\n\n1.\t优化辅助方法性能\n2.\tNodeJS 用户可以通过 npm 获取 artTemplate：``$ npm install art-template -g``\n3.\t不转义输出语句推荐使用``\u003c%=#value%\u003e``（兼容 v2.0.3 版本之前使用的``\u003c%==value%\u003e``），而简版语法则可以使用``{{#value}}``\n4.\t提供简版语法的合并版本 dist/[template-simple.js](https://raw.github.com/aui/artTemplate/master/dist/template-simple.js)\n\n### v2.0.2\n\n1.\t优化自定义语法扩展，减少体积\n2.\t[重要]为了最大化兼容第三方库，自定义语法扩展默认界定符修改为``{{``与``}}``。\n3.\t修复合并工具的BUG [#25](https://github.com/aui/artTemplate/issues/25)\n4.\t公开了内部缓存，可以通过``template.cache``访问到编译后的函数\n5.\t公开了辅助方法缓存，可以通过``template.helpers``访问到\n6.\t优化了调试信息\n\n### v2.0.1\n\n1.\t修复模板变量静态分析的[BUG](https://github.com/aui/artTemplate/pull/22)\n\n### v2.0 release\n\n1.\t~~编译工具更名为 atc，成为 artTemplate 的子项目单独维护：\u003chttps://github.com/cdc-im/atc\u003e~~\n\n### v2.0 beta5\n\n1. 修复编译工具可能存在重复依赖的问题。感谢 @warmhug\n2. 修复预编译``include``内部实现可能产生上下文不一致的问题。感谢 @warmhug\n3. 编译工具支持使用拖拽文件进行单独编译\n\n### v2.0 beta4\n\n1. 修复编译工具在压缩模板可能导致 HTML 意外截断的问题。感谢 @warmhug\n2. 完善编译工具对``include``支持支持，可以支持不同目录之间模板嵌套\n3. 修复编译工具没能正确处理自定义语法插件的辅助方法\n\n### v2.0 beta1\n\n1.\t对非 String、Number 类型的数据不输出，而 Function 类型求值后输出。\n2.\t默认对 html 进行转义输出，原文输出可使用``\u003c%==value%\u003e``（备注：v2.0.3 推荐使用``\u003c%=#value%\u003e``），也可以关闭默认的转义功能``template.defaults.escape = false``。\n3.\t增加批处理工具支持把模板编译成不依赖模板引擎的 js 文件，可通过 RequireJS、SeaJS 等模块加载器进行异步加载。\n\n## 授权协议\n\nReleased under the MIT, BSD, and GPL Licenses\n\n============\n\n[所有演示例子](http://aui.github.com/artTemplate/demo/index.html) | [引擎原理](http://cdc.tencent.com/?p=5723)\n\n© tencent.com\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgithub1586%2Farttemplate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgithub1586%2Farttemplate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgithub1586%2Farttemplate/lists"}