Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to access Hugo's template variables in a javascript file?

Tags:

hugo

I'm trying to use react.js in Hugo. I know Go template variables are accessible in HTML file.

My question is how to access them in javascript. or is there a workaround?

thanks in advance.

UPDATE:

currently my workaround is to use meta tags in HTML and load Go template variables like this:

<meta name="title" content={{.Title}} />

and then in javascript,

function getMetaTitle() {
   var metas = document.getElementsByTagName('meta');

   for (i=0; i<metas.length; i++) {
      if (metas[i].getAttribute("name") == "title") {
         return metas[i].getAttribute("content");
      }
   }

   return "failed to access...";
}
var metaTitle = getMetaTitle();

but this way is inconvenient when the number of meta tags growing, is there a more concise way to do this?

like image 875
Gnimuc Avatar asked Sep 08 '15 11:09

Gnimuc


People also ask

Can I use JavaScript with Hugo?

Hugo Pipes can process JavaScript files with ESBuild. Any JavaScript resource file can be transpiled and “tree shaken” using js. Build which takes for argument either a string for the filepath or a dict of options listed below.

How do you import a variable in JavaScript?

To import a variable from another file in JavaScript:Export the variable from file A , e.g. export const str = 'Hello world' . Import the variable in file B as import { str } from './another-file. js' .

How do you define a variable in Hugo?

Variables are storage containers with descriptive names used to store data values. When you put a variable inside the curly brackets, the value stored in that variable will be inserted into the page. The easiest example of accessing a variable using go html template is to access page front matter.

What is Hugo JavaScript?

Hugo is a general-purpose website framework. Technically speaking, Hugo is a static site generator. Unlike systems that dynamically build a page with each visitor request, Hugo builds pages when you create or update your content.


1 Answers

I doubt Hugo and React is a good pair but that's off topic and I might be wrong about that. You are asking, how to get Hugo variables into website's JavaScript. My answer:


Hugo is static website engine, so it only converts templates and markup documents (with your content) into HTML files. Now, when you upload your files onto your server, your JS cannot see anything Hugo — only your files.

The question becomes, how to transfer Hugo variables into some files of your website.

As you suggested, it's best to write variables into your HTML (or JSON) using Hugo, then read them by JS. If it's small amount, use attributes or tags. If there's a lot and it doesn't differ per-page, use a separate JSON file.

For example, personally I have a multilingual site which a) requires different language titles to appear dynamically via JS; b) uses JS which queries different Lunr.js search indexes in JSON format.

For both I use data-<name> attributes:

    <section class="section-search" data-index="{{ .Site.BaseURL }}searchIndex.json" id="section-search">
      <input type="search" id="search-input" placeholder="{{ ( index $.Site.Data.translations $.Site.Params.locale ).dataloading }}" data-loaded="{{ ( index $.Site.Data.translations $.Site.Params.locale ).dataloaded }}">
      <!-- search button goes here -->
    </section>

For example, on English templates (rendered into /public/), data-loaded attribute would be in English, but for Lithuanian templates (rendered into /public/lt/), data-loaded attribute would be in Lithuanian.

I wouldn't worry about "growing meta tags", but you could maybe write variables into a JSON file and then read it in JS if you are concerned about HTML bloat?

I'm building custom JSON first as HTML, then minifying/renaming it into JSON when building indexes for Hugo Lunr search as per this recipe. Instead of "baking in" the content with range as in mentioned recipe, you could simply list all the variables.

By the way, I'm using npm scripts as a build runner (instead of Grunt/Gulp) so I use json-minify:

"index:prepare": "json-minify public/json/index.html > public/site-index.json",

You could "bake" JSON files with any content (including Hugo template variables) via Hugo this way. Hope it helps.

like image 84
revelt Avatar answered Jan 04 '23 00:01

revelt