Attributes
GoHT supports more than declaring an id or classes on an element. In Haml and Slim templates, you can declare any attribute you need to on an element.
The attribute syntax is similar to the syntax for a Go map. You use curly braces {} to declare the attributes. Inside the curly braces, you have key-value pairs separated by a colon :. The key is the attribute name, and the value is the attribute value.
EGO templates use normal HTML attributes. The Haml and Slim syntax on this page does not apply inside EGO HTML.
Basic Attributes
renders as:
Complex Attribute Names
Attribute names can be complex. They can contain spaces, dashes, and other characters that are not valid in GoHT. Outside of dashes and underscores, to use any other character that is not alphanumeric, you can use a string.
renders as:
Attribute names cannot be interpolated. GoHT escapes rendered attribute names and rendered attribute values before writing them to the response.
Dynamic Attribute Values
You can also use Go to create dynamic attribute values using the same interpolation syntax as you would for Go evaluation.
renders as:
Boolean Attributes
Boolean attributes are attributes that do not require a value. They are either present or not. To include a boolean attribute, you can do it one of two ways. The first is to simply include the attribute name in the attribute list.
renders as:
The second is to include the attribute name with an interpolated value. If that value evaluates to true at render time, the attribute will be included. We use a question mark ? in place of the colon : to indicate that the attribute is a boolean attribute.
renders as:
Classes
The class attribute is a special type of attribute.
There are multiple sources of classes, and you may want to add classes in one case but not another.
It gets complicated quickly.
GoHT provides multiple ways to add classes to an element.
Class Attribute
We’ve already seen the period . syntax for adding classes. You can also use the class attribute to add classes.
renders as:
Multiple Classes
An interpolated string value can be used to add multiple classes.
renders as:
Multiple Classes with a Slice
The interpolated value can also be a slice of strings to add multiple classes.
renders as:
Conditional Classes
You can also use the class attribute with a map of strings keys to boolean values to add multiple classes conditionally.
renders as:
Combining Class Sources
And all three can be combined.
renders as:
Dynamic Attributes
GoHT also provides an easy way to add dynamic attributes to an element in Haml and Slim templates. You can use the @attributes directive to add a map of attributes to an element. The directive requires the use of an interpolated value. It accepts a list of attributes in two forms. The first is a map of strings to strings which represent attribute names and values, and the second is a map of strings to boolean values which represent attribute names and whether the attribute should be included.
renders as:
Both kinds of maps can be used together.
renders as:
Supported dynamic attribute map types are map[string]string and map[string]bool.