Templating code outside "layouts" [solved]

Can you tell me if executing templating code outside the layouts folder, for instance in a bundle resource, is possible or impossible ?
This template (which otherwise works) doesn’t do it, the page appears but golang is plain text. I surmise that what I want is impossible but would like a confirmation.

{{ with .Resources.GetMatch "*.html" }}
  {{ .Content | $.Render }}
{{ end }}


See https://gohugo.io/hugo-pipes/resource-from-template/

But I can not pass the .Content of the same page like that. Or the page itself.

ok, what should I write in single.html so that to the following content file under content/docs/essai/ is rendered as if taken from assets:

type: standalone
title: essai

<!DOCTYPE html><html><body>

Currently it’s {{. | resources.ExecuteAsTemplate "content" . }}
but page.Page not supported in Resource transformations. why is this not working but {{ resources.Get "sass/template.scss" | resources.ExecuteAsTemplate "main.scss" . }} is fine ?
.RawContent (string) is not accepted and Content (template.HTML) neither.
If had lots of exterior html pages to include, putting them under assets/ would need the directory structure duplicated so I would rather have them as content files.

I have no idea what you are trying to do. Maybe you should define the objective, and then we can figure out a way to get there.

Sorry, I didn’t express myself well.
What template can get this file be rendered as a html but with the goland code interpreted ?

I still have no idea what you are trying to do. Maybe someone can help.

damn, I must have learnt Chinese without noticing :rofl:
It’s simple. I have html files in my content directory, with a frontmatter. I want them to be published, but with the template code in it to be executed/rendered/whatever. Is it possible or not ?

There are a few ways to handle this:

  1. A template, specified by the layout field in front matter, that executes .RawContent as a template
  2. A shortcode that executes .Inner as a template
  3. An inline shortcode

It depends on whether or not you anticipate adding anything other than template code in the body of the page.

I think #2 is the most flexible and easy to use. Let me know which you prefer.

1 Like

I meant the option 1, yes. something must go in single.html but I don’t know what.


{{ with resources.FromString .File.Path .RawContent }}
  {{ with . | resources.ExecuteAsTemplate $.File.Path $.Page }}
    {{ .Content | safeHTML }}
  {{ end }}
{{ end }}


title: "Foo"
date: 2023-07-23T15:03:53-07:00
draft: false
layout: "render-content-as-template-code"

<!DOCTYPE html><html><body>
1 Like

Ahahah, that’s the insane wizardry I expected. A very complicated code to a simple problem :rofl:. Thanks. Imho, you should put that in the site somewhere, in Single page templates | Hugo perhaps. Other people might find useful what are basically singe-use templates.

No, it’s not.

it wasn’t a criticism. it just wasn’t obvious that text or templateHTML etc are not considered “resources” because we don’t have access to a full map of internal types hugo utilize. nevermind. thanks !

Ok one last question, I tried with a mhtml file that I have, that chromium read correctly. hugo does more things than just process the templating code here, so the file gets mangled. I rename the extension to “html” then rename the output’s “mhtml”.

can I tell it to treat the file as plain text and not touch it at all beside the code ?
I wanna serve my archives or other pages and use hugo as a preprocessor of sort (beside my articles).

I don’t understand your question.

I have a series of mhtml files, a webarchive format. chromium (among other browsers) read it and use it to store web pages.
I want to serve them on my website. I could put them in static, but I need them as content files to write things, use templating code etc, the initial purpose of this thread.
But they’re mhtml, and so hugo ignore them… and when I change the extension to html, they become invalid with the message: Malformed multipart archive: *filename*
so is it possible to avoid touching the file at all except to expand the golang code ?

I’ll first try with custom mediatypes and outputs.

Sorry, but I’m not going to spend any time on this.

no problem. I’ve done it with custom output and mediatypes.

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