2014-05-27 22:32:57 +00:00
|
|
|
|
---
|
2014-05-29 22:42:05 +00:00
|
|
|
|
aliases:
|
|
|
|
|
- /templates/views/
|
2016-01-06 22:45:19 +00:00
|
|
|
|
lastmod: 2015-05-22
|
2014-05-29 22:42:05 +00:00
|
|
|
|
date: 2013-07-01
|
2014-05-27 22:32:57 +00:00
|
|
|
|
menu:
|
|
|
|
|
main:
|
2014-05-29 22:42:05 +00:00
|
|
|
|
parent: layout
|
|
|
|
|
next: /templates/partials
|
|
|
|
|
prev: /templates/terms
|
|
|
|
|
title: Content Views
|
|
|
|
|
weight: 70
|
2015-05-22 18:46:09 +00:00
|
|
|
|
toc: true
|
2014-05-27 22:32:57 +00:00
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
In addition to the [single content template](/templates/content/), Hugo can render alternative views of
|
2015-01-28 02:17:09 +00:00
|
|
|
|
your content. These are especially useful in [list templates](/templates/list/).
|
2014-05-27 22:32:57 +00:00
|
|
|
|
|
|
|
|
|
For example you may want content of every type to be shown on the
|
|
|
|
|
homepage, but only a summary view of it there. Perhaps on a taxonomy
|
|
|
|
|
list page you would only want a bulleted list of your content. Views
|
|
|
|
|
make this very straightforward by delegating the rendering of each
|
|
|
|
|
different type of content to the content itself.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Creating a content view
|
|
|
|
|
|
2014-12-24 23:58:28 +00:00
|
|
|
|
To create a new view, simply create a template in each of your different
|
2014-12-24 11:55:11 +00:00
|
|
|
|
content type directories with the view name. In the following example, we
|
2014-05-27 22:32:57 +00:00
|
|
|
|
have created a "li" view and a "summary" view for our two content types
|
2014-12-24 11:55:11 +00:00
|
|
|
|
of post and project. As you can see, these sit next to the [single
|
2015-01-28 02:17:09 +00:00
|
|
|
|
content view](/templates/content/) template "single.html". You can even
|
2014-05-27 22:32:57 +00:00
|
|
|
|
provide a specific view for a given type and continue to use the
|
|
|
|
|
\_default/single.html for the primary view.
|
|
|
|
|
|
|
|
|
|
▾ layouts/
|
|
|
|
|
▾ post/
|
|
|
|
|
li.html
|
|
|
|
|
single.html
|
|
|
|
|
summary.html
|
|
|
|
|
▾ project/
|
|
|
|
|
li.html
|
|
|
|
|
single.html
|
|
|
|
|
summary.html
|
|
|
|
|
|
|
|
|
|
Hugo also has support for a default content template to be used in the event
|
|
|
|
|
that a specific template has not been provided for that type. The default type
|
2014-09-03 04:12:26 +00:00
|
|
|
|
works the same as the other types, but the directory must be called "_default".
|
2014-05-27 22:32:57 +00:00
|
|
|
|
Content views can also be defined in the "_default" directory.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
▾ layouts/
|
|
|
|
|
▾ _default/
|
|
|
|
|
li.html
|
|
|
|
|
single.html
|
|
|
|
|
summary.html
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Which Template will be rendered?
|
|
|
|
|
Hugo uses a set of rules to figure out which template to use when
|
|
|
|
|
rendering a specific page.
|
|
|
|
|
|
2014-09-03 04:12:26 +00:00
|
|
|
|
Hugo will use the following prioritized list. If a file isn’t present,
|
|
|
|
|
then the next one in the list will be used. This enables you to craft
|
2014-05-27 22:32:57 +00:00
|
|
|
|
specific layouts when you want to without creating more templates
|
2014-09-03 04:12:26 +00:00
|
|
|
|
than necessary. For most sites only the \_default file at the end of
|
2014-05-27 22:32:57 +00:00
|
|
|
|
the list will be needed.
|
|
|
|
|
|
|
|
|
|
* /layouts/`TYPE`/`VIEW`.html
|
|
|
|
|
* /layouts/\_default/`VIEW`.html
|
|
|
|
|
* /themes/`THEME`/layouts/`TYPE`/`VIEW`.html
|
|
|
|
|
* /themes/`THEME`/layouts/\_default/`view`.html
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Example using views
|
|
|
|
|
|
|
|
|
|
### rendering view inside of a list
|
|
|
|
|
|
|
|
|
|
Using the summary view (defined below) inside of a ([list
|
2015-01-28 02:17:09 +00:00
|
|
|
|
templates](/templates/list/)).
|
2014-05-27 22:32:57 +00:00
|
|
|
|
|
|
|
|
|
<section id="main">
|
|
|
|
|
<div>
|
|
|
|
|
<h1 id="title">{{ .Title }}</h1>
|
|
|
|
|
{{ range .Data.Pages }}
|
|
|
|
|
{{ .Render "summary"}}
|
|
|
|
|
{{ end }}
|
|
|
|
|
</div>
|
|
|
|
|
</section>
|
|
|
|
|
|
2014-12-24 11:55:11 +00:00
|
|
|
|
In the above example, you will notice that we have called `.Render` and passed in
|
|
|
|
|
which view to render the content with. `.Render` is a special function available on
|
2014-05-27 22:32:57 +00:00
|
|
|
|
a content which tells the content to render itself with the provided view template.
|
2014-12-24 11:55:11 +00:00
|
|
|
|
In this example, we are not using the li view. To use this we would
|
2014-05-27 22:32:57 +00:00
|
|
|
|
change the render line to `{{ .Render "li" }}`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### li.html
|
|
|
|
|
|
|
|
|
|
Hugo will pass the entire page object to the view template. See [page
|
2015-01-28 02:17:09 +00:00
|
|
|
|
variables](/templates/variables/) for a complete list.
|
2014-05-27 22:32:57 +00:00
|
|
|
|
|
2015-01-28 02:17:09 +00:00
|
|
|
|
This content template is used for [spf13.com](http://spf13.com/).
|
2014-05-27 22:32:57 +00:00
|
|
|
|
|
|
|
|
|
<li>
|
|
|
|
|
<a href="{{ .Permalink }}">{{ .Title }}</a>
|
|
|
|
|
<div class="meta">{{ .Date.Format "Mon, Jan 2, 2006" }}</div>
|
|
|
|
|
</li>
|
|
|
|
|
|
|
|
|
|
### summary.html
|
|
|
|
|
|
|
|
|
|
Hugo will pass the entire page object to the view template. See [page
|
2015-01-28 02:17:09 +00:00
|
|
|
|
variables](/templates/variables/) for a complete list.
|
2014-05-27 22:32:57 +00:00
|
|
|
|
|
2015-01-28 02:17:09 +00:00
|
|
|
|
This content template is used for [spf13.com](http://spf13.com/).
|
2014-05-27 22:32:57 +00:00
|
|
|
|
|
|
|
|
|
<article class="post">
|
|
|
|
|
<header>
|
|
|
|
|
<h2><a href='{{ .Permalink }}'> {{ .Title }}</a> </h2>
|
|
|
|
|
<div class="post-meta">{{ .Date.Format "Mon, Jan 2, 2006" }} - {{ .FuzzyWordCount }} Words </div>
|
|
|
|
|
</header>
|
|
|
|
|
|
|
|
|
|
{{ .Summary }}
|
|
|
|
|
<footer>
|
|
|
|
|
<a href='{{ .Permalink }}'><nobr>Read more →</nobr></a>
|
|
|
|
|
</footer>
|
|
|
|
|
</article>
|
|
|
|
|
|
|
|
|
|
|