gohugoio / hugo

The world’s fastest framework for building websites.
https://gohugo.io
Apache License 2.0
76.11k stars 7.54k forks source link

render function lookup order different with doc #8101

Closed wu0407 closed 3 years ago

wu0407 commented 3 years ago

What version of Hugo are you using (hugo version)?

$ hugo version
Hugo Static Site Generator v0.79.1/extended windows/amd64 BuildDate: unknown

Does this issue reproduce with the latest release?

yes

My project dir tree:

content
           post
                 one.md
           other
                 two.md
layouts
           _default
                  summary.html
           index.html
themes
          even
                layouts
                       post
                             single.html
                             summary.html

The home page layouts/index.html file use {{ .Render "summary" }} that it will using the content view located at /layouts/_default/summary.html, but in fact it use themes/even/layouts/post/summary.html when render post/one.md .

The docs for template lookup order: https://gohugo.io/templates/views/#which-template-will-be-rendered https://gohugo.io/functions/render/

I find lookup order:

layouts/post/summary.html
themes/even/layouts/post/summary.html
/layouts/_default/summary.html

layouts/index.html:

{{- define "content" -}}
  <section id="posts" class="posts">
    {{/* (index .Site.Paginate) */}}
    {{- $paginator := .Paginate (where (where .Site.RegularPages "Type" "in" site.Params.mainSections) ".Params.hiddenfromhomepage" "!=" true) }}
    {{/* {{- $paginator := .Paginate (where (where .Site.RegularPages "Kind" "page") ".Params.hiddenfromhomepage" "!=" true) }} */}}
    {{- range $paginator.Pages -}}
      {{ .Render "summary" }}
    {{ end -}}
  </section>
  <!-- pagination -->
  <nav class="pagination">
    {{ with $paginator.Prev -}}
      <a class="prev" href="{{ .URL }}">
        <i class="iconfont icon-left"></i>
        <span class="prev-text">{{ T "prevPage" }}</span>
      </a>
    {{- end }}
    {{ with $paginator.Next -}}
      <a class="next" href="{{ .URL }}">
        <span class="next-text">{{ T "nextPage" }}</span>
        <i class="iconfont icon-right"></i>
      </a>
    {{- end }}
  </nav>
{{- end -}}
bep commented 3 years ago

Is it shit particular block you're talking about?

{{- range $paginator.Pages -}}
      {{ .Render "summary" }}
    {{ end -}}
  </section>

I moved it back here to have a look at it, but I don't think this is a bug (it has a million tests).

So, every Page in the loop above will get its own "what summary template do I get" evaluation. What I think you're seeing is that $paginator.Pages returns pages not only from the posts section.

wu0407 commented 3 years ago

Yes, I want to override the theme of summary template, so all section to use /layouts/_default/summary.html. You mean the evaluation search summary template order is expected.

bep commented 3 years ago

You mean the evaluation search summary template order is expected.

You have a summary overridden for a given section (post) -- but then you have content living in 2 sections, for the latter I would expect the "default summary" template would be used.

I suggest that you only query the regular pages inside posts (which I suspect is what you want).

wu0407 commented 3 years ago

yes , other section of page use default summary, only the post section use themes summary. so add layout/post/summary.html it works.

full tree

