Golang从文件中嵌入HTML

huangapple go评论72阅读模式
英文:

Golang embed html from file

问题

如果你想在 Golang 中处理类似以下的 HTML 文件:

<html>
  <head lang="en">
    
  </head>
  <body>
    <header>{{.Header}}</header>
    <div class="panel panel-default">
        
    </div>
  </body>
</html>

并且你想要将另一个文件中的一部分代码嵌入到 <header> 标签中,像这样:

<div id="logo"></div><div id="motto"></div>

你可以尝试以下方法:

header, _ := template.ParseFiles("header.html")
c := Content{Header: ""}
header.ExecuteTemplate(c.Header, "header.html", nil)

index := template.Must(template.ParseFiles("index.html"))
index.Execute(w, c)

请注意,上述代码中的 Content 结构体是你自己定义的,用于传递数据给模板。确保在代码中正确定义和使用该结构体。

英文:

How can I do in Golang if I have an HTML file like this:

&lt;html&gt;
  &lt;head lang=&quot;en&quot;&gt;
    
  &lt;/head&gt;
  &lt;body&gt;
    &lt;header&gt;{{.Header}}&lt;/header&gt;
    &lt;div class=&quot;panel panel-default&quot;&gt;
        
    &lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;

and I want to embed a part of code into to header tags from an other file like this:

&lt;div id=&quot;logo&quot;&gt;&lt;/div&gt;&lt;div id=&quot;motto&quot;&gt;&lt;/div&gt;

My try:

header, _ := template.ParseFiles(&quot;header.html&quot;)
c := Content{Header: &quot;&quot;}
header.Execute(c.Header, nil)

index := template.Must(template.ParseFiles(&quot;index.html&quot;))
index.Execute(w, c)

答案1

得分: 13

如果你使用template.ParseFiles()或者template.ParseGlob()来解析所有的模板文件,那么这些模板可以相互引用,可以包含彼此。

将你的index.html文件修改为以下形式,包含header.html

<html>
  <head lang="en">
  
  </head>
  <body>
    <header>{{template "header.html"}}</header>
    <div class="panel panel-default">
  
    </div>
  </body>
</html>

然后完整的程序代码(从当前目录解析文件,执行"index.html"并将结果写入标准输出)如下:

t, err := template.ParseFiles("index.html", "header.html")
if err != nil {
    panic(err)
}

err = t.ExecuteTemplate(os.Stdout, "index.html", nil)
if err != nil {
    panic(err)
}

如果使用template.ParseGlob(),代码如下:

t, err := template.ParseGlob("*.html")
// ...其余部分相同...

输出结果(在控制台打印)如下:

<html>
  <head lang="en">
  
  </head>
  <body>
    <header><div id="logo"></div><div id="motto"></div></header>
    <div class="panel panel-default">
  
    </div>
  </body>
</html>
英文:

If you parse all your template files with template.ParseFiles() or with template.ParseGlob(), the templates can refer to each other, they can include each other.

Change your index.html to include the header.html like this:

&lt;html&gt;
  &lt;head lang=&quot;en&quot;&gt;

  &lt;/head&gt;
  &lt;body&gt;
    &lt;header&gt;{{template &quot;header.html&quot;}}&lt;/header&gt;
    &lt;div class=&quot;panel panel-default&quot;&gt;

    &lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;

And then the complete program (which parses files from the current directory, executes &quot;index.html&quot; and writes the result to the standard output):

t, err := template.ParseFiles(&quot;index.html&quot;, &quot;header.html&quot;)
if err != nil {
	panic(err)
}

err = t.ExecuteTemplate(os.Stdout, &quot;index.html&quot;, nil)
if err != nil {
	panic(err)
}

With template.ParseGlob() it could look like this:

t, err := template.ParseGlob(&quot;*.html&quot;)
// ...and the rest is the same...

The output (printed on the console):

&lt;html&gt;
  &lt;head lang=&quot;en&quot;&gt;

  &lt;/head&gt;
  &lt;body&gt;
    &lt;header&gt;&lt;div id=&quot;logo&quot;&gt;&lt;/div&gt;&lt;div id=&quot;motto&quot;&gt;&lt;/div&gt;&lt;/header&gt;
    &lt;div class=&quot;panel panel-default&quot;&gt;

    &lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;

huangapple
  • 本文由 发表于 2015年11月29日 22:56:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/33984147.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定