Content Adapter Being Overwritten at Different Locations on Site

Hi Hugo folks,

I wanted to raise this question here as I am pretty confused. I am using the below content adapter which recursively parses over a JSON file & adds pages and sections. This template works great for my needs but I am trying to reuse it across different parts of my site.

In my case I am trying to pull different projects & content into different parts of the site.

That is where you can see I have added a conditional to check if the project id is in the JSON & will continue to the add page to add content. I have also added the project id to all levels of the hierarchy including the ‘children’ pages and so on.

The issue I am experiencing is when I add this content adapter to another part of the site- it seems like the initial one is overwritten.

For clarity say I have 2 folders: test 1 & test 2. I have pasted the same content adapter into both of these directories with the only difference being the index files and the project id in the conditional of the go template.

Would anyone have any ideas as to why this may be happening? If more detail is required I can boot up a Github repo to reproduce the issue.

{{ $jsonContent := site.Data.nested_data }}
 
{{ with $jsonContent }}
  {{ template "walk" (dict "data" .roots "path" "" "ctx" $) }}
{{ end }}
 
{{ define "walk" }}
  {{ range .data }}
 
    <!-- Build the path for the current node -->
    {{ $urlLink := .title | urlize }}
    {{ $path := path.Join $.path .hugoPath }}
   
    <!-- Render the page content -->
    {{ template "add-page" (dict "data" . "path" $path "ctx" $.ctx) }}
 
    <!-- Recursively walk through children -->
    {{ with .children }}
      {{ template "walk" (dict "data" . "path" $path "ctx" $.ctx) }}
    {{ end }}
  {{ end }}
{{ end }}
 
{{ define "add-page" }}
  {{ with .data }}
 
    {{/* Define the list of project IDs to include */}}
    {{ $includedProjects := slice "123456" }}
    {{if in $includedProjects .project_id}}
 
    <!-- Recursively walk through children -->
    {{ $taxonomies := readFile "config/_default/taxonomies.toml" | transform.Unmarshal }}
    {{ $params := dict }}
 
    {{ range $key, $value := . }}
      {{ if isset $taxonomies $key }}
        {{ $taxonomy := index $taxonomies $key }}
        {{ $params = merge $params (dict $taxonomy $value) }}
      {{ else }}
        {{ $params = merge $params (dict $key $value) }}
      {{ end }}
    {{ end }}
 
    {{ $content := dict "mediaType" "text/html" "value" .content }}
   
    {{$kind := ""}}
    {{$type := ""}}
    {{ if  .children}}
        {{ $kind = "section" }}
        {{ $type = "tileView"}}
    {{ else }}
        {{ $kind = "page" }}
        {{ $type = "docs"}}
    {{ end }}
 
    {{ $page := dict
      "content" $content
      "kind" $kind
      "params" $params
      "path" $.path
      "title" .title
      "type" $type
      "description" .description
      "tabletype" .tableType
    }}
 
 
    {{ $.ctx.AddPage $page }}
    {{ end }}
  {{ end }}
{{ end }}
 

Any help would be… helpful! Thanks in advance.

What does that mean?

As always, that would be very helpful.

I will set one up shortly.

I add a go template to hugosite/test1/_content.gotmpl
and one to go template to hugosite/test2/_content.gotmpl

The only difference is that I set this value to be different

{{ $includedProjects := slice "123456" }}

I build the site with this first then add the second go template with:

{{ $includedProjects := slice "98765" }}

It will only add stuff for 98765 to both pages or remove what has been added for 123456

My first thought is that the issue relates to my JSON data- but want to make sure that the go template logic is sound.

I have added warnfs to both go templates. When I build the site it only displays one of the warnings despite both go templates containing “includedProjects”. If I remove one it will show the other and vice versa. Is there any possibility that there could be an issue with how I am parsing over the list of projects to include?

What’s weird is when I put both to use, say one queries project 123, and the other queries project 456, both locations will render content for project 456.

When I individually just use one I can get both project 456 & 123 depending on which I have selected.

This makes me question if it is something to do with how these paths are made, if the addpage method may have some nuance I am missing, or something along these lines

I may be in a rabbit hole but I think the issue is I am checking for a Project ID at every recursive parse.

What I really want to do is just check for the Project ID at the highest level, and then if the conditional hits the given Project ID, add pages for its children- without checking EACH time we go down a level)

Does that sound like a reasonable assement?

I have no idea. A reproducible example would be very helpful.

Apologies for a vague explanation.

I was able to figure it out by just conditionally selecting pages at the top level & pulling out their children, instead of conditionally checking for a match at each level at hierarchy.

Probably have some sort of mismatch or many to many in the data that was causing issues, but seems to be resolved with the new logic. I will attach the updated template when I return home.

Thanks for the help!