Dynamic/Custom attributes of tags in kotlinx.html Builders


When using toolkits like SemanticUI ( https://semantic-ui.com ) they introduce custom attributes in otherwise seemingly HTML tags. eg data-tab in case of tab panel (expand the very first example at https://semantic-ui.com/modules/tab.html )

<div class="ui top attached tabular menu">
  <a class="active item" data-tab="first">First</a>
  <a class="item" data-tab="second">Second</a>
  <a class="item" data-tab="third">Third</a>
<div class="ui bottom attached active tab segment" data-tab="first">
<div class="ui bottom attached tab segment" data-tab="second">
<div class="ui bottom attached tab segment" data-tab="third">

or data-value in case of a drop down (expand example titled Selection at https://semantic-ui.com/modules/dropdown.html )

<div class="ui selection dropdown">
  <input name="gender" type="hidden">
  <i class="dropdown icon"></i>
  <div class="default text">Gender</div>
  <div class="menu">
    <div class="item" data-value="1">Male</div>
    <div class="item" data-value="0">Female</div>

Is there a way to reuse the kotlinx.html DSL builders but pass in the custom attribute name value pairs? If not what would be a good compact way to extend the DSL elements in kotlinx.html?


I have personally not tried this, but as far as I understand each Html tag has a property called attributes which is a mutable map. You should be able to add any attribute you want to it. For attributes you use often I suggest you override the div function to create a div tag with the attributes set ( adjust for whichever tag you nee)

fun <T, C : TagConsumer<T>> C.div(classes : String? = null, yourAttribute: String?, block : DIV.() -> Unit = {}){
    this.div(classes) {
        attributes.put("yourAttribute", yourAttribute)


Thank you very much … that solved it. eg.

a("active item") {
    attrs["data-tab"] ="first"