当我从除 main 之外的其他 go 文件访问文件时,如何处理文件路径。
\n在 other.go 文件中,我尝试运行 ParseFS,但它给出了 template:pattern matches no files:templates/test.tmpl错误。这是我的文件树。
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 go.mod\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 main\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 main.go\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 other\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 other.go\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 templates\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test.tmpl\nRun Code Online (Sandbox Code Playgroud)\n其他/其他.go
\npackage other\n\nimport (\n "embed"\n "fmt"\n "html/template"\n)\n\nvar templateFS embed.FS\n\nfunc Check() error {\n _, err := template.New("email").ParseFS(templateFS, "templates/"+ "test.tmpl")\n\n if err != nil {\n fmt.Println(err)\n }\n return nil\n}\nRun Code Online (Sandbox Code Playgroud)\n主/main.go
\nfunc main() {\n err :=othher.Check()\n\n if err != nil {\n fmt.Println(err)\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n 我了解 Helm 图表中的用法.Values.varName,它用于引用Values.yaml文件中的变量
我想知道$.Values.varName和之间的区别.Values.varName
例如,当我设置name: $.Values.varName和name: .Values.varName
我使用go语言使用go-template提交HTML表单.得到一些奇怪的结果.
基本思想是我有一个名为Page的数据结构,其中包含一些元素.模板中填充了一系列Pages.在模板内部,我遍历每个页面并显示其内容.这些内容中的每一个都嵌入在HTML表单中,并带有相应的链接.点击链接后,它将提交相应的表单.
代码段如下:
{{range $index, $element := .Pages}}
<form action="/detailNews" id="readMore{{$index}}" method="post" name="readMore{{$index}}">
//displaying elements from each page
<div id="more">
<input name="query" type="hidden" value="{{printf "%s" .Title}}">
<a href="#" onclick="document.readMore{{$index}}.submit()">Read More</a>
</div>
</form>
{{end}}
Run Code Online (Sandbox Code Playgroud)
代码主要适用于一个小问题.id和name属性按预期生成输出,例如:readMore0,readMore1等.
问题出现在"a"标签上,其中onclick属性填充为:document.readMore 0 .submit(),document.readMore 1 .submit()等.注意0,1附近的空间.这样,单击链接时未找到相应的表单.
我无法弄清楚这个的原因.
任何帮助将受到高度赞赏.
谢谢,Ripul
我是golang的新手。为了学习它,我从使用gin框架的简单Web应用程序开始。我已经关注了gin doc和配置的模板文件,但无法使其工作。我遇到错误-
Run Code Online (Sandbox Code Playgroud)panic: html/template: pattern matches no files: `templates/*` goroutine 1 [running]: html/template.Must /usr/local/Cellar/go/1.5.2/libexec/src/html/template/template.go:330 github.com/gin-gonic/gin.(*Engine).LoadHTMLGlob /Users/ameypatil/deployment/go/src/github.com/gin-gonic/gin/gin.go:126 main.main() /Users/ameypatil/deployment/go/src/github.com/ameykpatil/gospike/main.go:17
下面是我的代码-
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
//os.Setenv("GIN_MODE", "release")
//gin.SetMode(gin.ReleaseMode)
// Creates a gin router with default middleware:
// logger and recovery (crash-free) middleware
router := gin.Default()
router.LoadHTMLGlob("templates/*")
//router.LoadHTMLFiles("templates/index.tmpl")
router.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"title": "GoSpike",
})
})
// By default it serves on :8080 unless a
// PORT environment variable was defined.
router.Run(":4848") …Run Code Online (Sandbox Code Playgroud) 我发现这个文档加入了两个字符串,但这在 go 模板中不起作用。
有没有办法在 go 模板中加入字符串?
在模板中,我该如何实现这一目标?
{{$var := template "my-template"}}
Run Code Online (Sandbox Code Playgroud)
我得到了"unexpected <template> in operand".
我正在尝试建立一个类似Reddit的网络论坛.有回复的顶级帖子,回复可以有回复等等.
董事会看起来像这样:
var board map[string]*Post
Run Code Online (Sandbox Code Playgroud)
和a Post:
type Post struct {
Title string
Body string
ID string
PostNum int
Replies []*Post
}
Run Code Online (Sandbox Code Playgroud)
我将如何使用模板通过嵌套到范围Replies切片(记住,每个*Post包含一个Replies包含片*Posts含有Replies等等)?
到目前为止我所拥有的:
<div id="posts">
{{ .Title }}
{{ .Body }}
<ul>
{{ range $key, $value := .Replies }}
<li class="post">
<div class="postHead">
<div class="postTitle"><b>{{ $value.ID }}</b></div>
</div>
<div class="postBody">{{ $value.PostNum }}</div>
</li>
<ul>
{{ range $key, $value := $value.Replies }}
<li class="post">
<div class="postHead">
<div class="postTitle"><b>{{ $value.ID …Run Code Online (Sandbox Code Playgroud) 我有一个简单的案例,其中模板(text/templates)包括这样的另一个
`index.html`
{{ template "image_row" . }}
`image_row.html`
{{ define "image_row" }}
To stuff here
{{ end }}
Run Code Online (Sandbox Code Playgroud)
现在我想重用图像行模板.假设我想传递一个简单的数字,以便image_row模板根据此数字构建行
我想有类似的东西(其中5是附加参数)
index.html
{{ template "image_row" . | 5 }}
Run Code Online (Sandbox Code Playgroud)
在这种情况下我怎么能实现呢?
我想显示.metadata.labels.app设置为"myapp"值的条目。
命令:
kubectl get pods -o go-template --template="{{range .items}}{{if eq .metadata.labels.app "myapp"}}{{.metadata.name}} {{end}}{{end}}"
Run Code Online (Sandbox Code Playgroud)
它给出了一个错误:
输出:1:未定义函数“ myapp”
结构如下所示:
- apiVersion: v1
kind: Pod
metadata:
creationTimestamp: 2017-09-15T08:18:26Z
generateName: myapp-2830765207-
labels:
app: myapp
pod-template-hash: "2830765207"
name: myapp-2830765207-dh359
namespace: default
Run Code Online (Sandbox Code Playgroud) 我想在范围循环中比较字符串类型的两个变量,如下所示:
<select name="category" id="category">
{{range $c := .cats}}
<option value="{{$c.Title}}" {{ if eq $c.Title .category}}active{{end}}>{{$c.Title}}</option>
{{end}}
</select>
Run Code Online (Sandbox Code Playgroud)
both $c.Title和category都是控制器调度的字符串.
但是,相反,在渲染模板中的下拉菜单中,我得到:
无法在model.category类型中评估字段类别
$c 是结构类型类别:
type Category struct {
ID int `db:"id"`
Title string `db:"title"`
Slug string `db:"slug"`
CreatedAt time.Time `db:"created_at"`
}
Run Code Online (Sandbox Code Playgroud)
当我直接放入字符串值category而不是.category上面的代码时,没有问题.
我正在使用 gowebapp MVC框架,如果它确实重要.
我怎样才能解决这个问题?
go-templates ×10
go ×8
file ×1
forms ×1
go-gin ×1
html ×1
kubernetes ×1
nested ×1
string ×1
templates ×1