Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CodeGenerator ¶
type CodeGenerator struct {
Engine TemplateEngine
FileExt string
}
CodeGenerator 使用 TemplateEngine 渲染并将结果写入磁盘
func NewCodeGeneratorWithEngine ¶
func NewCodeGeneratorWithEngine(engine TemplateEngine) *CodeGenerator
NewCodeGeneratorWithEngine 使用指定的引擎创建生成器
type EmbeddedTemplateEngine ¶
type EmbeddedTemplateEngine struct {
// contains filtered or unexported fields
}
EmbeddedTemplateEngine 从给定的 name->[]byte 映射构建模板集合并提供渲染。
func NewEmbeddedTemplateEngine ¶
func NewEmbeddedTemplateEngine(srcs map[string][]byte) (*EmbeddedTemplateEngine, error)
func NewEmbeddedTemplateEngineFromMap ¶
func NewEmbeddedTemplateEngineFromMap(srcs map[string][]byte, funcs template.FuncMap) (*EmbeddedTemplateEngine, error)
NewEmbeddedTemplateEngineFromMap 使用预先准备好的模板字节映射创建引擎, 可选传入 FuncMap,会在解析模板前调用 template.New(name).Funcs(funcs)。 键应为相对路径样式(使用 '/' 分隔),例如 `main.tpl` 或 `sub/main.tpl`。
func (*EmbeddedTemplateEngine) InstallFuncMap ¶
func (e *EmbeddedTemplateEngine) InstallFuncMap(funcs template.FuncMap)
func (*EmbeddedTemplateEngine) ListTemplates ¶
func (e *EmbeddedTemplateEngine) ListTemplates() []string
ListTemplates 返回可用模板名称列表(相对于映射的键,如 "main.tpl")。
type FileTemplateEngine ¶
type FileTemplateEngine struct {
// contains filtered or unexported fields
}
FileTemplateEngine 从磁盘加载并缓存模板
func NewFileTemplateEngine ¶
func NewFileTemplateEngine(root string) (*FileTemplateEngine, error)
NewFileTemplateEngine 创建并预加载模板目录(支持 .tpl/.tmpl 后缀)
func (*FileTemplateEngine) InstallFuncMap ¶
func (e *FileTemplateEngine) InstallFuncMap(funcs template.FuncMap)
func (*FileTemplateEngine) ListTemplates ¶
func (e *FileTemplateEngine) ListTemplates() []string
ListTemplates 列出可用模板名(相对于 root 的路径,使用 '/' 分隔)
type Generator ¶
type Generator interface {
// Generate 根据选项渲染指定模板并写入输出目录
Generate(ctx context.Context, opts Options, tplName string) (outputPath string, err error)
}
Generator 通用生成器:渲染指定模板并写入输出
type Options ¶
type Options struct {
// Module 模块名,例如 github.com/user/project
Module string
// ProjectName 项目名称,例如 project
ProjectName string
// OutDir 输出目录
OutDir string
// OutputName 输出文件名(可选),如果为空则使用模板名去掉后缀作为文件名
OutputName string
// Vars 额外变量映射,可在模板中使用
Vars map[string]interface{}
}
Options 生成选项:包含模块名、项目名、输出目录和变量等
Click to show internal directories.
Click to hide internal directories.