If you like my work, please consider supporting its development.

Plugins for Nebula Forms may allow the use of templates to add some logic to various parameters based on the contents of the form submission.

How Templates Work

Templates are specially formatted text that use {{ }} characters to indicate that special processing should happen between the braces. This allows for some variation in the output of the template, based on certain logic.

The best resource for the features of the templating system is its official documentation. The documentation goes over the in-built logical constructs and syntax of templates, so reading it is a prerequisit for the following sections, which go over some of the functionality added by Nebula.

Template Functions

Nebula comes with a few template functions available for use by all plugins. Below you can find descriptions of each one and how to use them, with examples.

It is up to each plugin developer to make these functions available in their plugin. If you cannot use one or more of these functions in a plugin, report it to the plugin developer, not Nebula Forms.

Errorf

Errorf allows you to throw custom errors that will get returned to the user. This is your biggest tool when it comes to form validation and should be used in (nearly) every template in conjunction with other functions for validating inputs.

In the below example, Errorf is used to tell the user that they must enter an email address.

{{ with (FormValue "email") }}
    {{ . }}
{{ else }}
    {{ Errorf "An email address is required." }}
{{ end }}

with is a special template action that is equivalent to using an if to see if the thing has a non-empty value, then sets the current context (the .) equal to that value. So the above code prints the contents of the email address field if it has any, or returns an error to the user saying they need to enter something.

FormValue

The FormValue function allows you to retrieve a single value from a form field and do something with it.

HTML forms technically allow for multiple values for each field, like with checkboxes. It could also be done with text input fields by having multiple fields with the same name. FormValue returns the first value and ignores any others.

The below example prints a greeting based on the contents of the input field named “name”.

Hello, {{ FormValue "name" }}!

FormValues

The FormValues function returns all values associated with a named form input as a Go slice (think list or dynamic array). These values can then be iterated over using range.

Your favorite colors are: {{ range (FormValues "fav_colors") }}
 - {{ . }}{{ end }}

This is particularly useful if the form uses a collection of checkboxes or perhaps a multiselect field - anything allowing one field to have multiple values.