From be1bb5fcf5a773016a0489efcac1460fe7134f33 Mon Sep 17 00:00:00 2001 From: Ryan Watters Date: Sat, 18 Feb 2017 02:16:50 -0600 Subject: [PATCH] Make further improvements to Go Template Primer --- content/templates/go-template-primer.md | 99 +++++++++++-------------- 1 file changed, 43 insertions(+), 56 deletions(-) diff --git a/content/templates/go-template-primer.md b/content/templates/go-template-primer.md index d7273e3ec..0961aafd9 100644 --- a/content/templates/go-template-primer.md +++ b/content/templates/go-template-primer.md @@ -28,8 +28,7 @@ A unique characteristic of Go templates is they are content aware. Variables and ## Basic Syntax -Golang templates are HTML files with the addition of [variables][variablesparams] and -[functions][]. +Golang templates are HTML files with the addition of [variables][variablesparams] and [functions][hugofunctions]. **Go variables and functions are accessible within {{ }}** @@ -81,16 +80,15 @@ Variables can also be defined and referenced. ## Functions -Go template ships with a few functions which provide basic functionality. The Go template system also provides a mechanism for applications to extend the -available functions with their own. [Hugo template functions][] provide some additional functionality we believe are useful for building websites. Functions are called by using their name followed by the required parameters separated by spaces. Template functions cannot be added without recompiling Hugo. +Go template ships with a few functions which provide basic functionality. The Go template system also provides a mechanism for applications to extend the available functions with their own. [Hugo template functions][hugofunctions] provide some additional functionality we believe are useful for building websites. Functions are called by using their name followed by the required parameters separated by spaces. Template functions cannot be added without recompiling Hugo. -**Example 1: Adding numbers** +### Example 1: Adding Numbers ```golang {{ add 1 2 }} ``` -**Example 2: Comparing numbers** +### Example 2: Comparing Numbers ```golang {{ lt 1 2 }} @@ -107,15 +105,18 @@ able to access. To pass along the current context, please remember to include a trailing dot. The templates location will always be starting at the /layout/ directory within Hugo. -**Example:** +### Template and Partial Examples - {{ template "partials/header.html" . }} +```golang +{{ template "partials/header.html" . }} +``` And, starting with Hugo v0.12, you may also use the `partial` call -for [partial templates](/templates/partials/): - - {{ partial "header.html" . }} +for [partial templates][]: +```golang +{{ partial "header.html" . }} +``` ## Logic @@ -248,7 +249,7 @@ Could be rewritten as Stuff Here {{ end }} -### Internet Explorer conditional comments using Pipes +### Internet Explorer Conditional Comments By default, Go Templates remove HTML comments from output. This has the unfortunate side effect of removing Internet Explorer conditional comments. As a workaround, use something like this: @@ -262,25 +263,22 @@ Alternatively, use the backtick (`` ` ``) to quote the IE conditional comments, {{ `` | safeHTML }} ``` -## Context (a.k.a. the dot) +## Context (aka "the dot") -The most easily overlooked concept to understand about Go templates is that `{{ . }}` -always refers to the current context. In the top level of your template, this -will be the data set made available to it. Inside of a iteration, however, it will have -the value of the current item. When inside of a loop, the context has changed: -`{{ . }}` will no longer refer to the data available to the entire page. If you need -to -access this from within the loop, you will likely want to do one of the following: +The most easily overlooked concept to understand about Go templates is that `{{ . }}` always refers to the current context. In the top level of your template, this will be the data set made available to it. Inside of a iteration, however, it will have the value of the current item. When inside of a loop, the context has changed: `{{ . }}` will no longer refer to the data available to the entire page. If you need to access this from within the loop, you will likely want to do one of the following: -1. Set it to a variable instead of depending on the context. For example: +### Define Variable Independent of Context + +variable instead of depending on the context. For example: {{% input "range-through-tags-w-variable.html" %}} ```html {{ $title := .Site.Title }} +{{ $base := .Site.BaseURL }}