Files
menav/src/helpers/index.js
2025-05-09 00:14:28 +08:00

59 lines
1.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Handlebars助手函数中心
*
* 导入并重导出所有助手函数方便在generator中统一注册
*/
const formatters = require('./formatters');
const conditions = require('./conditions');
const utils = require('./utils');
/**
* 注册所有助手函数到Handlebars实例
* @param {Handlebars} handlebars Handlebars实例
*/
function registerAllHelpers(handlebars) {
// 注册格式化助手函数
Object.entries(formatters).forEach(([name, helper]) => {
handlebars.registerHelper(name, helper);
});
// 注册条件判断助手函数
Object.entries(conditions).forEach(([name, helper]) => {
handlebars.registerHelper(name, helper);
});
// 注册工具类助手函数
Object.entries(utils).forEach(([name, helper]) => {
handlebars.registerHelper(name, helper);
});
// 注册HTML转义函数作为助手函数方便在模板中调用
handlebars.registerHelper('escapeHtml', function(text) {
if (text === undefined || text === null) {
return '';
}
return String(text)
.replace(/&/g, "&")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#039;");
});
// 注册非转义助手函数安全输出HTML
handlebars.registerHelper('safeHtml', function(text) {
if (text === undefined || text === null) {
return '';
}
return new handlebars.SafeString(text);
});
}
// 导出所有助手函数和注册函数
module.exports = {
formatters,
conditions,
utils,
registerAllHelpers
};