content,internal/godoc/dochtml: add declaration.tmpl

Logic for rendering code snippets from the output of render_decl is
moved to a helper template.

The </br> tag is removed for the Constants and Variables sections:
https://photos.app.goo.gl/FosXd66T3nfufaXf7

For golang/go#40939
For golang/go#42486

Change-Id: Icfe61be5f164e0be54e5357602477fc9ed24b05d
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/277632
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Trust: Julie Qiu <julie@golang.org>
diff --git a/content/static/html/doc/body.tmpl b/content/static/html/doc/body.tmpl
index 02f3ae6..c31841b 100644
--- a/content/static/html/doc/body.tmpl
+++ b/content/static/html/doc/body.tmpl
@@ -61,11 +61,7 @@
   <section class="Documentation-constants">
   {{- if .Consts -}}
     {{- range .Consts -}}
-      {{- $out := render_decl .Doc .Decl -}}
-      {{- $out.Decl -}}
-      {{- $out.Doc -}}
-      <br/>
-      {{"\n"}}
+      {{- template "declaration" . -}}
     {{- end -}}
   {{- else -}}
       <div class="Documentation-empty">This section is empty.</div>
@@ -76,11 +72,7 @@
   <section class="Documentation-variables">
   {{- if .Vars -}}
     {{- range .Vars -}}
-      {{- $out := render_decl .Doc .Decl -}}
-      {{- $out.Decl -}}
-      {{- $out.Doc -}}
-      <br/>
-      {{"\n"}}
+      {{- template "declaration" . -}}
     {{- end -}}
   {{- else -}}
     <div class="Documentation-empty">This section is empty.</div>
@@ -94,10 +86,7 @@
         <div class="Documentation-function">
             {{- $id := safe_id .Name -}}
             <h4 tabindex="-1" id="{{$id}}" data-kind="function" class="Documentation-functionHeader">func {{source_link .Name .Decl}} <a class="Documentation-idLink" href="#{{$id}}">¶</a></h4>{{"\n"}}
-            {{- $out := render_decl .Doc .Decl -}}
-            {{- $out.Decl -}}
-            {{- $out.Doc -}}
-            {{"\n"}}
+            {{- template "declaration" . -}}
             {{- template "example" (index $.Examples.Map .Name) -}}
         </div>
         {{- end -}}
@@ -114,27 +103,18 @@
       {{- $tname := .Name -}}
       {{- $id := safe_id .Name -}}
       <h4 tabindex="-1" id="{{$id}}" data-kind="type" class="Documentation-typeHeader">type {{source_link .Name .Decl}} <a class="Documentation-idLink" href="#{{$id}}">¶</a></h4>{{"\n"}}
-      {{- $out := render_decl .Doc .Decl -}}
-      {{- $out.Decl -}}
-      {{- $out.Doc -}}
-      {{"\n"}}
+      {{- template "declaration" . -}}
       {{- template "example" (index $.Examples.Map .Name) -}}
 
       {{- range .Consts -}}
       <div class="Documentation-typeConstant">
-        {{- $out := render_decl .Doc .Decl -}}
-        {{- $out.Decl -}}
-        {{- $out.Doc -}}
-        {{"\n"}}
+        {{- template "declaration" . -}}
       </div>
       {{- end -}}
 
       {{- range .Vars -}}
       <div class="Documentation-typeVariable">
-        {{- $out := render_decl .Doc .Decl -}}
-        {{- $out.Decl -}}
-        {{- $out.Doc -}}
-        {{"\n"}}
+        {{- template "declaration" . -}}
       </div>
       {{- end -}}
 
@@ -142,10 +122,7 @@
       <div class="Documentation-typeFunc">
         {{- $id := safe_id .Name -}}
         <h4 tabindex="-1" id="{{$id}}" data-kind="function" class="Documentation-typeFuncHeader">func {{source_link .Name .Decl}} <a class="Documentation-idLink" href="#{{$id}}">¶</a></h4>{{"\n"}}
-        {{- $out := render_decl .Doc .Decl -}}
-        {{- $out.Decl -}}
-        {{- $out.Doc -}}
-        {{"\n"}}
+        {{- template "declaration" . -}}
         {{- template "example" (index $.Examples.Map .Name) -}}
       </div>
       {{- end -}}
@@ -155,10 +132,7 @@
         {{- $name := (printf "%s.%s" $tname .Name) -}}
         {{- $id := (safe_id $name) -}}
         <h4 tabindex="-1" id="{{$id}}" data-kind="method" class="Documentation-typeMethodHeader">func ({{.Recv}}) {{source_link .Name .Decl}} <a class="Documentation-idLink" href="#{{$id}}">¶</a></h4>{{"\n"}}
-        {{- $out := render_decl .Doc .Decl -}}
-        {{- $out.Decl -}}
-        {{- $out.Doc -}}
-        {{"\n"}}
+        {{- template "declaration" . -}}
         {{- template "example" (index $.Examples.Map $name) -}}
       </div>
       {{- end -}}
diff --git a/content/static/html/doc/declaration.tmpl b/content/static/html/doc/declaration.tmpl
new file mode 100644
index 0000000..b24d017
--- /dev/null
+++ b/content/static/html/doc/declaration.tmpl
@@ -0,0 +1,12 @@
+<!--
+  Copyright 2020 The Go Authors. All rights reserved.
+  Use of this source code is governed by a BSD-style
+  license that can be found in the LICENSE file.
+-->
+
+{{- define "declaration" -}}
+  {{- $out := render_decl .Doc .Decl -}}
+  {{- $out.Decl -}}
+  {{- $out.Doc -}}
+  {{"\n"}}
+{{- end -}}
diff --git a/internal/godoc/dochtml/template.go b/internal/godoc/dochtml/template.go
index 6a9b140..18c0cbe 100644
--- a/internal/godoc/dochtml/template.go
+++ b/internal/godoc/dochtml/template.go
@@ -28,6 +28,7 @@
 		tc := template.TrustedSourceFromConstant
 
 		example := join(dir, tc("example.tmpl"))
+		declaration := join(dir, tc("declaration.tmpl"))
 		unitTemplate = template.Must(template.New("unit.tmpl").
 			Funcs(tmpl).
 			ParseFilesFromTrustedSources(
@@ -36,7 +37,8 @@
 				join(dir, tc("sidenav.tmpl")),
 				join(dir, tc("sidenav-mobile.tmpl")),
 				join(dir, tc("body.tmpl")),
-				example))
+				example,
+				declaration))
 	})
 }