2017-09-25 3 views
0

Dans Kotlin page, sous HTML-Builder Je peux voir le code ci-dessous, comment puis-je l'utiliser dans un simple fichier .tk? comment commencer ici?Kotlin HTML-Builder

val data = mapOf(1 to "one", 2 to "two") 

createHTML().table { 
    for ((num, string) in data) { 
Iterate over data 
     tr { 
Functions to create HTML tags 
      td { +"$num" } 
      td { +string } 
     } 
    } 
} 
+0

Votre lien ne pointe même pas koans. Vous cherchez [celui-ci] (https://try.kotlinlang.org/#/Kotlin%20Koans/Builders/Html%20builders/Task.kt). Mais remarquez le fichier [html.kt] (https://try.kotlinlang.org/#/Kotlin%20Koans/Builders/Html%20builders/html.kt) sur la gauche. – chris

+0

Cet exemple vient des Koans, mais vous cherchez peut-être [la bibliothèque 'kotlinx.html'] (https://github.com/Kotlin/kotlinx.html). – mkobit

+0

@chris toujours pas comment faire, il n'y a pas de fonction «principale», comment appeler cela, et h comment il sera affiché. –

Répondre

2

Vous faites référence à un DSL écrit en HTML Kotlin pour la construction par le constructeur. La bibliothèque se trouve ici: https://github.com/Kotlin/kotlinx.html

Voici un exemple en cours d'exécution:

fun main(args: Array<String>) { 
    val document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument() 
    val html = document.create.html { 
     head { 
      title("Hello world") 
     } 
     body { 
      h1("h1Class"){ 
       +"My header1" 
      } 
      p("pClass"){ 
       +"paragraph1" 
      } 
     } 
    } 

    intoStream(html, System.out) 
} 

fun intoStream(doc: Element, out: OutputStream) { 
    with(TransformerFactory.newInstance().newTransformer()){ 
     setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no") 
     setOutputProperty(OutputKeys.METHOD, "xml") 
     setOutputProperty(OutputKeys.INDENT, "yes") 
     setOutputProperty(OutputKeys.ENCODING, "UTF-8") 
     setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4") 
     transform(DOMSource(doc), 
       StreamResult(OutputStreamWriter(out, "UTF-8"))) 
    } 
} 

Et enfin voici la sortie correspondante:

<?xml version="1.0" encoding="UTF-8"?><html> 
<head> 
    <title>Hello world</title> 
</head> 
<body> 
    <h1 class="h1Class">My header1</h1> 
    <p class="pClass">paragraph1</p> 
</body> 
</html> 
+0

Toujours pas eu comment faire, pouvez-vous donner un exemple complet, merci. –

+0

J'ai ajouté un exemple – s1m0nw1

+0

Merci, pouvez-vous aider ici: https://stackoverflow.com/questions/46455185/handling-freemaker-template-with-ktor-kotlin –