2013-08-17 08:34:25 -04:00
|
|
|
---
|
|
|
|
title: "Content Views"
|
|
|
|
date: "2013-07-01"
|
2013-10-26 02:18:14 -04:00
|
|
|
groups: ["layout"]
|
|
|
|
groups_weight: 70
|
2013-08-17 08:34:25 -04:00
|
|
|
---
|
|
|
|
|
|
|
|
In addition to the [single content view](/layout/content/), Hugo can render alternative views of
|
2013-11-07 13:47:42 -05:00
|
|
|
your content. These are especially useful in [index](/layout/indexes) templates.
|
2013-08-17 08:34:25 -04:00
|
|
|
|
|
|
|
To create a new view simple create a template in each of your different content
|
|
|
|
type directories with the view name. In the following example we have created a
|
|
|
|
"li" view and a "summary" view for our two content types of post and project. As
|
|
|
|
you can see these sit next to the [single content view](/layout/content)
|
|
|
|
template "single.html"
|
|
|
|
|
|
|
|
▾ 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
|
|
|
|
works the same as the other types but the directory must be called "_default".
|
|
|
|
Content views can also be defined in the "_default" directory.
|
|
|
|
|
|
|
|
|
|
|
|
▾ layouts/
|
|
|
|
▾ _default/
|
|
|
|
li.html
|
|
|
|
single.html
|
|
|
|
summary.html
|
|
|
|
|
|
|
|
|
|
|
|
Hugo will pass the entire page object to the view template. See [page
|
|
|
|
variables](/layout/variables) for a complete list.
|
|
|
|
|
|
|
|
## Example li.html
|
|
|
|
This content template is used for [spf13.com](http://spf13.com).
|
|
|
|
|
2014-01-10 21:19:19 -05:00
|
|
|
{{% highlight html %}}
|
|
|
|
<li>
|
|
|
|
<a href="{{ .Permalink }}">{{ .Title }}</a>
|
|
|
|
<div class="meta">{{ .Date.Format "Mon, Jan 2, 2006" }}</div>
|
|
|
|
</li>
|
|
|
|
{{% /highlight %}}
|
2013-08-17 08:34:25 -04:00
|
|
|
|
|
|
|
## Example summary.html
|
|
|
|
This content template is used for [spf13.com](http://spf13.com).
|
|
|
|
|
2014-01-10 21:19:19 -05:00
|
|
|
{{% highlight html %}}
|
|
|
|
<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>
|
2013-08-17 08:34:25 -04:00
|
|
|
|
2014-01-10 21:19:19 -05:00
|
|
|
{{ .Summary }}
|
|
|
|
<footer>
|
|
|
|
<a href='{{ .Permalink }}'><nobr>Read more →</nobr></a>
|
|
|
|
</footer>
|
|
|
|
</article>
|
|
|
|
{{% /highlight %}}
|
2013-08-17 08:34:25 -04:00
|
|
|
|
|
|
|
|
|
|
|
## Example render of view
|
|
|
|
Using the summary view inside of another ([index](/layout/index)) template.
|
|
|
|
|
2014-01-10 21:19:19 -05:00
|
|
|
{{% highlight html %}}
|
|
|
|
<section id="main">
|
|
|
|
<div>
|
|
|
|
<h1 id="title">{{ .Title }}</h1>
|
|
|
|
{{ range .Data.Pages }}
|
|
|
|
{{ .Render "summary"}}
|
|
|
|
{{ end }}
|
|
|
|
</div>
|
|
|
|
</section>
|
|
|
|
{{% /highlight %}}
|
2013-08-17 08:34:25 -04: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
|
|
|
|
a content which tells the content to render itself with the provided view template.
|