"page" function doesn't work for getting page resources

Code: GitHub - willfaught/paige at ff21e2480a94fa6df7a591dd47e08495133f2ab1

Run and view: http://localhost:1313/shortcodes/gallery/

The images work.

Apply this diff:

diff --git a/layouts/partials/paige/resource.html b/layouts/partials/paige/resource.html
index 0ad1101..0307a67 100644
--- a/layouts/partials/paige/resource.html
+++ b/layouts/partials/paige/resource.html
@@ -16,10 +16,10 @@
 {{ end }}
 
 {{ if and (not $result) $page }}
-    {{ with $page.Resources.GetMatch $url }}
+    {{ with page.Resources.GetMatch $url }}
         {{ $result = . }}
     {{ else }}
-        {{ with $page.Resources.Get $url }}
+        {{ with page.Resources.Get $url }}
             {{ $result = . }}
         {{ end }}
     {{ end }}

Refresh http://localhost:1313/shortcodes/gallery/

The images are broken.

the page function always refer to current/top-most context page being executed.

based on this code, i suspect the Page object passed via $params.page is different than current Page being executed.

2 Likes

That would mean that the images would be wrong when using $page instead of page, but they’re correct.

I think you misunderstood @pamubay’s explanation.

The page function gives you access to the top-most page context.

content structure

content/
β”œβ”€β”€ posts/
β”‚   β”œβ”€β”€ post-1.md
β”‚   β”œβ”€β”€ post-2.md
β”‚   └── post-3.md
└── _index.md

layouts/home.html

{{ range site.Sections }}
  {{ range .Pages }}
    {{ page.Title }}
  {{ end }}
{{ end }}

This will print the title of content/_index.md (the home page) three times.

If you’re going to use the global page function, you need to know where you started.

And I can assure you that page.Resources.Somemethod works just fine.

3 Likes

Ah, I see. OK, I understand what’s happening now. Thanks to you both!

Why was β€œpage” designed to work that way instead of the way I expected? What’s the useful scenario in that case?

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