Hugo evaluate template in json

I have the following code in a JSON file placed in /static directory. I want this json to be available at /search.json endpoint so that my client-side blog search works. There are no template errors but the template part isn’t rendered. What is pasted below is what I see when I visit the endpoint. Any ideas on how to evaluate the templating in the json file? Thanks!

  {{ range .Data.Pages }}
      "title"    : "{{ .Title }}",
      "article"  : "<div class='article col col-12'> <div class='article__inner'> <div class='article__content'> <h2 class='article__title'> <a href='{{ .RelPermalink }}'>{{ .Title }}<i class='ion ion-md-arrow-round-forward'></i></a> </h2> <div class='article__meta'> <time class="article__date" datetime="{{ .Date.Format "2006-01-02T15:04:05Z07:00" }}">{{ .Date.Format "2 January 2006" }}</time> – <span class='article__minutes'>{{ .ReadingTime }} min read</span> </div><p class='article__excerpt'>{{ .Summary }}</p></div></div></div>",
      "category" : "{{ with .Params.categories }}{{ . }}{{ end }}",
	  "content"  : "{{ .PlainWords }}",
      "tags"     : "{{ with .Params.tags }}{{ delimit . ", " }}{{ end }}",
      "url"      : "{{ .RelPermalink }}",
      "date"     : "{{ .Date.Format "2 January 2006" }}"
	{{ end }}

See the ExecuteAsTemplate funtction. The JSON will have to reside under the assetDir of your project. The staticDir by default will not load Assets.

1 Like

Thanks, that’s just what I wanted, but how do I make the evaluated resource sit at a particular endpoint?
I have this code in my index.html

{{ $search := resources.Get “json/search.json” }}
{{ $search := $search | resources.ExecuteAsTemplate “/search.json” . }}

no issues there, but when I build the site, it isn’t in anywhere in /public. I’m trying to make the evaluated search.json available at localhost:1313/search.json if that makes sense. :slight_smile:

Check out Output Formats

Here’s an awesome article by the great @regis with a step by step.

1 Like

A resource gets rendered only once its .Permalink is called:


Thanks, this seems closest to what I want. I’ve read the linked article four times now but I still can’t wrap my head around how to generate the json for just one endpoint, and not all pages!

you can selectively control output formats per page. . .

1 Like

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