# resources.ExecuteAsTemplate
> 返回由 Go 模板创建的资源，它用给定上下文解析并执行。
- 官方英文原文：https://gohugo.io/functions/resources/executeastemplate/
- 本页规范地址：https://hugozh.cn/functions/resources/executeastemplate/
- 最近更新：2026-10-02
- 最后提交：912b1d3 chore(site): 添加 static/CNAME（hugozh.cn），供 GitHub Pages 等平台绑定自定义域名
- 签名：resources.ExecuteAsTemplate TARGETPATH CONTEXT RESOURCE
- 返回类型：resource.Resource
- 站点：Hugo 中文文档（https://hugozh.cn/）· 社区维护的非官方中文翻译，如有出入以官方英文原文为准

---
> [!NOTE]
> 该函数可用于全局资源、页面资源或远程资源。

`resources.ExecuteAsTemplate` 函数返回由 Go 模板创建的资源，它用给定上下文解析并执行，并以目标路径作为缓存键缓存结果。

调用资源的 [`Publish`][]、[`Permalink`][] 或 [`RelPermalink`][] 方法时，Hugo 会把该资源发布到目标路径。

假设你有一个 CSS 文件，希望用项目配置中的值填充它：

```go-html-template {file="assets/css/template.css"}
body {
  background-color: {{ site.Params.style.bg_color }};
  color: {{ site.Params.style.text_color }};
}
```

而项目配置中包含：

```toml
[params.style]
bg_color = '#fefefe'
text_color = '#222'
```

把下面的代码放进 baseof.html 模板：

```go-html-template
{{ with resources.Get "css/template.css" }}
  {{ with resources.ExecuteAsTemplate "css/main.css" $ . }}
    <link rel="stylesheet" href="{{ .RelPermalink }}">
  {{ end }}
{{ end }}
```

上面的示例：

1. 把该模板捕获为资源
1. 以当前页面作为上下文，把该资源作为模板执行
1. 把该资源发布到 css/main.css

结果是：

```css {file="public/css/main.css"}
body {
  background-color: #fefefe;
  color: #222;
}
```

[`Permalink`]: /methods/resource/permalink/
[`Publish`]: /methods/resource/publish/
[`RelPermalink`]: /methods/resource/relpermalink/

