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.