+---archetypes
|       default.md
|
+---content
|   +---kubernetes
|   |       node-lifecycle-controller-manager.md
|   |
|   +---osd
|   |       osd-new.md
|   |
|   \---post
|           new.md
|           new1.md
|
+---data
+---docs
|   |   404.html
|   |   android-chrome-192x192.png
|   |   android-chrome-512x512.png
|   |   apple-touch-icon.png
|   |   browserconfig.xml
|   |   favicon-16x16.png
|   |   favicon-32x32.png
|   |   favicon.ico
|   |   index.html
|   |   index.xml
|   |   manifest.json
|   |   mstile-150x150.png
|   |   robots.txt
|   |   safari-pinned-tab.svg
|   |   sitemap.xml
|   |   sitemap.xsl
|   |
|   +---categories
|   |       index.html
|   |       index.xml
|   |
|   +---fonts
|   |   +---chancery
|   |   |       apple-chancery-webfont.eot
|   |   |       apple-chancery-webfont.svg
|   |   |       apple-chancery-webfont.ttf
|   |   |       apple-chancery-webfont.woff
|   |   |       apple-chancery-webfont.woff2
|   |   |
|   |   \---iconfont
|   |           iconfont.eot
|   |           iconfont.svg
|   |           iconfont.ttf
|   |           iconfont.woff
|   |
|   +---img
|   |   |   spinner.svg
|   |   |
|   |   \---reward
|   |           alipay.png
|   |           wechat.png
|   |
|   +---js
|   |       main.min.c12618f9a600c40bd024996677e951e64d3487006775aeb22e200c990006c5c7.js
|   |
|   +---lib
|   |   +---fancybox
|   |   |       jquery.fancybox-3.1.20.min.css
|   |   |       jquery.fancybox-3.1.20.min.js
|   |   |
|   |   +---flowchartDiagrams
|   |   |       flowchart-1.8.0.min.js
|   |   |       raphael-2.2.7.min.js
|   |   |
|   |   +---highlight
|   |   |       highlight.pack.js
|   |   |
|   |   +---jquery
|   |   |       jquery-3.2.1.min.js
|   |   |
|   |   +---js-sequence-diagrams
|   |   |       danielbd.woff2
|   |   |       sequence-diagram-2.0.1.min.css
|   |   |       sequence-diagram-2.0.1.min.js
|   |   |       snap.svg-0.5.1.min.js
|   |   |       underscore-1.8.3.min.js
|   |   |       webfontloader-1.6.28.js
|   |   |
|   |   +---slideout
|   |   |       slideout-1.0.1.min.js
|   |   |
|   |   \---timeago
|   |           timeago-3.0.2.min.js
|   |           timeago.locales-3.0.2.min.js
|   |
|   +---page
|   |   \---1
|   |           index.html
|   |
|   +---sass
|   |       main.min.c7bc1becf36bcf6a9ebd25d2947e43a2eb745ddb0c9a32b43126fd7fa460c351.css
|   |
|   \---tags
|           index.html
|           index.xml
|
+---layouts
|   |   index.html
|   |
|   +---post
|   |       summary.html
|   |
|   \---_default
|           summary.html
|
+---resources
|   \---_gen
|       +---assets
|       |   \---scss
|       |       \---sass
|       |               main.scss_48b060fe05b0a273d182ef83c0605941.content
|       |               main.scss_48b060fe05b0a273d182ef83c0605941.json
|       |
|       \---images
+---static
\---themes
    \---even
        |   .gitignore
        |   CHANGELOG.md
        |   LICENSE.md
        |   Makefile
        |   netlify.toml
        |   README-zh.md
        |   README.md
        |   theme.toml
        |
        +---archetypes
        |       default.md
        |
        +---assets
        |   +---js
        |   |       even.js
        |   |       main.js
        |   |
        |   \---sass
        |       |   main.scss
        |       |   _base.scss
        |       |   _iconfont.scss
        |       |   _variables.scss
        |       |
        |       +---_common
        |       |       _animation.scss
        |       |       _normalize.scss
        |       |       _utils.scss
        |       |
        |       +---_custom
        |       |       _custom.scss
        |       |
        |       \---_partial
        |           |   _404.scss
        |           |   _archive.scss
        |           |   _back-to-top.scss
        |           |   _footer.scss
        |           |   _header.scss
        |           |   _mobile.scss
        |           |   _pagination.scss
        |           |   _post.scss
        |           |   _slideout.scss
        |           |   _terms.scss
        |           |
        |           +---_footer
        |           |       _copyright.scss
        |           |       _social.scss
        |           |
        |           +---_header
        |           |       _logo.scss
        |           |       _menu.scss
        |           |
        |           \---_post
        |                   _admonition.scss
        |                   _code.scss
        |                   _content.scss
        |                   _copyright.scss
        |                   _footer.scss
        |                   _header.scss
        |                   _outdated.scss
        |                   _reward.scss
        |                   _toc.scss
        |
        +---exampleSite
        |   |   config.toml
        |   |
        |   \---content
        |       |   about.md
        |       |
        |       \---post
        |               chinese-preview.md
        |               english-preview.md
        |               even-preview.md
        |               hidden-post.md
        |               japanese-preview.md
        |               js-flowchart-diagrams.md
        |               js-sequence-diagrams.md
        |               shortcodes.md
        |               syntax-highlighting.md
        |
        +---i18n
        |       de.yaml
        |       en.yaml
        |       es.yaml
        |       fr.yaml
        |       ja.yaml
        |       ru.yaml
        |       zh-CN.yaml
        |       zh-TW.yaml
        |
        +---images
        |       screenshot.png
        |       showcase.png
        |       tn.png
        |
        +---layouts
        |   |   404.html
        |   |   index.html
        |   |   robots.txt
        |   |   sitemap.xml
        |   |
        |   +---partials
        |   |   |   comments.html
        |   |   |   footer.html
        |   |   |   head.html
        |   |   |   header.html
        |   |   |   scripts.html
        |   |   |   slideout.html
        |   |   |
        |   |   \---post
        |   |           copyright.html
        |   |           outdated-info-warning.html
        |   |           reward.html
        |   |           toc.html
        |   |
        |   +---post
        |   |       single.html
        |   |       summary.html
        |   |
        |   +---shortcodes
        |   |       admonition.html
        |   |       bilibili.html
        |   |       center.html
        |   |       left.html
        |   |       music.html
        |   |       right.html
        |   |
        |   \---_default
        |           baseof.html
        |           section.html
        |           single.html
        |           single.md
        |           taxonomy.html
        |           terms.html
        |
        +---resources
        |   \---_gen
        |       \---assets
        |           \---scss
        |               \---sass
        |                       main.scss_48b060fe05b0a273d182ef83c0605941.content
        |                       main.scss_48b060fe05b0a273d182ef83c0605941.json
        |
        \---static
            |   android-chrome-192x192.png
            |   android-chrome-512x512.png
            |   apple-touch-icon.png
            |   browserconfig.xml
            |   favicon-16x16.png
            |   favicon-32x32.png
            |   favicon.ico
            |   manifest.json
            |   mstile-150x150.png
            |   safari-pinned-tab.svg
            |   sitemap.xsl
            |
            +---fonts
            |   +---chancery
            |   |       apple-chancery-webfont.eot
            |   |       apple-chancery-webfont.svg
            |   |       apple-chancery-webfont.ttf
            |   |       apple-chancery-webfont.woff
            |   |       apple-chancery-webfont.woff2
            |   |
            |   \---iconfont
            |           iconfont.eot
            |           iconfont.svg
            |           iconfont.ttf
            |           iconfont.woff
            |
            +---img
            |   |   spinner.svg
            |   |
            |   \---reward
            |           alipay.png
            |           wechat.png
            |
            \---lib
                +---fancybox
                |       jquery.fancybox-3.1.20.min.css
                |       jquery.fancybox-3.1.20.min.js
                |
                +---flowchartDiagrams
                |       flowchart-1.8.0.min.js
                |       raphael-2.2.7.min.js
                |
                +---highlight
                |       highlight.pack.js
                |
                +---jquery
                |       jquery-3.2.1.min.js
                |
                +---js-sequence-diagrams
                |       danielbd.woff2
                |       sequence-diagram-2.0.1.min.css
                |       sequence-diagram-2.0.1.min.js
                |       snap.svg-0.5.1.min.js
                |       underscore-1.8.3.min.js
                |       webfontloader-1.6.28.js
                |
                +---slideout
                |       slideout-1.0.1.min.js
                |
                \---timeago
                        timeago-3.0.2.min.js
                        timeago.locales-3.0.2.min.js
jmooring commented 3 years ago

Closing. Behavior is expected and documented.

github-actions[bot] commented 2 years ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.