That day is upon us folks. It’s finally time to discuss on the Frontend part for creating forms in your Hugo Webpage.
Since we are creating a new HTML element in Hugo, we have to use something called shortcode.
You must be asking…
What is a shortcode?
Keeping inline with my copy-paste nature, here’s a brief description of the same
Hugo loves Markdown because of its simple content format, but there are times when Markdown falls short. Often, content authors are forced to add raw HTML (e.g., video iframe’s) to Markdown content. We think this contradicts the beautiful simplicity of Markdown’s syntax.
Hugo created shortcodes to circumvent these limitations.
A shortcode is a simple snippet inside a content file that Hugo will render using a predefined template. Note that shortcodes will not work in template files. If you need the type of drop-in functionality that shortcodes provide but in a template, you most likely want a partial template instead.
In addition to cleaner Markdown, shortcodes can be updated any time to reflect new classes, techniques, or standards. At the point of site generation, Hugo shortcodes will easily merge in your changes. You avoid a possibly complicated search and replace operation.
Making a shortcode for my form
If you are Certified Lazy™, you can copy below code into your layouts/shortcodes/contact.html. I am skipping detailing what each line does, as it’s pretty self-explanatory.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<formid="contact-form"name="contact-form"class="contact-form formdisable"onsubmit="return false">
<divclass="message">
<textareaid="message"class="form-input"name="message"required="required"aria-describedby="messageHelpBlock">Hi BLZR, </textarea>
<spanid="messageHelpBlock">Enter your message/query (Required)</span>
</div>
<divclass="captcha">
<canvasid="captchaCanvas"height=80>Don't be a dork, Enable JavaScript</canvas>
<buttonid="refresh"name="refresh"onclick="generateCaptcha()">🗘</button>
<inputid="captchaText"class="form-input"name="CAPTCHA"placeholder="Enter text above"maxlength="7"type="text"required="required"aria-describedby="CAPTCHAHelpBlock">
<spanid="CAPTCHAHelpBlock">CAPTCHA Test (Required)</span>
</div>
<divclass="submit">
<buttonid="submit"name="submit"onclick="sendMessage()">Send</button>
</div>
</form>
CSS-ify that shit
Pretty simple Sass code to beautify my form. I am using a theme for my site, from which I am importing _predefined.scss. You can ignore that if you’re not using the same.
This is something I think I need to explain. In the first section, I am taking few variables and one as alphaNums to store alphanumeric string, which shall be used later. Now we also need url, where the message will be sent. In previous article, we have seen how to make backend for the forms. We need that URL for the backend here.
12
contactForm.classList.remove("formdisable");
Here, if JS is loaded, contact Form will be disabled. It is a simple Quality of Life thing for me.
This For loop is used to create 7 alphanumeric string from alphaNums. These characters are placed in random height and random orientation. Last 2 lines are for resetting height and orientation after each character.
If captcha is verified and message length is greater than 20 characters, we take current date from system, disable all the input fields and create a JSON document for the message, which also includes browsers'
userAgent
language
timeZone
Just to verify that a human has indeed sent the message. I could have used browser fingerprinting, but it is a 3rd party package and I don’t want to use your browser for advertising.
After that a POST request is sent to Cloudflare worker, and to make this stuff transparent, submit button is updated.
Thus, you have a proper contact form in your Hugo Page. Use it to your heart’s content.
I know this is a bit rushed and could not provide my usual humor. There is much going on in my life. This is the reason why I could not update the blog for such a long time.