How to Pass Go's Pipeline into String (or Vice Versa)

Say I parsed the following file processed it into a string:

{{- $x := 1 -}}
{{- printf "Hello World %v time." $x -}}

How can I pass the page pipeline to process that string or vice versa without needing to re-write the entire text/template package (already proven possible but it’s a heavy work)?

Why:

  1. Files are located anywhere but outside of layouts/partials. I just want to build an API like partial hopefully without taking the long road.

I didn’t understood none of your sentence. Nothing makes sense to me.

What are you trying to achieve ?

Normally, we place the rendering file above in the layouts/partials directory, use the partial myfilename.html and call it a day. The file can access the Go functions and the rendering is complete.

However, I got template files that are not residing inside the layouts directory. The best I can achieve was to use readFile function to read those layouts as string but I cannot pass in all the Go’s functions into it like partial API does.

My goal is to make sure I can pass those Go functions into the string and then render as normal.

I already have a way (by building the decoder from scratch that recogizes {{- -}} braces accurately but to develop all the pipelines (e.g. index, range, …) will take a very long time.

Hence, the question, can the pipelines (Go functions) be passed into the read out string (or pass the string to somewhere or something) so that the rendering can be done similar to partial function?


Bottomline: as long as I some kind of partial-like API that works outside of layouts/partials directory, I’m good.

OR an API that can access to this for page context:

OK.

May be you can try to mount your “external” partial files as modules in “layouts”

Haha. Thanks. I tried this method but not feasible.

Those files are staying with their page-specific directory in content. I believe we can’t conflict them in the configs. Moreover, those files’ existence are chaotic (some directories has them, some not).

Got any other idea?

Site-note: modules are fine and working as expected. I don’t think we should modify this drastic settings.

Just out of curiosity, why do you have such a weird setup, inducing your problems ?? And not to stick with the way Hugo works.

Because I need the theme module to be an extremely stable content+asset compiler on its own for coping with the latest web standards and UI fashion demands (dating 2023) + to resolve some user experience problem when using Hugo. So far, kudos to the developers for making Hugo that powerful fitting in nicely.

Resources run scarce when you need to deal with a lot of aspects simultaneously like responsive graphics + PWA + WASM, CSS variables adjustment, low-code, publishers-specific files, all those SEO requirements, etc.

In short, we won’t have time dealing with disputable inconsistencies caused by Hugo (e.g. case `absLangURL` and `absURL` alters BaseURL in Multilingual Mode · Issue #5736 · gohugoio/hugo · GitHub - both me and bep are right in our own perspectives). Sometimes, Hugo developers cannot take certain design decisions deliberately (as they are serving a wider audience) but the theme developers have to step up and compliment them for our specific use cases.

I may be a crazy oddball but you can’t blame me for creating the problem though. Let’s reference Local file templates | Hugo, quoting:

Hugo’s readDir and readFile functions make it easy to traverse your project’s directory structure and write file contents to your templates.

It did the read part correctly but the development forgotten about feeding pipeline functionalities into the content after reading. If it happens to me intuitively, I believe it will happen to the end user as well.

To make sure I’m not missing something, hence, this ticket is raised.

Have you tried combining resources.FromString then execute it using resources.ExecuteAsTemplate?

References:

1 Like

Thank you @pamubay. You saved the day! :grinning:


For the following test sample read from using readFile:

{{- $ret := "<p>{{- $x := 2 -}}\n{{- printf \"Hello World %v\" $x -}}</p>" -}}

You just need to go through something like:

{{- $filename := (printf "deletable-%v-%v" (now.Format "200601021504050700") (crypto.FNV32a $ret)) -}}
{{- $dataList := resources.FromString $filename $ret -}}
{{- $dataList = resources.ExecuteAsTemplate $filename . $dataList -}}
{{- $dataList = string $dataList.Content -}}

Which you get the following printout:

// {{- warnf "DEBUG: '%v'\n" $dataList -}}
DEBUG: '<p>Hello World 2</p>'

Wrap the above into a template function, you’ll get a template capable string sanitiser.

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