英文:
Looping slice value in HTML file
问题
我已经创建了一个结构体的切片和一个切片的数组。
type blogs struct {
id int
title string
featured_image string
created_at string
}
在"xyz"函数中创建了变量:
blog := blogs{}
blogData := []blogs{}
赋值如下:
rows, err := db.Query("SELECT id, title, featured_image, created_at from blogs order by created_at desc limit 0,6")
if err != nil {
ctx.Application().Logger().Fatalf("MySQL Error fetching row %s\n", err)
}
for rows.Next() {
rcan := rows.Scan(&id, &title, &featured_image, &created_at)
blog.id = id
blog.title = title
blog.featured_image = featured_image
blog.created_at = created_at
blogData = append(blogData, blog)
}
现在,我将"blogData"的值传递给"html"模板,并且在下面的"iterating"中出现错误:
<ul>
{{ range $value := .blogData }}
<li>{{$value.title}}</li>
{{ end }}
</ul>
错误信息:
template: master.html:18:5: executing "master.html" at <yield>: error calling yield: template: home.html:5:17: executing "home.html" at <$value.title>: title is an unexported field of struct type main.blogs
我该如何在模板中打印"title"和其他"blogData"变量的值?
如果我打印"$value",它会以以下格式返回所有的值:
{5 This is Title img/blog.jpg 2017-07-05T10:11:30+05:30 }
但我想分别打印"title"、"featured_image"和其他日期。
非常感谢您的帮助。我正在使用"github.com/get-ion/ion"框架。
谢谢。
英文:
I have created slice of struct and an array of slice.
type blogs struct {
id int
title string
featured_image string
created_at string
}
and created variable inside "xyz" function :
blog := blogs{}
blogData := []blogs{}
with value as :
rows, err := db.Query("SELECT id, title, featured_image, created_at from blogs order by created_at desc limit 0,6")
if err != nil {
ctx.Application().Logger().Fatalf("MySQL Error fetching row %s\n", err)
}
for rows.Next() {
rcan := rows.Scan(&id, &title, &featured_image, &created_at)
blog.id = id
blog.title = title
blog.featured_image = featured_image
blog.created_at = created_at
blogData = append(blogData, blog)
}
and now, I have passed "blogData" value to "html" template and iterating
below gives error :
<ul>
{{ range $value := .blogData }}
<li>{{ $value.title }}</li>
{{ end }}
</ul>
Error :
template: master.html:18:5: executing "master.html" at <yield>: error calling yield: template: home.html:5:17: executing "home.html" at <$value.title>: title is an unexported field of struct type main.blogs
How can I print "title" and other value from blogData
Variable in my template.
if I print $value
, it returns all value in below format
{5 This is Title img/blog.jpg 2017-07-05T10:11:30+05:30 }
But I want to print, title
, featured_image
and other date separately.
Any help would be appreciated. I am using "github.com/get-ion/ion" framework
Thanks
答案1
得分: 1
要在模板上访问结构字段,它必须是导出字段。请将您的结构更新为:
type blogs struct {
ID int
Title string
Featured_image string
Created_at string
}
了解更多关于Go中的导出/未导出标识符的信息。
英文:
To access your struct field on template, it must be exported fields. Update your struct to-
type blogs struct {
ID int
Title string
Featured_image string
Created_at string
}
Read more about Exported/Unexported Identifiers In Go.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论