pongo2 渲染出来的结果还是模版
时间: 2023-12-05 14:03:28 浏览: 34
是的,Pongo2 渲染出来的结果仍然是模板。Pongo2 中的模板文件以 `.html` 或 `.txt` 结尾,文件内容包含了模板语言和 HTML 或纯文本文本。当 Pongo2 渲染模板时,它会把模板语言解析成最终的文本输出,但仍然保留了 HTML 或文本的格式。
例如,假设有以下模板文件 `template.html`:
```
<!DOCTYPE html>
<html>
<head>
<title>{{ .Title }}</title>
</head>
<body>
<h1>{{ .Heading }}</h1>
<p>{{ .Content }}</p>
</body>
</html>
```
当执行以下代码:
```
tpl, err := pongo2.FromFile("template.html")
if err != nil {
// 处理错误
}
ctx := pongo2.Context{
"Title": "My Page",
"Heading": "Welcome to my page",
"Content": "This is the content of my page",
}
out, err := tpl.Execute(ctx)
if err != nil {
// 处理错误
}
fmt.Println(out)
```
输出结果如下:
```
<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
</head>
<body>
<h1>Welcome to my page</h1>
<p>This is the content of my page</p>
</body>
</html>
```
可以看到,输出结果仍然是一个 HTML 文件,但其中的 `{{ .Title }}`、`{{ .Heading }}` 和 `{{ .Content }}` 已经被真正的内容替换了。