# Trying to understand executeAsTemplate for javascripts

**URL:** <https://discourse.gohugo.io/t/trying-to-understand-executeastemplate-for-javascripts/29782>\
**Category:** support\
**Created:** [December 3, 2020, 3:36pm UTC](https://discourse.gohugo.io/t/trying-to-understand-executeastemplate-for-javascripts/29782 "2020-12-03T15:36:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidsneighbour](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/davidsneighbour/32/24507_2.png) [@davidsneighbour](https://discourse.gohugo.io/u/davidsneighbour)\
**Post date:** [December 3, 2020, 3:36pm UTC](https://discourse.gohugo.io/t/trying-to-understand-executeastemplate-for-javascripts/29782/1 "2020-12-03T15:36:36Z")

</div>

I am trying to add a javascript file to my pipe that needs to be executed as template. Somehow it fails (of course) and I am not sure what exactly the problem is.

the pipe:

```auto
{{ $js1 := resources.Get "js/somefile.js" }}
{{ $js2 := resources.Get "js/someotherfile.js" }}

{{ $collection := slice $js1 $js2}}
{{ $template := "js/somecompletelydifferentfile.js" }}
{{ $js3 := resources.Get $template | resources.ExecuteAsTemplate $template . }}

{{ $collection = $collection | append ($js3) }}

{{ $js := $collection | resources.Concat "script.js" | resources.Minify | resources.Fingerprint "sha384" }}

```

the error:

```auto
Error: 
Error building site: 
failed to render pages: render of "page" failed: execute of template failed: 
template: 
_default/single.html:40:5: executing "_default/single.html" at <partial "footer" .>: 
error calling partial: 
execute of template failed: 
template: 
partials/footer.html:6:8: executing "partials/footer.html" at <partialCached "func/getJavascript.html" .>: 
error calling partialCached: "/home/patrick/Projects/Hugo/dnb-hugo-garuda/layouts/partials/func/getJavascript.html:15:51": 
execute of template failed: 
template: 
partials/func/getJavascript.html:15:51: 
executing "partials/func/getJavascript.html" at <resources.ExecuteAsTemplate>: 
error calling ExecuteAsTemplate: 
type <nil> not supported in Resource transformations

```

That line 15 in getJavascript.html is the `{{ $js3 := resources.Get $template | resources.ExecuteAsTemplate $template . }}` part of the partial.

I admit I never got executeAsTemplate running in any case, so I might have a total failure of understanding the underlying concept here, but what might be the error? I would assume whatever resources.Get returns should be the same type as what (resources.Get | executeAsTemplate) returns?

If not, how would you attempt the following:

- there is a list of js files that need executing
- there is another list of js files that don’t need executing
- all those files need to be concatenated, minified, postprocessed afterwards.

---

<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:** [December 3, 2020, 3:45pm UTC](https://discourse.gohugo.io/t/trying-to-understand-executeastemplate-for-javascripts/29782/2 "2020-12-03T15:45:39Z")

</div>

looks like because the target path is exists?,

the first argument of `resources.ExecuteAsTemplate` is the target path. make sure it’s unique non-existent target path.

```auto
 {{ $js3 := resources.Get $template | resources.ExecuteAsTemplate (print now.Unix $template) . }}

```

from the docs here, we can see the target path is different from the original resource being processed:

```auto
{{ $sassTemplate := resources.Get "sass/template.scss" }}
{{ $style := $sassTemplate | resources.ExecuteAsTemplate "main.scss" . | resources.ToCSS }}

```

> **[Creating a resource from template](https://gohugo.io/hugo-pipes/resource-from-template/)**
>
> Hugo Pipes allows the creation of a resource from an asset file using Go Template.

---

<div class="post-metadata">

**Author:** ![davidsneighbour](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/davidsneighbour/32/24507_2.png) [@davidsneighbour](https://discourse.gohugo.io/u/davidsneighbour)\
**Post date:** [December 3, 2020, 3:58pm UTC](https://discourse.gohugo.io/t/trying-to-understand-executeastemplate-for-javascripts/29782/3 "2020-12-03T15:58:13Z")

</div>

The error stops coming up with your solution, but I am not sure if it works in general, because I don’t find that code in the resulting javascript file… this might with high probability be a completely unrelated issue… I’ll add the “solution” checkmark once I fixed it 🙂

On a sidenote (for future documentation optimizers): NOTHING on that documentation page says that the first parameter of `executeAsTemplate` must be different from the incoming file name. And I have the feeling that many samples here on Discourse use the same filename too. There really needs to be some kind of “language” that makes it clear to the uninitialized user. Some form of phpdoc or jsdoc for go.

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [December 3, 2020, 4:05pm UTC](https://discourse.gohugo.io/t/trying-to-understand-executeastemplate-for-javascripts/29782/4 "2020-12-03T16:05:22Z")

</div>

Not knowing your exact requirements, but I would recommend you look into js.Build with its params option.
