Not a recommended or very flexible approach but if you need to create a html document using groovy MarkupBuilder . A couple of real life examples might be if you are generating an email, creating a simple html page for a demo or injecting a snippet into a parent template.
Html MarkupBuilder @Test
public void generate_html_with_markupbuilder () {
def writer = new StringWriter ()
def html = new MarkupBuilder ( writer )
html . html {
head {
title: "Creating html document with groovy"
script: "alert('hello');"
}
body ( id: "main" ) {
h2 id: "book-mark" , "Tutorial on how to generate html with groovy"
p {
mkp . yield "Mixing text with"
strong "strong"
mkp . yield " elements."
}
a href: "http://www.leveluplunch.com/java/tutorials/" , "Java tutorials"
}
}
println writer
}
Output
<html>
<head />
<body id= 'main' >
<h2 id= 'book-mark' > Tutorial on how to generate html with groovy</h2>
<p> Mixing text with
<strong> strong</strong> elements.
</p>
<a href= 'http://www.leveluplunch.com/java/tutorials/' > Java tutorials</a>
</body>
</html>
Generate html with markupbuilder posted by Justin Musgrove on 03 November 2014
Tagged: groovy and groovy-html
Share on: Facebook Google+
All the code on this page is available on github:
GenerateHtmlWithMarkupBuilder.groovy