Buffalo 默认内置 Plush 模板引擎,无需安装;可手动替换为 html/template,需禁用 app.Templates 并自行渲染;自定义函数须在 actions/app.go 初始化时注册,且签名需导出;buffalo-plugins install 模板引擎命令已失效。

Buffalo 默认就带 Plush 模板引擎,不用额外安装
Buffalo 项目创建时已内置 Plush 作为默认模板引擎,templates/ 目录下所有 .plush.html 文件都会被自动识别和渲染,无需手动 go get 或注册。你看到的 home/index.plush.html、application.plush.html 就是它在工作的证据。
如果想换其他模板引擎(比如 html/template),得手动替换
Buffalo 不支持运行时切换模板引擎,但可以绕过内置流程,自己接管渲染逻辑:
- 在 handler 中跳过
c.Render(),改用标准html/template或text/template手动执行tmpl.Execute() - 禁用 Buffalo 的模板加载:在
app.go中设app.Templates = nil,否则c.Render()仍会尝试调用 Plush - 注意
c.Response()已被 Buffalo 封装,直接写 response body 前需确保未被中间件拦截(比如 gzip、session)
常见错误:Plush 模板里调用自定义函数失败
Plush 允许注册函数,但必须在模板加载前完成,且只能在 actions/app.go 的初始化阶段注册:
PigX UI Pro 前端开发指南 - Vue 3 + TypeScript + Element Plus。当用户提到 PigX UI、PigX 前端、lgb-mgui 项目、Vue 3 企业级后台开发、Element Plus 后台开发时使用此技能。
- 不要在
home.go或 handler 里注册——此时模板已加载完毕 - 注册方式是修改
app.Templates.AddFunc("myfunc", func() string { return "ok" }) - 函数签名必须是可导出的(首字母大写),且参数/返回值类型要匹配 Plush 调用上下文
- 若函数报
template: myfunc: function "myfunc" not defined,说明注册时机太晚或拼写不一致
别碰 buffalo-plugins install 试图“安装模板引擎”
像 buffalo plugins install github.com/gobuffalo/buffalo-plush 这类命令早已失效——Plush 是 Buffalo 核心依赖,硬装只会导致 go.mod 冲突或构建失败。当前版本(v1.0.1)中,Plush 由 github.com/gobuffalo/plush/v4 提供,版本锁定在 go.mod 里,手动干预反而破坏一致性。
立即学习“前端免费学习笔记(深入)”;

















