All Projects → celtric → kotlin-html

celtric / kotlin-html

Licence: Apache-2.0 License
A library to generate HTML in Kotlin.

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to kotlin-html

Giraffe.Razor
Razor view engine http handlers for Giraffe web applications.
Stars: ✭ 27 (+17.39%)
Mutual labels:  template-engine
engine
A pragmatic approach to templating for PHP 7.x+
Stars: ✭ 31 (+34.78%)
Mutual labels:  template-engine
shaven
DOM building utility & Template engine based on JsonML + syntax sugar
Stars: ✭ 66 (+186.96%)
Mutual labels:  template-engine
voldemort
A simple static site generator using Jinja2 and Markdown templates.
Stars: ✭ 48 (+108.7%)
Mutual labels:  template-engine
colon
Minimal, concise and blazing fast template engine.
Stars: ✭ 43 (+86.96%)
Mutual labels:  template-engine
Hierarchy
No-dependencies package that embodies WordPress template hierarchy
Stars: ✭ 78 (+239.13%)
Mutual labels:  template-engine
LiquidKit
Liquid template language parser engine in Swift.
Stars: ✭ 19 (-17.39%)
Mutual labels:  template-engine
idris-tmustache
Total Logic-Less Templating Library
Stars: ✭ 12 (-47.83%)
Mutual labels:  template-engine
karkas
A tiny template engine based on TypeScript
Stars: ✭ 14 (-39.13%)
Mutual labels:  template-engine
view
Template Engine For AdonisJS
Stars: ✭ 13 (-43.48%)
Mutual labels:  template-engine
gktemplate
GKTemplate - 采用Go开发的DedeCMS模板解析器
Stars: ✭ 32 (+39.13%)
Mutual labels:  template-engine
typed-html
TypeSafe HTML templates using TypeScript. No need to learn a template library.
Stars: ✭ 92 (+300%)
Mutual labels:  template-engine
pyxl4
Extend Python syntax with HTML.
Stars: ✭ 68 (+195.65%)
Mutual labels:  template-engine
Glize
📚 Glize is a clean and robust pure Javascript library.
Stars: ✭ 16 (-30.43%)
Mutual labels:  template-engine
typesafe-templates
Template engine that leverages JSX to generate JavaScript code from TypeScript code files rather than text templates.
Stars: ✭ 27 (+17.39%)
Mutual labels:  template-engine
liquidpy
A port of liquid template engine for python
Stars: ✭ 49 (+113.04%)
Mutual labels:  template-engine
django-mustache
Mustache (Pystache) template engine for Django 1.8 and newer, with support for Django context processors. Designed to support offline-capable web apps via progressive enhancement.
Stars: ✭ 20 (-13.04%)
Mutual labels:  template-engine
MulleScion
🌱 A modern template engine for Objective C
Stars: ✭ 14 (-39.13%)
Mutual labels:  template-engine
lua-resty-aries
openresty and lua multi-function template
Stars: ✭ 47 (+104.35%)
Mutual labels:  template-engine
korte
Kotlin cORoutines Template Engine for Multiplatform Kotlin
Stars: ✭ 69 (+200%)
Mutual labels:  template-engine

CircleCI License Download

kotlin-html

kotlin-html is a library to generate HTML in Kotlin.

This library is heavily inspired by kotlinx.html, but with a strong emphasis on context-independent composability.

Getting started

You can define HTML elements independently from an HTML document. The following code:

p("A paragraph")

will render the following HTML when .render() is called:

<p>A paragraph</p>

Note: all HTML examples in this README are generated after calling .render() on the expression presented in Kotlin. It's encouraged that you keep the number .render() calls to the minimum and instead combine elements to increase your code's expressiveness.

Composability

You can join any number of elements:

p("First paragraph") + p("Second paragraph")

HTML:

<p>First paragraph</p>
<p>Second paragraph</p>

You can also easily interact with list transformations:

val people = listOf("Gael", "Laura", "Ricard")

ul { people.map { li(it) } }

HTML:

<ul>
    <li>Gael</li>
    <li>Laura</li>
    <li>Ricard</li>
</ul>

Working with functions is also straightforward:

ul { numbers() }

fun numbers() = li("One") + li("Two") + li("Three")

HTML:

<ul>
    <li>One</li>
    <li>Two</li>
    <li>Three</li>
</ul>

Attributes

Attributes are optional. If you wish to define them, you must use a block for the content:

p("A paragraph") // Text-only element, cannot contain attributes
p { "A paragraph" } // Equivalent to previous line
p(classes = "a-class") { "A paragraph" } // Same paragraph with "a-class" class

HTML

<p>A paragraph</p>
<p>A paragraph</p>
<p class="a-class">A paragraph</p>

Each element offers some relevant attributes depending on its tag:

a(href = "http://www.example.com/", target = "_blank") { "A link" }
img(src = "/img/logo.png", alt = "Our site")

HTML

<a href="http://www.example.com/" target="_blank">A link</a>
<img src="/img/logo.png" alt="Our site">

In case you wish to define other attributes you can do so using other:

div(other = mapOf("key" to "value")) { "Content" }

HTML

<div key="value">Content</div>

Custom data attributes are also supported:

div(data = mapOf("user" to "celtric")) { "Content" }

HTML

<div data-user="celtric">Content</div>

Text

You can join elements and text:

strong("Strongly") + " disagree"
p(strong("Strongly") + " disagree")

HTML:

<strong>Strongly</strong> disagree
<p><strong>Strongly</strong> disagree</p>

Due to a limitation in Kotlin's overloading capabilities, a native string cannot be the first element in a list:

"foo" + strong("bar") // NOT allowed by Kotlin operator overloading
text("foo") + strong("bar") // Valid Kotlin
strong("foo") + "bar" // Valid Kotlin, as the native string is not the first element

Full example

import org.celtric.kotlin.html.*

fun main(args : Array<String>) {
    val document = doctype("html") + html {
        head {
            title("Document title") +
            meta(charset = "utf-8") +
            link(href = "css/style.css", rel = "stylesheet") +
            script(type = "text/javascript", src = "js/script.js")
        } +
        body {
            div(classes = "container") {
                h1("A title") +
                p(classes = "introduction") {
                    "A paragraph"
                } +
                ul {
                    li(a("http://www.example.com/", "A link")) +
                    li(a("http://www.example.com/", "A second link"))
                }
            }
        }
    }

    print(document.render())
}

HTML:

<!DOCTYPE html>
<html>
    <head>
        <title>Document title</title>
        <meta charset="utf-8">
        <link href="css/style.css" rel="stylesheet">
        <script type="text/javascript" src="js/script.js"></script>
    </head>
    <body>
        <div class="container">
            <h1>A title</h1>
            <p class="introduction">A paragraph</p>
            <ul>
                <li><a href="http://www.example.com/">A link</a></li>
                <li><a href="http://www.example.com/">A second link</a></li>
            </ul>
        </div>
    </body>
</html>
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].