Using inline partials in content adapters?

Hello,

I’m experimenting with Content Adapters, specifically I’d like to be able to use Go templates and pass these as the content to the .AddPagemethod. I came up with a simple example based on a combination of the examples in Content adapters and define as follows, which I added to content/test/_content.gotmpl:

{{ define "_partials/inline/foo.html" }}
  {{ printf "The _Hunchback of Notre Dame_ was written by Victor Hugo in %v." .answer }}
{{ end }}

{{ $content := dict
  "mediaType" "text/markdown"
  "value" (partial "inline/foo.html" (dict "answer" 1831))
}}

{{ $page := dict
  "content" $content
  "kind" "section"
  "path" "../test"
  "title" "The Hunchback of Notre Dame"
}}

{{ .AddPage $page }}

Unfortunately, this results in the following error message:

error building site: 
process: readAndProcessContent: "/content/test/_content.gotmpl:7:11": 
template: /content/test/_content.gotmpl:7:11: 
executing "/content/test/_content.gotmpl" at <partial "inline/foo.html" (dict "answer" 1831)>: 
error calling partial: partial "inline/foo.html" not found

Is it possible to define inline Go templates from Content Adapters in this way, or is that unsupported?

Thanks and best,

Ruben

It’s unsupported. I guess we could make that work, but you would need to create a GitHub issue remind me of that…

It is possible to use inline templates, which may or may not be what you want:

{{ define "foo" }}
  {{ printf "The _Hunchback of Notre Dame_ was written by Victor Hugo in %v." .answer }}
{{ end }}
{{ template "foo" . }}
1 Like

Thank you @bep for the super quick response, I’ve created an issue here: Add support for inline partials in content adapters · Issue #14480 · gohugoio/hugo · GitHub

Thank you also for the hint on template, it doesn’t fit my use case currently, because the output can’t be stored in a variable. But good to know that that does work.

I think as a workaround, I’ll just have to build the “template” in a string with printf—less elegant and readable, but it should work for now. It would be really awesome if this functionality could be added, but I understand it’s a niche feature.

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