Files
easygoadmin-wiki/EasyGoAdmin_GoFrame_EleVue/zh/generator/templates.md
T
2026-10-20 21:19:44 +08:00

77 lines
3.0 KiB
Markdown
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.
# 10.6 模板开发
:::tip 概述
生成器模板在 templates/ 磁盘目录:后端模板用 Go text/template 默认 {{ }} 定界;前端模板因与 Vue 插值冲突改用 [[ ]] 定界。修改模板即改变生成产物风格。
:::
## 模板清单
```text
templates/
├── api.tpl api/v1/{module}.go
├── controller.tpl internal/controller/{module}.go
├── logic.tpl internal/logic/{module}.go(ModuleSpec 基座风格)
├── service.tpl internal/service/{module}.go(接口三件套)
├── dao_index.tpl internal/dao/{module}.go(挂钩子+Ctx 覆写)
├── dao_internal.tpl internal/dao/internal/{module}.go
├── entity.tpl internal/model/entity/{module}.go
├── do.tpl internal/model/do/{module}.go
├── ui/ 前端普通模板(定界符 [[ ]])
│ ├── api.ts.tpl
│ ├── columns.ts.tpl
│ ├── querySchemas.ts.tpl
│ ├── index.vue.tpl
│ ├── edit.vue.tpl
│ └── detail.vue.tpl
└── ui2/ 前端树形模板(无 columns/querySchemas)
├── api.ts.tpl index.vue.tpl edit.vue.tpl detail.vue.tpl
```
## 两套定界符(关键差异)
| 模板 | 定界符 | 原因 |
|------|--------|------|
| 后端 *.tpl | `{{ }}`(Go text/template 默认) | 无冲突 |
| templates/ui* | `[[ ]]`(Delims("[[","]]")) | Vue 模板插值也是 {{ }},会冲突 |
```text
后端模板片段:{{.Module}} {{range .Fields}}…
前端模板片段:[[.Module]] [[range .Fields]]… ← 生成后再渲染为 Vue 的 {{ }}
```
## 模板函数
```text
containsAny 字符串包含任一子串(字段分类判定)
isUrlField URL 列名判定(website/site/url/link 不作模糊筛选)
(其余上下文:buildVars 产出的 module/Pascal/Fields/Choices/HasStatus…)
```
## 修改模板的流程
```text
1. 编辑 templates/*.tpl(保持定界符约定)
2. 用 -preview 预览渲染结果(不落盘核对)
3. 生成样例表验证(建议用 example 表回归对比)
4. go build 检查产物可编译
5. 注意:overwrite=false 时已存在文件不会更新——
验证用新表或临时开 overwrite
```
## 与其他"模板"的区分
::: warning 两套模板互不相干
+ `templates/*.tpl`(本节)= 代码生成器渲染模板,随仓库版本管理
+ `goframe_file_template` 表 = 业务功能(在线管理文件/代码片段模板)
修改生成产物风格改 templates/*.tpl;不要去 file_template 表里找。
:::
## 定制建议
+ 团队规范(注释风格/方法命名)可改进模板一次、全组受益
+ 新增生成字段类型(如开关列特殊渲染)改 buildVars + 模板两侧
+ 模板变更走版本管理 + 团队评审(影响所有新生成模块)
+ 已生成模块不会随模板更新(除非 overwrite 重生成并人工合并)
+ hack/tpl/dao_index.tpl 是 gf gen dao 的自定义模板(与生成器模板同构但独立)——改一处需评估另一处