aboutsummaryrefslogtreecommitdiffhomepage
path: root/services/nuldoc/public/blog/posts/2024-08-19/go-template-access-outer-scope-pipeline-within-with-or-range
diff options
context:
space:
mode:
Diffstat (limited to 'services/nuldoc/public/blog/posts/2024-08-19/go-template-access-outer-scope-pipeline-within-with-or-range')
-rw-r--r--services/nuldoc/public/blog/posts/2024-08-19/go-template-access-outer-scope-pipeline-within-with-or-range/index.html87
1 files changed, 41 insertions, 46 deletions
diff --git a/services/nuldoc/public/blog/posts/2024-08-19/go-template-access-outer-scope-pipeline-within-with-or-range/index.html b/services/nuldoc/public/blog/posts/2024-08-19/go-template-access-outer-scope-pipeline-within-with-or-range/index.html
index 5bdb0cc9..951f1c81 100644
--- a/services/nuldoc/public/blog/posts/2024-08-19/go-template-access-outer-scope-pipeline-within-with-or-range/index.html
+++ b/services/nuldoc/public/blog/posts/2024-08-19/go-template-access-outer-scope-pipeline-within-with-or-range/index.html
@@ -15,7 +15,7 @@
<meta name="Hatena::Bookmark" content="nocomment">
<link rel="icon" type="image/svg+xml" href="/favicon.svg">
<title>【Go】 text/template の with や range の内側から外側の &quot;.&quot; にアクセスする|REPL: Rest-Eat-Program Loop</title>
- <link rel="stylesheet" href="/style.css?h=81dfc0b483beda175e3e17562faac7c9">
+ <link rel="stylesheet" href="/style.css?h=c3724e2d900f13528c82005e79a8ec23">
</head>
<body class="single">
<header class="header">
@@ -90,20 +90,19 @@
Go には、標準ライブラリにテンプレートライブラリ <code>text/template</code> がある。この <code>text/template</code> における制御構造、<code>with</code> と <code>range</code> は次のように使われる。
</p>
<div class="codeblock">
- <pre class="highlight" style="background-color:#f5f5f5"><code># {{ .Title }}
-
-# User
-
-{{ with .User }}
- {{ .Name }} ({{ .ID }})
-{{ end }}
-
-# Items
-
-{{ range .Items }}
- - {{ . }}
-{{ end }}
-</code></pre>
+ <pre class="highlight" style="background-color:#f5f5f5"><code><div class="codeblock-line"># {{ .Title }}
+</div><div class="codeblock-line">
+</div><div class="codeblock-line"># User
+</div><div class="codeblock-line">
+</div><div class="codeblock-line">{{ with .User }}
+</div><div class="codeblock-line"> {{ .Name }} ({{ .ID }})
+</div><div class="codeblock-line">{{ end }}
+</div><div class="codeblock-line">
+</div><div class="codeblock-line"># Items
+</div><div class="codeblock-line">
+</div><div class="codeblock-line">{{ range .Items }}
+</div><div class="codeblock-line"> - {{ . }}
+</div><div class="codeblock-line">{{ end }}</div></code></pre>
</div>
<p>
<code>text/template</code> の <code>.</code> は、現在の操作対象を表す特殊なオブジェクトである。
@@ -115,19 +114,18 @@
つまりこのテンプレートは、次のような構造をレンダリングしている (<code>Execute()</code> の第2引数)。
</p>
<div class="codeblock">
- <pre class="highlight" style="background-color:#f5f5f5"><code><span style="color: #24292f;background-color: #f6f8fa">tmpl</span><span style="color: #0550ae">.</span><span style="color: #24292f;background-color: #f6f8fa">Execute</span><span style="color: #24292f;background-color: #f6f8fa">(</span><span style="color: #24292f;background-color: #f6f8fa">out</span><span style="color: #24292f;background-color: #f6f8fa">,</span> <span style="color: #24292f;background-color: #f6f8fa">Params</span><span style="color: #24292f;background-color: #f6f8fa">{</span>
- <span style="color: #24292f;background-color: #f6f8fa">Title</span><span style="color: #0550ae">:</span> <span style="color: #0a3069">"foo"</span><span style="color: #24292f;background-color: #f6f8fa">,</span>
- <span style="color: #24292f;background-color: #f6f8fa">User</span><span style="color: #0550ae">:</span> <span style="color: #24292f;background-color: #f6f8fa">User</span><span style="color: #24292f;background-color: #f6f8fa">{</span>
- <span style="color: #24292f;background-color: #f6f8fa">ID</span><span style="color: #0550ae">:</span> <span style="color: #0550ae">123</span><span style="color: #24292f;background-color: #f6f8fa">,</span>
- <span style="color: #24292f;background-color: #f6f8fa">Name</span><span style="color: #0550ae">:</span> <span style="color: #0a3069">"john"</span><span style="color: #24292f;background-color: #f6f8fa">,</span>
- <span style="color: #24292f;background-color: #f6f8fa">},</span>
- <span style="color: #24292f;background-color: #f6f8fa">Items</span><span style="color: #0550ae">:</span> <span style="color: #24292f;background-color: #f6f8fa">[]</span><span style="color: #cf222e">string</span><span style="color: #24292f;background-color: #f6f8fa">{</span>
- <span style="color: #0a3069">"hoge"</span><span style="color: #24292f;background-color: #f6f8fa">,</span>
- <span style="color: #0a3069">"piyo"</span><span style="color: #24292f;background-color: #f6f8fa">,</span>
- <span style="color: #0a3069">"fuga"</span><span style="color: #24292f;background-color: #f6f8fa">,</span>
- <span style="color: #24292f;background-color: #f6f8fa">},</span>
-<span style="color: #24292f;background-color: #f6f8fa">})</span>
-</code></pre>
+ <pre class="highlight" style="background-color:#f5f5f5"><code><div class="codeblock-line"><span style="color: #24292f;background-color: #f6f8fa">tmpl</span><span style="color: #0550ae">.</span><span style="color: #24292f;background-color: #f6f8fa">Execute</span><span style="color: #24292f;background-color: #f6f8fa">(</span><span style="color: #24292f;background-color: #f6f8fa">out</span><span style="color: #24292f;background-color: #f6f8fa">,</span> <span style="color: #24292f;background-color: #f6f8fa">Params</span><span style="color: #24292f;background-color: #f6f8fa">{</span>
+</div><div class="codeblock-line"> <span style="color: #24292f;background-color: #f6f8fa">Title</span><span style="color: #0550ae">:</span> <span style="color: #0a3069">"foo"</span><span style="color: #24292f;background-color: #f6f8fa">,</span>
+</div><div class="codeblock-line"> <span style="color: #24292f;background-color: #f6f8fa">User</span><span style="color: #0550ae">:</span> <span style="color: #24292f;background-color: #f6f8fa">User</span><span style="color: #24292f;background-color: #f6f8fa">{</span>
+</div><div class="codeblock-line"> <span style="color: #24292f;background-color: #f6f8fa">ID</span><span style="color: #0550ae">:</span> <span style="color: #0550ae">123</span><span style="color: #24292f;background-color: #f6f8fa">,</span>
+</div><div class="codeblock-line"> <span style="color: #24292f;background-color: #f6f8fa">Name</span><span style="color: #0550ae">:</span> <span style="color: #0a3069">"john"</span><span style="color: #24292f;background-color: #f6f8fa">,</span>
+</div><div class="codeblock-line"> <span style="color: #24292f;background-color: #f6f8fa">},</span>
+</div><div class="codeblock-line"> <span style="color: #24292f;background-color: #f6f8fa">Items</span><span style="color: #0550ae">:</span> <span style="color: #24292f;background-color: #f6f8fa">[]</span><span style="color: #cf222e">string</span><span style="color: #24292f;background-color: #f6f8fa">{</span>
+</div><div class="codeblock-line"> <span style="color: #0a3069">"hoge"</span><span style="color: #24292f;background-color: #f6f8fa">,</span>
+</div><div class="codeblock-line"> <span style="color: #0a3069">"piyo"</span><span style="color: #24292f;background-color: #f6f8fa">,</span>
+</div><div class="codeblock-line"> <span style="color: #0a3069">"fuga"</span><span style="color: #24292f;background-color: #f6f8fa">,</span>
+</div><div class="codeblock-line"> <span style="color: #24292f;background-color: #f6f8fa">},</span>
+</div><div class="codeblock-line"><span style="color: #24292f;background-color: #f6f8fa">})</span></div></code></pre>
</div>
</section>
<section id="section--what-i-want-to-do">
@@ -136,14 +134,13 @@
今回おこないたいのは、<code>with</code> や <code>range</code> の中で、その外側で使われていたトップレベルのオブジェクトを参照することだ。
</p>
<div class="codeblock">
- <pre class="highlight" style="background-color:#f5f5f5"><code>{{ with .User }}
- ここから .Title を参照するには?
-{{ end }}
-
-{{ range .Items }}
- ここから .User を参照するには?
-{{ end }}
-</code></pre>
+ <pre class="highlight" style="background-color:#f5f5f5"><code><div class="codeblock-line">{{ with .User }}
+</div><div class="codeblock-line"> ここから .Title を参照するには?
+</div><div class="codeblock-line">{{ end }}
+</div><div class="codeblock-line">
+</div><div class="codeblock-line">{{ range .Items }}
+</div><div class="codeblock-line"> ここから .User を参照するには?
+</div><div class="codeblock-line">{{ end }}</div></code></pre>
</div>
<p>
<code>with</code> や <code>range</code> は、<code>.</code> を自身の対象オブジェクトに変更するので、単に <code>{{ with .User }}</code> の中で <code>.Title</code> と書いても、それは <code>User</code> の <code>Title</code> プロパティを参照しているとみなされる。
@@ -152,8 +149,7 @@
<code>text/template</code> では変数が使えるので、テンプレートの先頭で
</p>
<div class="codeblock">
- <pre class="highlight" style="background-color:#f5f5f5"><code>{{ $params := . }}
-</code></pre>
+ <pre class="highlight" style="background-color:#f5f5f5"><code><div class="codeblock-line">{{ $params := . }}</div></code></pre>
</div>
<p>
とでもしておけば実現は可能である。
@@ -168,14 +164,13 @@
常にトップレベルを指す特殊変数 <code>$</code> を使えばよい。
</p>
<div class="codeblock">
- <pre class="highlight" style="background-color:#f5f5f5"><code>{{ with .User }}
- {{ $.Title }}
-{{ end }}
-
-{{ range .Items }}
- {{ $.User.Name }}
-{{ end }}
-</code></pre>
+ <pre class="highlight" style="background-color:#f5f5f5"><code><div class="codeblock-line">{{ with .User }}
+</div><div class="codeblock-line"> {{ $.Title }}
+</div><div class="codeblock-line">{{ end }}
+</div><div class="codeblock-line">
+</div><div class="codeblock-line">{{ range .Items }}
+</div><div class="codeblock-line"> {{ $.User.Name }}
+</div><div class="codeblock-line">{{ end }}</div></code></pre>
</div>
<p>
<code>$</code> は、テンプレートが実行されるときに渡されたオブジェクトを指す。これを使えば現在の <code>.</code> に関係なくトップレベルを参照できる。