Giter VIP home page Giter VIP logo

h's Introduction

h

Build Status Codecov branch release License

Html templating library for kotlin.

Get started

Download

  • Download the latest release. (View releases)
  • Clone the repo for the source code git clone https://github.com/phenax/h.
  • Test it with ./gradlew test. Generated test report -> build/reports/tests/test.

Examples

Simple component examples in /examples

Create a component

import io.github.phenax.h.Component
import io.github.phenax.h.Layout
import io.github.phenax.h.node.DOMNode
import io.github.phenax.h.layouts.EmptyLayout

class CardComponent(val myTitle: String): Component() {

  // Layout(EmptyLayout is no wrapper. You can use a custom layout)
  override val layout = EmptyLayout()

  // This renders a div card component
  // <div class="card">
  //   <h1 class="card--title">Card title</h1>
  //   <p class="card--description">Card description</p>
  // </div>
  override fun render(): DOMNode {
    return div( mapOf( "class" to "card" ),
      listOf(
        h1( mapOf( "class" to "card--title" ), myTitle),
        p( mapOf( "class" to "card--description" ), "Card description" )
      )
    )
  }
}

val helloWorldCard = CardComponent("Hello world")

OR

import io.github.phenax.h.*

fun createCard(myTitle: String) = component {
  div( mapOf( "class" to "card" ),
    listOf(
      h1( mapOf( "class" to "card--title" ), myTitle),
      p( mapOf( "class" to "card--description" ), "Card description")
    )
  )
}

val helloWorldCard = createCard("Hello world")

Create a layout

import io.github.phenax.h.Component
import io.github.phenax.h.Layout
import io.github.phenax.h.node.DOMNode

class HtmlLayout(val title: String = "Moosic"): Layout() {
  override fun render(component: AbstractComponent): DOMNode {
    return (
      html(null, listOf(
        head(null, listOf(
          h("title", null, listOf( text(title) ) ),
          style("/css/style.css"),                               // External stylesheet
          style(null, "html, body { background-color: red; }")   // Inline style
        )),
        body(null, listOf(
          div(null, h(component)),
          script("/js/script.js", mapOf( "defer" to "defer", "async" to "async" ))
        ))
      ))
    )
  }
}

Use components inside other components

class UserCardComponent(user: User): Component() {
  override val layout = EmptyLayout()
  override fun render(): DOMNode {
    return div(null, listOf(
      div(null, listOf( text(user.name) )),
      div(null, listOf( text('@' + user.nickname) )),
    ))
  }
}

class UserListComponent(usersList: List<User>): Component() {
  override val layout = EmptyLayout()
  override fun render(): DOMNode {
    return div(null,
      usersList.map { user ->
        div(null, listOf( h(UserCardComponent(user)) ))
      }
    )
  }
}

Render to html string

val component = CardComponent()
println(component.renderToHtml())

h's People

Contributors

phenax avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.