hugo

Fork of github.com/gohugoio/hugo with reverse pagination support

git clone git://git.shimmy1996.com/hugo.git

partials.md (7721B)

    1 ---
    2 title: Partial Templates
    3 linktitle: Partial Templates
    4 description: Partials are smaller, context-aware components in your list and page templates that can be used economically to keep your templating DRY.
    5 date: 2017-02-01
    6 publishdate: 2017-02-01
    7 lastmod: 2017-02-01
    8 categories: [templates]
    9 keywords: [lists,sections,partials]
   10 menu:
   11   docs:
   12     parent: "templates"
   13     weight: 90
   14 weight: 90
   15 sections_weight: 90
   16 draft: false
   17 aliases: [/templates/partial/,/layout/chrome/,/extras/analytics/]
   18 toc: true
   19 ---
   20 
   21 {{< youtube pjS4pOLyB7c >}}
   22 
   23 ## Partial Template Lookup Order
   24 
   25 Partial templates---like [single page templates][singletemps] and [list page templates][listtemps]---have a specific [lookup order][]. However, partials are simpler in that Hugo will only check in two places:
   26 
   27 1. `layouts/partials/*<PARTIALNAME>.html`
   28 2. `themes/<THEME>/layouts/partials/*<PARTIALNAME>.html`
   29 
   30 This allows a theme's end user to copy a partial's contents into a file of the same name for [further customization][customize].
   31 
   32 ## Use Partials in your Templates
   33 
   34 All partials for your Hugo project are located in a single `layouts/partials` directory. For better organization, you can create multiple subdirectories within `partials` as well:
   35 
   36 ```txt
   37 layouts/
   38 └── partials/
   39     ├── footer/
   40     │   ├── scripts.html
   41     │   └── site-footer.html
   42     ├── head/
   43     │   ├── favicons.html
   44     │   ├── metadata.html
   45     │   ├── prerender.html
   46     │   └── twitter.html
   47     └── header/
   48         ├── site-header.html
   49         └── site-nav.html
   50 ```
   51 
   52 All partials are called within your templates using the following pattern:
   53 
   54 ```go-html-template
   55 {{ partial "<PATH>/<PARTIAL>.html" . }}
   56 ```
   57 
   58 {{% note %}}
   59 One of the most common mistakes with new Hugo users is failing to pass a context to the partial call. In the pattern above, note how "the dot" (`.`) is required as the second argument to give the partial context. You can read more about "the dot" in the [Hugo templating introduction](/templates/introduction/).
   60 {{% /note %}}
   61 
   62 {{% note %}}
   63 `<PARTIAL>` including `baseof` is reserved. ([#5373](https://github.com/gohugoio/hugo/issues/5373))
   64 {{% /note %}}
   65 
   66 As shown in the above example directory structure, you can nest your directories within `partials` for better source organization. You only need to call the nested partial's path relative to the `partials` directory:
   67 
   68 ```go-html-template
   69 {{ partial "header/site-header.html" . }}
   70 {{ partial "footer/scripts.html" . }}
   71 ```
   72 
   73 ### Variable Scoping
   74 
   75 The second argument in a partial call is the variable being passed down. The above examples are passing the `.`, which tells the template receiving the partial to apply the current [context][context].
   76 
   77 This means the partial will *only* be able to access those variables. The partial is isolated and *has no access to the outer scope*. From within the partial, `$.Var` is equivalent to `.Var`.
   78 
   79 ## Returning a value from a Partial
   80 
   81 In addition to outputting markup, partials can be used to return a value of any type. In order to return a value, a partial must include a lone `return` statement _at the end of the partial_.
   82 
   83 ### Example GetFeatured
   84 
   85 ```go-html-template
   86 {{/* layouts/partials/GetFeatured.html */}}
   87 {{ return first . (where site.RegularPages "Params.featured" true) }}
   88 ```
   89 
   90 ```go-html-template
   91 {{/* layouts/index.html */}}
   92 {{ range partial "GetFeatured.html" 5 }}
   93   [...]
   94 {{ end }}
   95 ```
   96 
   97 ### Example GetImage
   98 
   99 ```go-html-template
  100 {{/* layouts/partials/GetImage.html */}}
  101 {{ $image := false }}
  102 {{ with .Params.gallery }}
  103   {{ $image = index . 0 }}
  104 {{ end }}
  105 {{ with .Params.image }}
  106   {{ $image = . }}
  107 {{ end }}
  108 {{ return $image }}
  109 ```
  110 
  111 ```go-html-template
  112 {{/* layouts/_default/single.html */}}
  113 {{ with partial "GetImage.html" . }}
  114   [...]
  115 {{ end }}
  116 ```
  117 
  118 {{% note %}}
  119 Only one `return` statement is allowed per partial file.
  120 {{% /note %}}
  121 
  122 ## Inline Partials
  123 
  124 {{< new-in "0.74.0" >}}
  125 
  126 You can also define partials inline in the template. But remember that template namespace is global, so you need to make sure that the names are unique to avoid conflicts.
  127 
  128 ```go-html-template
  129 Value: {{ partial "my-inline-partial.html" . }}
  130 
  131 {{ define "partials/my-inline-partial.html" }}
  132 {{ $value := 32 }}
  133 {{ return $value }}
  134 {{ end }}
  135 ```
  136 
  137 ## Cached Partials
  138 
  139 The [`partialCached` template function][partialcached] can offer significant performance gains for complex templates that don't need to be re-rendered on every invocation. The simplest usage is as follows:
  140 
  141 ```go-html-template
  142 {{ partialCached "footer.html" . }}
  143 ```
  144 
  145 You can also pass additional parameters to `partialCached` to create *variants* of the cached partial.
  146 
  147 For example, you can tell Hugo to only render the partial `footer.html` once per section:
  148 
  149 ```go-html-template
  150 {{ partialCached "footer.html" . .Section }}
  151 ```
  152 
  153 If you need to pass additional parameters to create unique variants, you can pass as many variant parameters as you need:
  154 
  155 ```go-html-template
  156 {{ partialCached "footer.html" . .Params.country .Params.province }}
  157 ```
  158 
  159 Note that the variant parameters are not made available to the underlying partial template. They are only use to create a unique cache key.
  160 
  161 ### Example `header.html`
  162 
  163 The following `header.html` partial template is used for [spf13.com](https://spf13.com/):
  164 
  165 {{< code file="layouts/partials/header.html" download="header.html" >}}
  166 <!DOCTYPE html>
  167 <html class="no-js" lang="en-US" prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb#">
  168 <head>
  169     <meta charset="utf-8">
  170 
  171     {{ partial "meta.html" . }}
  172 
  173     <base href="{{ .Site.BaseURL }}">
  174     <title> {{ .Title }} : spf13.com </title>
  175     <link rel="canonical" href="{{ .Permalink }}">
  176     {{ if .RSSLink }}<link href="{{ .RSSLink }}" rel="alternate" type="application/rss+xml" title="{{ .Title }}" />{{ end }}
  177 
  178     {{ partial "head_includes.html" . }}
  179 </head>
  180 {{< /code >}}
  181 
  182 {{% note %}}
  183 The `header.html` example partial was built before the introduction of block templates to Hugo. Read more on [base templates and blocks](/templates/base/) for defining the outer chrome or shell of your master templates (i.e., your site's head, header, and footer). You can even combine blocks and partials for added flexibility.
  184 {{% /note %}}
  185 
  186 ### Example `footer.html`
  187 
  188 The following `footer.html` partial template is used for [spf13.com](https://spf13.com/):
  189 
  190 {{< code file="layouts/partials/footer.html" download="footer.html" >}}
  191 <footer>
  192   <div>
  193     <p>
  194     &copy; 2013-14 Steve Francia.
  195     <a href="https://creativecommons.org/licenses/by/3.0/" title="Creative Commons Attribution">Some rights reserved</a>;
  196     please attribute properly and link back.
  197     </p>
  198   </div>
  199 </footer>
  200 {{< /code >}}
  201 
  202 [context]: /templates/introduction/ "The most easily overlooked concept to understand about Go templating is how the dot always refers to the current context."
  203 [customize]: /themes/customizing/ "Hugo provides easy means to customize themes as long as users are familiar with Hugo's template lookup order."
  204 [listtemps]: /templates/lists/ "To effectively leverage Hugo's system, see how Hugo handles list pages, where content for sections, taxonomies, and the homepage are listed and ordered."
  205 [lookup order]: /templates/lookup-order/ "To keep your templating dry, read the documentation on Hugo's lookup order."
  206 [partialcached]: /functions/partialcached/ "Use the partial cached function to improve build times in cases where Hugo can cache partials that don't need to be rendered with every page."
  207 [singletemps]: /templates/single-page-templates/ "The most common form of template in Hugo is the single content template. Read the docs on how to create templates for individual pages."
  208 [themes]: /themes/