📜  kotlin fabric - Html (1)

📅  最后修改于: 2023-12-03 15:02:32.098000             🧑  作者: Mango

Kotlin Fabric - Html

Kotlin Fabric - Html is a Kotlin library that provides an easy-to-use API for generating HTML code programmatically. With this library, you can create HTML pages, templates, and fragments using Kotlin code instead of writing HTML by hand.

Features

Some of the features of Kotlin Fabric - Html include:

  • Easy-to-use API for generating HTML code
  • Support for HTML5
  • Support for custom HTML tags and attributes
  • Support for internationalization
  • Support for templating
Getting Started

To start using Kotlin Fabric - Html, you first need to add the library to your project's dependencies. You can do this by adding the following lines to your build.gradle file:

repositories {
    mavenCentral()
}

dependencies {
    implementation 'io.github.kotlinfabric:kotlin-fabric-html:0.1.0'
}

Once you have added the library to your project, you can start using it to generate HTML code. Here is a simple example:

val html = html {
    head {
        title { +"Hello, World!" }
    }
    body {
        h1 { +"Hello, World!" }
        p { +"This is my first HTML generated with Kotlin Fabric - Html." }
    }
}

println(html)

This will generate the following HTML code:

<!DOCTYPE html>
<html>
    <head>
        <title>Hello, World!</title>
    </head>
    <body>
        <h1>Hello, World!</h1>
        <p>This is my first HTML generated with Kotlin Fabric - Html.</p>
    </body>
</html>
Custom Tags and Attributes

One of the powerful features of Kotlin Fabric - Html is the ability to define custom HTML tags and attributes. Here is an example:

object CustomTags {
    val myTag = Tag("my-tag", emptyMap(), true)
}

object CustomAttributes {
    val myAttr = Attribute("my-attr")
}

val html = html {
    head {
        title { +"Custom Tags and Attributes" }
    }
    body {
        CustomTags.myTag {
            CustomAttributes.myAttr("custom value")
        }
    }
}

println(html)

This will generate the following HTML code:

<!DOCTYPE html>
<html>
    <head>
        <title>Custom Tags and Attributes</title>
    </head>
    <body>
        <my-tag my-attr="custom value"></my-tag>
    </body>
</html>
Templating

Kotlin Fabric - Html also supports templating, which allows you to reuse HTML fragments across multiple pages. Here is an example:

fun main() {
    val template = htmlTemplate("Template") {
        body {
            div("content") {}
        }
    }

    val page1 = html {
        head {
            title { +"Page 1" }
        }
        body {
            insertTemplate(template) {
                find { it.classes.contains("content") }?.content {
                    p { +"This is the content of page 1." }
                }
            }
        }
    }

    println(page1)

    val page2 = html {
        head {
            title { +"Page 2" }
        }
        body {
            insertTemplate(template) {
                find { it.classes.contains("content") }?.content {
                    p { +"This is the content of page 2." }
                }
            }
        }
    }

    println(page2)
}

This will generate the following HTML code for page1:

<!DOCTYPE html>
<html>
    <head>
        <title>Page 1</title>
    </head>
    <body>
        <div class="content">
            <p>This is the content of page 1.</p>
        </div>
    </body>
</html>

And this will generate the following HTML code for page2:

<!DOCTYPE html>
<html>
    <head>
        <title>Page 2</title>
    </head>
    <body>
        <div class="content">
            <p>This is the content of page 2.</p>
        </div>
    </body>
</html>
Conclusion

Kotlin Fabric - Html is a powerful library that makes generating HTML code programmatically much easier. It provides an easy-to-use API for creating HTML pages, templates, and fragments, and supports a wide range of features, including custom tags and attributes and templating. If you're looking for a way to generate HTML code from Kotlin, give Kotlin Fabric - Html a try!