Skip to content

Enum attributes

Shreck Ye edited this page Apr 12, 2022 · 4 revisions

Enum attributes

There are attributes with predefined list of possible values. For example form.method. For such attributes there are corresponding enum classes generated:

form(method = FormMethod.post) {
    // input fields goes here
}

However there are sort of attributes that could have both custom and predefined values. A typical example is target attribute of <A> tag. In such cases builder function parameter has type String and there are constants declared

a(target = "myCustomValue") { + "..." }

a(target = ATarget.blank) { + "...." } 

Tags with type shortcuts

Sometimes an attribute denotes a tag type. For example an input field could look like input(type = InputType.text, name = "myField"). Unfortunately this may look boring if there are a lot of input fields in a form. Consider:

form(action = "/form", encType = FormEncType.multipartFormData, 
                       method = FormMethod.post) {
    input(type = InputType.text, name = "field1")
    input(type = InputType.text, name = "field2")
    input(type = InputType.checkBox, name = "field3")
    input(type = InputType.text, name = "field4")
    input(type = InputType.file, name = "field5")
    input(type = InputType.text, name = "field6")
}

The reason why it is so boring is that it is difficult to read what input fields do we have. To get rid of this there are shortcuts generated so we can rewrite the example above as follows:

form(action = "/form", encType = FormEncType.multipartFormData, 
                       method = FormMethod.post) {
    textInput(name = "field1")
    textInput(name = "field2")
    checkBoxInput(name = "field3")
    textInput(name = "field4")
    fileInput(name = "field5")
    textInput(name = "field6")
}