Why Content View and not a Partial?

I understand what a content view is but why would you use one over a partial?

There is a defined lookup order for content views.

Let’s say you have three content types:

content
├── article
│   ├── article-1.md
│   └── article-2.md
├── photo
│   ├── photo-1.md
│   └── photo-2.md
└── video
    ├── video-1.md
    └── video-2.md

And, when displaying a mix of content types on a list page, you want to format each item’s summary based on its type.

Using Partials

layouts/_default/list.html:

{{ define "main" }}
  {{ .Content }}
  {{ range .Site.RegularPages }}
    {{ partial (printf "summary/%s.html" .Type) . }}
  {{ end }}
{{ end }}

Then create a directory structure for the partials:

layouts/
└── partials
    └── summary
        ├── article.html
        ├── photo.html
        └── video.html

Using Content views

layouts/_default/list.html:

{{ define "main" }}
  {{ .Content }}
  {{ range .Site.RegularPages }}
    {{ .Render "summary" }}
  {{ end }}
{{ end }}
layouts/
├── _default
│   ├── baseof.html
│   ├── list.html
│   ├── single.html
│   └── summary.html
├── photo
│   └── summary.html
└── video
    └── summary.html

Notice that I did not define a “summary” content view for articles. When rendering an article it falls back to _default/summary.html.

Now, let’s say that I want the summary for photos to be different when rendering RSS pages:

layouts/
├── _default
│   ├── baseof.html
│   ├── list.html
│   ├── single.html
│   └── summary.html
├── photo
│   ├── summary.html
│   └── summary.rss.xml  <--
└── video
    └── summary.html

Additional References:

3 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.