HUGO

ExecuteAsTemplate for any file in Hugo

Hi everyone,

I’m using the following code to process a JavaScript file as a template with the intention of importing some of the data into it:

{{ $script := resources.Get "js/script.js" }}
{{ $scriptjs := $report | resources.ExecuteAsTemplate "script.js" . }}
<script src="{{ $scriptjs.Permalink }}" defer></script>

I’d like to do the same for Netlify functions. They are also JavaScript files that reside in /functions/ folder.

Is it possible, maybe in the config, it tell Hugo to process specific files as ExecuteAsTemplate. Currently, I don’t think Hugo is aware of any of the files inside of /functions/.

TL;DR - I’d like to use Hugo data in Netlify functions.

Thanks in advance!

Dave.

Do you mean that you want Hugo to generate your functions files? Or that you want Hugo to see your functions files and do something with the data?

I’d like Hugo to see my function files so I can import some data into them.

Thanks!

Dave.

To be more clear-- you want to Hugo to write a functions file, using template logic?

You can point netlify to a custom functions folder: Configure and deploy Functions | Netlify Docs

Note that assets only get published once you call its .Permalink: Hugo Pipes Introduction | Hugo

You need to run your $script.Permalink and generate it into public/somewhere, then point Netlify to public/somewhere

Thanks, @pointyfar!

That’s done the trick I think. I did the following steps:

  1. In Hugo, created a new layout that links to the various API files.
  2. In Hugo, created a new page which uses the template so the template is used in the public folder.
  3. In Netlify, set up a redirect from the new page to a 404 so it doesn’t exist on the website.
  4. In Netlify, changed the functions folder to /public/api/ which is where I generated the files to.

Seems to have done the trick. Can the process above be improved in anyway?

Thanks!

Dave.

Looks and sounds good!

I don’t use functions myself, so I couldn’t say if it could be improved from that perspective.

I wouldn’t have thought of point 3 myself, but it’s a good idea to do that.

Thanks for your help, @pointyfar!

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