如何在Go中直接将SVG作为HTML部分包含进去

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

How to directly include SVG as a html partial in Go

问题

我想将svg文件作为html部分包含在内,以便可以在我的HTML中内联使用它。
现在我将我的svg包装在html文件中,并像这样包含它们:

{{ template "/partial/my-svg.html" }}

但是我想直接包含我的svg。PHP可以这样做:

<?php echo file_get_contents("/partial/my-svg.svg"); ?> 

我不认为Go有类似的功能?所以我想我需要通过自定义函数来扩展模板逻辑。类似于:

{{ includeSvg "/partial/my-svg.svg" }}

在Go中,这样的函数会是什么样子?

英文:

I want to include svg files as a html partial so I can use it inline in my HTML.
I now I have my svg wrapped in html files and include them like this:

{{ template "/partial/my-svg.html" }}

But I want to include my svg's directly. PHP does this like so:

<?php echo file_get_contents("/partial/my-svg.svg"); ?> 

I don't think go has something similar? So I think I need to extend the template logic with a custom function. Something like:

{{ includeSvg "/partial/my-svg.svg" }}

How would such a function look like in go?

答案1

得分: 2

这是一个工作示例,演示如何添加一个模板函数并在模板中通过路径包含一个 SVG。

package main

import (
	"html/template"
	"io/ioutil"
	"log"
	"net/http"
)

func IncludeHTML(path string) template.HTML {
	b, err := ioutil.ReadFile(path)
	if err != nil {
		log.Println("includeHTML - error reading file: %v", err)
		return ""
	}

	return template.HTML(string(b))
}

func main() {
	tmpl := template.New("sample")
	tmpl.Funcs(template.FuncMap{
		"IncludeHTML": IncludeHTML,
	})

	tmpl, err := tmpl.Parse(`
	<!DOCTYPE>
	<html>
	<body>
		<h1>Check out this svg</h1>
		{{ IncludeHTML "/path/to/svg" }}
	</body>
	</html>
	`)
	if err != nil {
		log.Fatal(err)
	}

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "text/html")
		if err := tmpl.Execute(w, nil); err != nil {
			log.Println("Error executing template: %v", err)
		}
	})

	if err := http.ListenAndServe(":8000", nil); err != nil {
		log.Fatal(err)
	}
}

这段代码展示了如何创建一个名为sample的模板,并添加了一个名为IncludeHTML的模板函数。在模板中,使用{{ IncludeHTML "/path/to/svg" }}来包含一个 SVG 文件。然后,通过 HTTP 服务器将模板渲染为 HTML 并提供访问。

英文:

Here is a working example of how to add a template function and include an svg by path in a template

package main

import (
	&quot;html/template&quot;
	&quot;io/ioutil&quot;
	&quot;log&quot;
	&quot;net/http&quot;
)

func IncludeHTML(path string) template.HTML {
	b, err := ioutil.ReadFile(path)
	if err != nil {
		log.Println(&quot;includeHTML - error reading file: %v&quot;, err)
		return &quot;&quot;
	}

	return template.HTML(string(b))
}

func main() {
	tmpl := template.New(&quot;sample&quot;)
	tmpl.Funcs(template.FuncMap{
		&quot;IncludeHTML&quot;: IncludeHTML,
	})

	tmpl, err := tmpl.Parse(`
&lt;!DOCTYPE&gt;
&lt;html&gt;
&lt;body&gt;
	&lt;h1&gt;Check out this svg&lt;/h1&gt;
	{{ IncludeHTML &quot;/path/to/svg&quot; }}
&lt;/body&gt;
&lt;/html&gt;
	`)
	if err != nil {
		log.Fatal(err)
	}

	http.HandleFunc(&quot;/&quot;, func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set(&quot;Content-Type&quot;, &quot;text/html&quot;)
		if err := tmpl.Execute(w, nil); err != nil {
			log.Println(&quot;Error executing template: %v&quot;, err)
		}
	})

	if err := http.ListenAndServe(&quot;:8000&quot;, nil); err != nil {
		log.Fatal(err)
	}
}

huangapple
  • 本文由 发表于 2016年11月16日 21:41:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/40633669.html
匿名

发表评论

匿名网友

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

确定