ajbdev / twigjspackagist
Twig.js 是 Twig PHP 模板语言的纯 JavaScript 实现(http://twig.sensiolabs.org/)
This package is not auto-updated.
Last update: 2024-09-14 13:33:40 UTC
README
关于
Twig.js 是 Twig PHP 模板语言的纯 JavaScript 实现 (http://twig.sensiolabs.org/)
目标是提供与浏览器和服务器端容器(如 node.js)兼容的库。
Twig.js 目前仍在开发中,并支持 Twig 模板语言的有限子集(更多功能即将推出)。
文档可在 Github 上的 twig.js wiki 上找到。
功能支持
有关支持的标签/过滤器/函数/测试的列表,请参阅维基上的 实现说明。
Node.js 使用
可以使用 NPM 安装 Twig.js
npm install twig
您可以使用以下方式将 twig 包含到您的应用程序中
var twig = require('twig');
Twig 与 express 2 和 3 兼容。您可以通过将视图引擎设置为 twig 来使用 twig.js 模板语言创建一个 express 应用程序。
app.js
Express 3
var Twig = require("twig"), express = require('express'), app = express(); // This section is optional and used to configure twig. app.set("twig options", { strict_variables: false }); app.get('/', function(req, res){ res.render('index.twig', { message : "Hello World" }); }); app.listen(9999);
Express 2
var twig = require("twig"), app = require('express').createServer(); app.configure(function () { app.set('view engine', 'twig'); app.set("view options", { layout: false }); // This section is optional and used to configure twig. app.set("twig options", { strict_variables: false }); }); app.register('twig', twig); app.get('/', function(req, res){ res.render('index', { message : "Hello World" }); }); app.listen(9999);
views/index.twig
Message of the moment: <b>{{ message }}</b>
浏览器使用
将 twig.js 或 twig.min.js 包含到您的页面中,然后
var template = twig({ data: 'The {{ baked_good }} is a lie.' }); console.log( template.render({baked_good: 'cupcake'}) ); // outputs: "The cupcake is a lie."
测试
tiwg.js 测试是用 Mocha 编写的,可以通过 make test
运行。
许可证
Twig.js 在 BSD 2-Clause License 下提供,有关更多信息,请参阅 LICENSE 文件。
致谢
请参阅 LICENSES.md 文件以获取引用的许可证副本。
-
src/twig.fills.js 中的 JavaScript 数组填充来自 https://mdn.org.cn/,并在 MIT 许可证 或公共领域下提供。
-
src/twig.lib.js 中的 Date.format 函数来自 http://jpaq.org/,并在 MIT 许可证 下使用。
-
src/twig.lib.js 中的 sprintf 实现用于格式过滤器,来自 http://www.diveintojavascript.com/projects/javascript-sprintf,并在 BSD 3-Clause 许可证 下使用。
-
src/twig.lib.js 中的 strip_tags 实现用于 striptags 过滤器,来自 http://phpjs.org/functions/strip_tags,并在 MIT 许可证 下使用。