{{ .URL }} is blank inside partials template

here is my code for layouts/index.html template

  {{ range where .Site.RegularPages "Section" "products"}}
    {{ partial "product.html" .Params }}
  {{ end }}

and in my partials/product.html, I have

 <h2 class="product__name"><a href="{{ .URL}}">{{ .name }}</a></h2>

{{ .URL}} is blank. Can anybody help me?

You are handing .Params to the partial, not . - If you have no parameter URL in your Params then its empty. The dot inside of your partial is .Params, not what the dot inside of index.html would be.

Read about the context: Hugo, the scope, the context and the dot | Regis Philibert

Not sure about the .URL too, it might be .Permalink within the context.

Thank you for the quick reply but actually I’ve already tried passing . (dot) only and got an error.

That error basically says what I noted above: where do you define name? Try .Title. That’s an existing variable. It can’t execute what is not there.

name is in the products section pages’ frontmatter

content/products/product-1.md

+++
title = "Product 1"
date = 2021-07-08T10:11:15-07:00
draft = false
name = "My Product"
+++

layouts/index.html

{{ range where .Site.RegularPages "Section" "products"}}
  {{ partial "product.html" . }}
{{ end }}

layouts/partials/product.html

<h2 class="product__name"><a href="{{ .RelPermalink }}">{{ .Params.name }}</a></h2>

As I have said, if I change {{ partial “product.html” .Params }} to {{ partial “product.html” . }}, I get that error.

Here’s the complete layouts/index.html

{{ partial "header.html" . }}
<main class="products">
  {{ range where .Site.RegularPages "Section" "products"}}
    {{ partial "product.html" .Params }}
  {{ end }}
</main>
{{ partial "footer.html" . }}

I do not post untested code.

git clone --single-branch -b hugo-forum-topic-33745 https://github.com/jmooring/hugo-testing hugo-forum-topic-33745
cd hugo-forum-topic-33745
hugo server
1 Like

One of the most common errors people do is that they have multiple product pages but not on all they have the actual frontmatter while they test around. ALL your pages need that frontmatter.

Without your sample repository we can’t help you.

I’ll share the test repo later. By the way, I’m integrating it with Forestry?

Here it is: GitHub - markvverallo/test

diff --git a/layouts/index.html b/layouts/index.html
index 1d538f2..132b1fb 100644
--- a/layouts/index.html
+++ b/layouts/index.html
@@ -1,7 +1,7 @@
 {{ partial "header.html" . }}
 <main class="products">
   {{ range where .Site.RegularPages "Section" "products"}}
-    {{ partial "product.html" .Params }}
+    {{ partial "product.html" . }}
   {{ end }}
 </main>
-{{ partial "footer.html" . }}
\ No newline at end of file
+{{ partial "footer.html" . }}
diff --git a/layouts/partials/product.html b/layouts/partials/product.html
index 7912420..316966e 100644
--- a/layouts/partials/product.html
+++ b/layouts/partials/product.html
@@ -1,18 +1,18 @@
 
 <div class="product">
-  <h2 class="product__name"><a href="{{ .RelPermalink }}">{{ .name }}</a></h2>
-  <img class="product__image" src="{{ .image }}" alt="">
-  <p class="product__description">{{ .description }}</p>
+  <h2 class="product__name"><a href="{{ .RelPermalink }}">{{ .Params.name }}</a></h2>
+  <img class="product__image" src="{{ .Params.image }}" alt="">
+  <p class="product__description">{{ .Params.description }}</p>
   <div class="product__button-container">
-    <div class="product__price">${{ .price }}</div>
+    <div class="product__price">${{ .Params.price }}</div>
     <button
       class="snipcart-add-item buy-button"
-      data-item-id="{{ .id }}"
-      data-item-name="{{ .name }}"
-      data-item-price="{{ .price }}"
-      data-item-url="{{ .product_url }}"
-      data-item-description="{{ .description }}">
+      data-item-id="{{ .Params.id }}"
+      data-item-name="{{ .Params.name }}"
+      data-item-price="{{ .Params.price }}"
+      data-item-url="{{ .Params.product_url }}"
+      data-item-description="{{ .Params.description }}">
       Add to cart
     </button>
   </div>
-</div>
\ No newline at end of file
+</div>

@jmooring Thank you. It’s working now except that I used {{ .URL }} instead of {{ .RelPermalink }}

However, I’m wondering now why index is only displaying one product and not three?

Look at the messages generated when building the site…

Page.URL is deprecated and will be removed in a future release. Use .Permalink or .RelPermalink. 

This is what I see:

Got it. Regarding index maybe, it has something to do with the filename convention between Windows and Mac. I fixed it by renaming and using only lower cases and no space.

Again, thank you so much.

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