我正在尝试创建一些模板,但我无法理解接下来的事情: 为什么这样的 build 行不通? 我有 test.go 文件:
package main
import (
"net/http"
"html/template"
"fmt"
)
func main() {
http.HandleFunc("/test.html", TestHandler)
http.ListenAndServe(":8080", nil)
}
func TestHandler(w http.ResponseWriter, r *http.Request) {
//Parsing HTML
t, err := template.ParseFiles("test.html")
if err != nil {
fmt.Println(err)
}
Name := "MyName"
City := "MyCity"
t.ExecuteTemplate(w, "T1", Name)
t.ExecuteTemplate(w, "T2", City)
}
我还有 test.html:
<html>
<head>
<title>Tamplates</title>
</head>
<body>
<h1>Testing some templates</h1>
<p> Empty template: {{ `"Some text"` }} </p>
<p> Name is {{ template "T1" . }} </p>
<p> City is {{ template "T2" . }} </p>
</body>
</html>
这是个简单的例子,但不知为什么它不起作用。
我尝试添加字符串 {{ define "T1"}} {{ . }} {{ end }} 和 {{ 定义“T2”}} {{ . }} {{ end }}在 h1 标题之前,但结果网页只包含字符串 MyName MyCity
最佳答案
更新 (07/02/2016)
我将尽我所能解释为什么您的代码没有正确执行,以及使用嵌套模板的正确解决方案。
在您的 test.html 文件中,您导入了两个模板:T1 和 T2,
<p> Name is {{ template "T1" . }} </p>
<p> City is {{ template "T2" . }} </p>
这些模板应该被定义并写入某个地方以供使用(一个好的写入位置可以在单独的文件中)。这是我发现的第一个问题,您是这样定义它们的:
{{ define "T1" }} {{ . }} {{ end }}
{{ define "T2" }} {{ . }} {{ end }}
请注意,{{.}} 是当前对象的简写,因此它将呈现您在调用 ExecuteTemplate 函数时传递的任何数据。因此,要解决这个问题,您应该在每个模板中指定要渲染的对象:
{{ define "T1" }} {{ .Name }} {{ end }}
{{ define "T2" }} {{ .City }} {{ end }}
现在,这是我发现的第二个问题。在您的 TestHandler 函数中,您只是渲染子模板,首先是 T1 模板,然后是 T2 模板:
t.ExecuteTemplate(w, "T1", Name)
t.ExecuteTemplate(w, "T2", City)
因此,您绝不会调用父模板。
您可以在下面找到使用嵌套模板的外观。希望能解决您对模板使用的疑惑。
test.go文件
package main
import (
"fmt"
"html/template"
"net/http"
)
func main() {
http.HandleFunc("/test.html", TestHandler)
http.ListenAndServe(":8080", nil)
}
func TestHandler(w http.ResponseWriter, r *http.Request) {
//Parsing HTML
t, err := template.ParseFiles("test2.html", "t1.tmpl", "t2.tmpl")
if err != nil {
fmt.Println(err)
}
items := struct {
Name string
City string
}{
Name: "MyName",
City: "MyCity",
}
t.Execute(w, items)
}
test.html文件
<html>
<head>
<title>Templates</title>
</head>
<body>
<h1>Testing some templates</h1>
<p> Empty template: {{ `"Some text"` }} </p>
<p> Name is {{ template "T1" . }} </p>
<p> City is {{ template "T2" . }} </p>
</body>
</html>
t1.tmpl 文件
{{ define "T1" }} {{ .Name }} {{ end }}
t2.tmpl 文件
{{ define "T2" }} {{ .City }} {{ end }}
不使用子模板:
我对您的代码进行了一些更改,使其能够正常工作。我只是将结构集合中的“名称”和“城市”变量发送到 Execute方法。检查以下内容:
package main
import (
"fmt"
"html/template"
"net/http"
)
func main() {
http.HandleFunc("/test.html", TestHandler)
http.ListenAndServe(":8080", nil)
}
func TestHandler(w http.ResponseWriter, r *http.Request) {
//Parsing HTML
t, err := template.ParseFiles("test.html")
if err != nil {
fmt.Println(err)
}
items := struct {
Name string
City string
}{
Name: "MyName",
City: "MyCity",
}
t.Execute(w, items)
}
并使用 {{.Name}} 和 {{.City}} 访问那些导出的字段。
<html>
<head>
<title>Tamplates</title>
</head>
<body>
<h1>Testing some templates</h1>
<p> Name is {{.Name}} </p>
<p> City is {{.City}} </p>
</body>
</html>
关于html - 无法在golang中很好地执行模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35243493/
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳
在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这
我遵循了教程http://gettingstartedwithchef.com/,第1章。我的运行list是"run_list":["recipe[apt]","recipe[phpap]"]我的phpapRecipe默认Recipeinclude_recipe"apache2"include_recipe"build-essential"include_recipe"openssl"include_recipe"mysql::client"include_recipe"mysql::server"include_recipe"php"include_recipe"php::modul