在模板中使用条件语句

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

Using conditions inside templates

问题

在模板中使用if语句确实让我感到困惑。

我正在尝试在使用golang模板创建的导航列表中添加class = "active",以实现一个基本的选项卡菜单,用于检测活动选项卡。
以下是我的尝试:

{{define "header"}}
<!DOCTYPE html>
<html>
	<head>
		<title>Geoprod</title>
		{{template "stylesheet" .}}
	</head>
	<body>
	    <nav class="navbar" role="navigation">
	      <div class="navbar-header">
	        <a{{if eq .Active "accueil"}} class="active"{{end}} href="/">Geoprod</a>
	      </div>
	      <div class="navbar-body">
	        <ul class="navbar-list">
				<li{{if eq .Active "societe"}} class="active"{{end}}><a href="/societe">Société</a></li>
	        	<li{{if eq .Active "dossier"}} class="active"{{end}}><a href="/dossier">Dossier</a></li>
				<li{{if eq .Active "temps"}} class="active"{{end}}><a href="/temps">Temps</a></li>
				<li{{if eq .Active "mails"}} class="active"{{end}}><a href="/mails">Mails</a></li>
	        </ul>
	      </div>
	    </nav>
{{end}}

在main.go中:

var FuncMap = template.FuncMap{
	"eq": func(a, b interface{}) bool {
		return a == b
	},
}
var templates = template.Must(template.ParseGlob("templates/*.html"))

func main()中:

templates.Funcs(FuncMap)

程序可以编译,但我发现添加{{if eq .Active "something"}} class="active"{{end}}(如上所示)会导致程序不再显示任何文本。有任何想法是为什么吗?

英文:

The use of if statements inside templates really is puzzling me.

I'm trying to put a class = &quot;active&quot; inside a nav list made with golang templates, to do a basic tab menu that detects the active tab.
Here's my attempt :

{{define &quot;header&quot;}}
&lt;!DOCTYPE html&gt;
&lt;html&gt;
	&lt;head&gt;
		&lt;title&gt;Geoprod&lt;/title&gt;
		{{template &quot;stylesheet&quot; .}}
	&lt;/head&gt;
	&lt;body&gt;
	    &lt;nav class=&quot;navbar&quot; role=&quot;navigation&quot;&gt;
	      &lt;div class=&quot;navbar-header&quot;&gt;
	        &lt;a{{if eq .Active &quot;accueil&quot;}} class=&quot;active&quot;{{end}} href=&quot;/&quot;&gt;Geoprod&lt;/a&gt;
	      &lt;/div&gt;
	      &lt;div class=&quot;navbar-body&quot;&gt;
	        &lt;ul class=&quot;navbar-list&quot;&gt;
				&lt;li{{if eq .Active &quot;societe&quot;}} class=&quot;active&quot;{{end}}&gt;&lt;a href=&quot;/societe&quot;&gt;Soci&amp;eacutet&amp;eacute&lt;/a&gt;&lt;/li&gt;
	        	&lt;li{{if eq .Active &quot;dossier&quot;}} class=&quot;active&quot;{{end}}&gt;&lt;a href=&quot;/dossier&quot;&gt;Dossier&lt;/a&gt;&lt;/li&gt;
				&lt;li{{if eq .Active &quot;temps&quot;}} class=&quot;active&quot;{{end}}&gt;&lt;a href=&quot;/temps&quot;&gt;Temps&lt;/a&gt;&lt;/li&gt;
				&lt;li{{if eq .Active &quot;mails&quot;}} class=&quot;active&quot;{{end}}&gt;&lt;a href=&quot;/mails&quot;&gt;Mails&lt;/a&gt;&lt;/li&gt;
	        &lt;/ul&gt;
	      &lt;/div&gt;
	    &lt;/nav&gt;
{{end}}

And in main.go :

var FuncMap = template.FuncMap{
	&quot;eq&quot;: func(a, b interface{}) bool {
		return a == b
	},
}
var templates = template.Must(template.ParseGlob(&quot;templates/*.html&quot;))

and in func main()

templates.Funcs(FuncMap)

The program compiles, but i've found out adding the {{if eq .Active &quot;something&quot;}} class=&quot;active&quot;{{end}} (^^ which I included here) causes the program to not display any text anymore. Any idea why?

答案1

得分: 5

我尝试将你的代码转换为一个最小工作示例,并且我相信你的代码和模板按预期工作。你可以在Go Playground上查看我的代码(并运行它)。

我对出错原因的猜测是:你是否注意到{{define ...}}只是定义了一个模板以供将来使用。你仍然需要告诉Go实际使用这个模板,可以通过在主模板中使用{{ template "header" }}或类似的方式,或者使用templates.ExecuteTemplate来实现。

英文:

I tried to convert your code into a minimal working example, and I believe your code and template works as expected. You can see my code (and run it) on the Go Playground.

My guess about what went wrong: Did you notice that {{define ...}} only defines a template for future use. You will still need to tell Go to actually use this template, either by using {{ template &quot;header&quot; }} or similar in a main template, or by using templates.ExecuteTemplate.

huangapple
  • 本文由 发表于 2014年7月15日 18:12:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/24755509.html
匿名

发表评论

匿名网友

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

确定