HUGO

resources.Get not works in shortcodes with .Get 0

Hello,

The following code is working in shortcode if I passed constant string to resources.Get.

{{ $js_new_file := resources.Get “js/answers/2021/apr/answers-1.js” }}

{{ $js_answer := $js_new_file | minify }}

Although, when I passed .Get 0 or .Get “src” to resources.Get it gives me error.

{{ $js_new_file := resources.Get .Get 0 }}

{{ $js_answer := $js_new_file | minify }}

I even tried to use {{ $js_new_file := resources.Get (.Get 0) }}

ShortCode:

{{< customscript src=“js/answers/2021/apr/answers-1.js” >}}

Please look if anyone can help me on this.

{{ $param := printf "%s" (.Get 0) }}

You probably have to transform the parameter into a string. Then you can use it with resources.Get.

Shortcodes accept two types of parameters: named and positional.

Named

{{< myshortcode arg="foo" >}}
{{ .Get "arg" }}

Positional

{{< myshortcode "foo" >}}
{{ .Get 0 }}

You are trying to access a named parameter with the positional syntax.

Hello David,

I tried your solution but still getting error.

{{ $js_file_name := printf “%s” (.Get “src”) }}

{{ $js_resource := resources.Get $js_file_name }}

{{ $js_answer := $js_resource | minify }}

at <minify>: wrong type for value; expected resources.ResourceTransformer; got resource.Resource

Hi jmooring,

I tried named as well but it’s not working. Please look the code I just shared with David.

Huge thanks for David and your quick feedback.

Please read my previous response carefully.

You can either do this:

{{< customscript "js/answers/2021/apr/answers-1.js" >}}
{{ $js_new_file := resources.Get (.Get 0) }}

Or this:

{{< customscript src="js/answers/2021/apr/answers-1.js" >}}
{{ $js_new_file := resources.Get (.Get "src") }}

But you cannot mix the two.

There is no need to use printf anywhere.

Hello jmooring,

I tried your last solution and it’s still not working.

{{< customscript src=“js/answers/2021/apr/answers-1.js” >}}

{{ $js_resource := resources.Get .Get “src” }}

{{ $js_answer := $js_resource | minify }}

Error I am receiving are following.

at <resources>: wrong number of args for Get: want 1 got 2

Also, if I enclosed .Get “src” inside round brackets still getting error.

{{< customscript src=“js/answers/2021/apr/answers-1.js” >}}

{{ $js_resource := resources.Get (.Get “src”) }}

{{ $js_answer := $js_resource | minify }}

Error: at : wrong type for value; expected resources.ResourceTransformer; got resource.Resource

git clone --single-branch -b hugo-forum-topic-32618 https://github.com/jmooring/hugo-testing hugo-forum-topic-32618
cd hugo-forum-topic-32618
hugo server

Then visit http://localhost:1313/post/test/.

Thanks jmooring, your git code example is working. Let me find out why my code is not working.

Hello jmooring,

The git code you provided working on your project and minify as well. However, on my side it’s working but not minify. I find that it’s working if I use $js_answer.Permalink instead of $js_answer | absURL.

May be due to any config settings or structural changes but following code is working with minify.

{{ $js_resource := resources.Get (.Get “src”) }}

{{ $js_answer := $js_resource | resources.Minify }}

<script src='{{ $js_answer.Permalink }}'></script>

I really appreciate your time and patience. Great! work Man.

Resources are published only if their .Permalink or .RelPermalink is used in a template.

Therefore calling a .Resource with the absURL function will not work.

The following quote is from the Hugo Documentation about Assets Publishing

Assets will only be published (to /public ) if .Permalink or .RelPermalink is used.

1 Like

Thank you for clarification @onedrawingperday