fix: 解决重构后自定义页面问题

1. 添加模板回退机制,当找不到指定模板时自动使用page.hbs通用模板
2. 支持在页面配置中通过template字段指定使用的模板文件
3. 更新配置文件和文档,添加template字段使用说明

Closes #10
This commit is contained in:
Zuoling Rong
2025-05-09 16:12:49 +08:00
parent fb32f06f19
commit 4a5f44bc71
7 changed files with 98 additions and 3 deletions

View File

@@ -88,7 +88,46 @@ function renderTemplate(templateName, data, useLayout = true) {
// 检查模板是否存在
if (!fs.existsSync(templatePath)) {
throw new Error(`Template ${templateName}.hbs not found. Cannot proceed without template.`);
// 尝试使用通用模板 page.hbs
const genericTemplatePath = path.join(process.cwd(), 'templates', 'pages', 'page.hbs');
if (fs.existsSync(genericTemplatePath)) {
console.log(`模板 ${templateName}.hbs 不存在,使用通用模板 page.hbs 代替`);
const genericTemplateContent = fs.readFileSync(genericTemplatePath, 'utf8');
const genericTemplate = handlebars.compile(genericTemplateContent);
// 添加 pageId 到数据中,以便通用模板使用
const enhancedData = {
...data,
pageId: templateName // 确保pageId在模板中可用
};
// 渲染页面内容
const pageContent = genericTemplate(enhancedData);
// 如果不使用布局,直接返回页面内容
if (!useLayout) {
return pageContent;
}
try {
// 使用辅助函数获取默认布局模板
const { template: layoutTemplate } = getDefaultLayoutTemplate();
// 准备布局数据,包含页面内容
const layoutData = {
...enhancedData,
body: pageContent
};
// 渲染完整页面
return layoutTemplate(layoutData);
} catch (layoutError) {
throw new Error(`Error rendering layout for ${templateName}: ${layoutError.message}`);
}
} else {
throw new Error(`Template ${templateName}.hbs not found and generic template page.hbs not found. Cannot proceed without template.`);
}
}
try {
@@ -660,7 +699,8 @@ function renderPage(pageId, config) {
// 准备页面数据
const data = {
...config,
currentPage: pageId
currentPage: pageId,
pageId // 同时保留pageId字段用于通用模板
};
// 确保navigation是数组
@@ -697,8 +737,15 @@ function renderPage(pageId, config) {
Object.assign(data, config[pageId]);
}
// 检查页面配置中是否指定了模板
let templateName = pageId;
if (config[pageId] && config[pageId].template) {
templateName = config[pageId].template;
console.log(`页面 ${pageId} 使用指定模板: ${templateName}`);
}
// 直接渲染页面内容不使用layout布局因为layout会在generateHTML中统一应用
return renderTemplate(pageId, data, false);
return renderTemplate(templateName, data, false);
}
/**