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

**URL:** <https://discourse.gohugo.io/t/how-to-pass-gos-pipeline-into-string-or-vice-versa/43289>\
**Category:** support\
**Created:** [March 5, 2023, 12:57am UTC](https://discourse.gohugo.io/t/how-to-pass-gos-pipeline-into-string-or-vice-versa/43289 "2023-03-05T00:57:13Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![hollowaykeanho](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/hollowaykeanho/32/22239_2.png) [@hollowaykeanho](https://discourse.gohugo.io/u/hollowaykeanho)\
**Post date:** [March 5, 2023, 12:57am UTC](https://discourse.gohugo.io/t/how-to-pass-gos-pipeline-into-string-or-vice-versa/43289/1 "2023-03-05T00:57:13Z")

</div>

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

```auto
{{- $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.

---

<div class="post-metadata">

**Author:** ![divinerites](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/divinerites/32/12625_2.png) [@divinerites](https://discourse.gohugo.io/u/divinerites)\
**Post date:** [March 5, 2023, 5:27am UTC](https://discourse.gohugo.io/t/how-to-pass-gos-pipeline-into-string-or-vice-versa/43289/2 "2023-03-05T05:27:52Z")

</div>

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

What are you trying to achieve ?

---

<div class="post-metadata">

**Author:** ![hollowaykeanho](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/hollowaykeanho/32/22239_2.png) [@hollowaykeanho](https://discourse.gohugo.io/u/hollowaykeanho)\
**Post date:** [March 5, 2023, 5:39am UTC](https://discourse.gohugo.io/t/how-to-pass-gos-pipeline-into-string-or-vice-versa/43289/3 "2023-03-05T05:39:03Z")

</div>

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:

> **[template package - text/template - Go Packages](https://pkg.go.dev/text/template#Template.Parse)**
>
> Package template implements data-driven templates for generating textual output.

---

<div class="post-metadata">

**Author:** ![divinerites](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/divinerites/32/12625_2.png) [@divinerites](https://discourse.gohugo.io/u/divinerites)\
**Post date:** [March 5, 2023, 5:46am UTC](https://discourse.gohugo.io/t/how-to-pass-gos-pipeline-into-string-or-vice-versa/43289/4 "2023-03-05T05:46:15Z")

</div>

OK.

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

> **[Configure Modules](https://gohugo.io/hugo-modules/configuration/#module-config-mounts)**
>
> This page describes the configuration options for a module.

---

<div class="post-metadata">

**Author:** ![hollowaykeanho](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/hollowaykeanho/32/22239_2.png) [@hollowaykeanho](https://discourse.gohugo.io/u/hollowaykeanho)\
**Post date:** [March 5, 2023, 5:51am UTC](https://discourse.gohugo.io/t/how-to-pass-gos-pipeline-into-string-or-vice-versa/43289/5 "2023-03-05T05:51:21Z")

</div>

> [@divinerites](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![divinerites](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/divinerites/32/12625_2.png) [@divinerites](https://discourse.gohugo.io/u/divinerites)\
**Post date:** [March 5, 2023, 10:01am UTC](https://discourse.gohugo.io/t/how-to-pass-gos-pipeline-into-string-or-vice-versa/43289/6 "2023-03-05T10:01:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hollowaykeanho](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/hollowaykeanho/32/22239_2.png) [@hollowaykeanho](https://discourse.gohugo.io/u/hollowaykeanho)\
**Post date:** [March 5, 2023, 10:50am UTC](https://discourse.gohugo.io/t/how-to-pass-gos-pipeline-into-string-or-vice-versa/43289/7 "2023-03-05T10:50:21Z")

</div>

> [@divinerites](#):
>
> why do you have such a weird setup, … 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](https://github.com/gohugoio/hugo/issues/5736) - 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.

> [@divinerites](#):
>
> inducing your problems ??

I may be a crazy oddball but you can’t blame me for creating the problem though. Let’s reference [Local file templates | Hugo](https://gohugo.io/templates/files/), 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.

---

<div class="post-metadata">

**Author:** ![pamubay](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/pamubay/32/17838_2.png) [@pamubay](https://discourse.gohugo.io/u/pamubay)\
**Post date:** [March 5, 2023, 1:39pm UTC](https://discourse.gohugo.io/t/how-to-pass-gos-pipeline-into-string-or-vice-versa/43289/8 "2023-03-05T13:39:19Z")

</div>

> [@hollowaykeanho](#):
>
> 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.

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

References:

- [FromString | Hugo](https://gohugo.io/hugo-pipes/resource-from-string/)
- [ExecuteAsTemplate | Hugo](https://gohugo.io/hugo-pipes/resource-from-template/)

---

<div class="post-metadata">

**Author:** ![hollowaykeanho](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/hollowaykeanho/32/22239_2.png) [@hollowaykeanho](https://discourse.gohugo.io/u/hollowaykeanho)\
**Post date:** [March 5, 2023, 11:07pm UTC](https://discourse.gohugo.io/t/how-to-pass-gos-pipeline-into-string-or-vice-versa/43289/9 "2023-03-05T23:07:09Z")

</div>

> [@pamubay](#):
>
> Have you tried combining `resources.FromString` then execute it using `resources.ExecuteAsTemplate`?
> 
> References:
> 
> - [Creating a resource from a string | Hugo](https://gohugo.io/hugo-pipes/resource-from-string/)
> - [Creating a resource from template | Hugo](https://gohugo.io/hugo-pipes/resource-from-template/)

Thank you @pamubay. You saved the day! 😀

* * *

For the following test sample read from using `readFile`:

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

```

You just need to go through something like:

```auto
{{- $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:

```auto
// {{- 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.

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [March 7, 2023, 11:07pm UTC](https://discourse.gohugo.io/t/how-to-pass-gos-pipeline-into-string-or-vice-versa/43289/10 "2023-03-07T23:07:38Z")

</div>

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