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.