buffalo 的 layout 复用本质是显式 include 而非模板继承,需将 application.html 放于 templates/ 根目录并手动通过 partial 嵌入子模板,同时显式传参(如 title、metadesc)以实现差异化渲染。

Buffalo 的 layout 文件不是模板继承,而是显式 include
Buffalo 没有类似 Rails 的 yield 或 Django 的 {% block content %} 机制。所谓“layout 复用”,本质是把 templates/application.html 当作一个普通模板,用 render 或 partial 在其他模板里手动嵌入——它不自动包裹子模板,也不传递上下文变量。
常见错误现象:改了 application.html 但子页面没更新;或者在子模板里写 ,结果报错 function not defined。
-
application.html必须放在templates/根目录,且文件名必须是application.html(或application.plush.html),Buffalo 才会在调用r.HTML("users/show")时默认查找它 - 子模板(如
templates/users/show.html)里不能靠“自动注入”内容,得显式写,否则它只是独立渲染 - 如果想复用 header/nav/footer 片段,应单独建
templates/partials/header.html,再用调用,而不是塞进application.html里硬编码
如何让多个子模板共用同一 layout 并传入不同 title/meta
Buffalo 的 r.HTML() 不自动把参数透传给 layout,必须手动构造 map 并通过 partial 显式传递。否则所有页面 title 都一样,SEO 和调试都受影响。
正确做法是在每个子模板顶部手动组装 context:
title := "用户列表" metaDesc := "查看所有注册用户" body := `<h1>` + title + `</h1><p>这里是用户表格...</p>` <p>而 <code>application.html</code> 里要写成:</p><div class="aritcle_card flexRow artxards"> <div class="artcardd flexRow"> <a class="aritcle_card_img" rel="nofollow" href="/xiazai/skill8661" title="Golang Uber Dig"><img src="https://img.php.cn/upload/skill/000/000/081/179215391842073.jpg" alt="Golang Uber Dig" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a> <div class="aritcle_card_info flexColumn"> <a rel="nofollow" href="/xiazai/skill8661" title="Golang Uber Dig" class="overflowclass">Golang Uber Dig</a> <p class="overflowclass">使用 uber-go/dig 在 Golang 中实现依赖注入——基于反射容器,提供 Provide/Invoke、dig.In/dig.Out 参数与结果对象,支持命名依赖等</p> </div> <a rel="nofollow" href="/xiazai/skill8661" title="Golang Uber Dig" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span> </a> </div> </div> <pre class="brush:php;toolbar:false;"> <title></title><meta name="description" content="<%= meta_desc %>">
- 别在
application.html里直接调c.Param("title")——handler 没把值塞进去,c是空的 - 避免在 layout 里做逻辑判断(比如
if title == ""),Plush 不支持复杂表达式,容易静默失败 - 如果 title 来自数据库或 API,务必在 handler 里提前计算好,再传进模板;不要在
application.html里调db.Find()
为什么修改 application.html 后 buffalo dev 不热重载?
Buffalo 的热重载只监听 actions/ 和 models/ 目录,默认不 watch templates/ 下的 layout 文件——这是最常被忽略的坑。
现象:改了 application.html,刷新页面没变化,甚至重启 buffalo dev 都无效,直到删掉 tmp/ 目录才恢复。
- 临时解决:加
--watch ./templates参数启动:buffalo dev --watch ./templates - 长期方案:在
main.go或app.go里禁用模板缓存(仅开发环境):app.Options().Env = "development"; app.Options().TemplateOptions.DisableCache = true - 注意:
.plush.html文件即使加了 watch 也不会热重载,必须用.html后缀才能触发重新解析
多语言/多主题 layout 怎么安全切换
Buffalo 不提供 layout 动态路由或中间件注入机制,所谓“主题切换”只能靠 handler 决定用哪个 layout 模板,然后手动传参。
例如支持中英文 layout:
// 在 handler 里
lang := c.Param("lang")
layout := "application.html"
if lang == "zh" {
layout = "application_zh.html"
}
c.Set("layout", layout)
r.HTML("users/show")
然后在 users/show.html 里根据 c.Param("layout") 切换 partial:
- 别用
os.Getenv("LANG")或全局变量控制 layout——并发请求会互相覆盖 - 每个 layout 文件(
application.html,application_zh.html)必须结构一致,否则body渲染位置可能错乱 - 如果主题还涉及 CSS/JS,记得在
assets/css/下配对应文件,并在 layout 里用<link href="/css/<%=%20theme%20%>.css">引入
buffalo build 重新打包,上线后还是旧版本。部署前务必确认 tmp/ 目录已清空,或加 --no-cache 参数强制重建模板树。golang免费学习笔记(深入):立即使用
在学习笔记中,你将探索golang的核心概念和高级技巧!










