How to reuse partials code for different sections in home page?

Hi, all

I am trying to have a snippet of code (partial) that lists some posts from a certain section (first 5 posts in content/eventos folder, for example) in the home page. My goal is to reuse the same code for other different sections, except for some style adjustments in heading color and the corresponding section posts.

To be more graphical, here is what I want to accomplish:

In my content/ folder I have two sections:

├── content
│   ├── eventos
│   ├── enfermedades

In index.html:

...
{{- partial "posts_category.html" (dict "contexto" . "articulosSeccion" "eventos" "cabecera" "rojo") }}
...
{{- partial "posts_category.html" (dict "contexto" . "articulosSeccion" "enfermedades" "cabecera" "negro") }}
...

Since I have a toml file in data/posts_category/eventos.toml (which is the name of a posts category, by the way),

title = "Eventos"
description = "Esta es la categoría de eventos de la asociación"

I also want to use it as the h2 title and lead text.

In my layouts/partials/posts_category.html:
(this code yet lacks the listing of the most recent articles for each section, I know, but it a work in progress ;D )

{{ range first 5 (where site.RegularPages "Section" "{{ .articulosSeccion }}") }}

<div id="seccion-{{ .articulosSeccion }}" class="container">
  <div class="row">
    <h2 class="">{{ .Site.Params.Data.posts_category.articulosSeccion.title }}</h2>
    <p class="lead">{{ .Site.Params.Data.posts_category.articulosSeccion.description }}</p>
    <div class="col-sm-6 bg-{{ .cabecera }}">
      <p>Here in this column I will deal with only the very most recent article of section articulosSeccion</p>
    </div>
    <div class="col-sm-6 bg-warning">
      <p>...and here in this column I will put the other 4 more recent articles</p>
    </div>
  </div>
</div>
{{ end }}

My problem is that Hugo renders ZERO of this code. Is it because of the passing of variables to the partial? I followed the documentation…

Nevertheless, I have these two lines in posts_category.html at the very beginning as troubleshooting:

{{ printf "%#v" . }}
{{ printf "%#v" $.Site }}

However, the output, as follows, helps me nothing:

map[string]interface {}{"articulosSeccion":"eventos", "cabecera":"rojo", "contexto":(*hugolib.pageState)(0xc000a1d140)}
map[string]interface {}{"articulosSeccion":"eventos", "cabecera":"rojo", "contexto":(*hugolib.pageState)(0xc000a1d140)}

And as output of range

Here, you pass current Page object . (the dot) under contexto:

{{- partial "posts_category.html" (dict "contexto" . "articulosSeccion" "eventos" "cabecera" "rojo") }}

.Site is part of Page object,

in layouts/partials/posts_category.html you need to call the .Site variable under contexto key:

{{ .contexto.Site.Params.Data.posts_category.articulosSeccion.title }}
{{ .contexto.Site.Params.Data.posts_category.articulosSeccion.title }}

Easy Way: Use site function instead .Site variable.

{{ site.Params.Data.posts_category.articulosSeccion.title }}
{{ site.Params.Data.posts_category.articulosSeccion.title }}

Thanks, @pamubay for your quick response!

I corrected as you told me. However, I still cannot get anything… Either this line or next one renders nothing:

{{ .contexto.Site.Params.Data.posts_category.articulosSeccion.title }}

{{ site.Params.Data.posts_category.articulosSeccion.title }}

site.Params.Data is not a thing.

You’re looking for site.Data.

See docs.

1 Like

You are right. Thanks.

In my mind I have mixed site.Data with .Params.variable-in-front-matter.

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