|
This is a basic tutorial on generating HTML using Hypirinha.
First stepsAdd hypirinha to your classpathDownload the latest release version from the downloads tab. Add it to your application's classpath. Hypirinha has no runtime dependencies other than what comes as standard with Java 5 or above, so you don't need to download anything else. However, you may find it useful to also download the source jar and configure your IDE to use it when you drill down into the compiled classes. Import some classesChoose a new or existing class and add the following imports to the top of the file. These wildcard imports cover all of the types and methods in the rest of this page. import static org.hypirinha.html.AttributeFactory.*; import static org.hypirinha.html.ElementFactory.*; import org.hypirinha.html.elements.*; import org.hypirinha.html.output.PrintAdapter; Create an elementThe first step in creating markup is to create a root element. For convenience, all elements can be created standalone using one of the static methods on ElementFactory. For example: Html html = html(); Choose an adapterAfter creating to some markup, you need to do something with it. Hypirinha objects need translating before they are useful in the outside world. Currently two adapter classes are provided for this purpose:
For example, to send text to stdout, use code like this: new PrintAdapter().print(html(), System.out); Hello worldWith all these steps completed, we can now create a very simple document: import static org.hypirinha.html.AttributeFactory.*;
import static org.hypirinha.html.ElementFactory.*;
import org.hypirinha.html.elements.*;
import org.hypirinha.html.output.PrintAdapter;
public class HelloWorldCommandLine {
public static void main(String[] args) {
Html html = html();
html.head().link(rel("stylesheet"), href("styles.css"));
html.body().p(classs("greeting")).text("Hello World");
new PrintAdapter().print(html, System.out);
}
}produces on stdout: <html>
<head>
<link rel="stylesheet" href="styles.css"></link>
</head>
<body>
<p class="greeting">Hello World</p>
</body>
</html>Building a documentIf you can follow the examples below, you will understand the whole of the Hypirinha API. Combining these techniques you'll be able to produce any HTML structure you like. Calling a method on an element creates a child element and returns that new child elementHtml html = html(); html.body().table().tbody().tr().td().span(); html renders to: <html>
<body>
<table>
<tbody>
<tr>
<td>
<span></span>
</td>
</tr>
</tbody>
</table>
</body>
</html>Parameters passed to any method call are attributes for the element being createdDiv div = div(id("menuContainer1"), classs("menuContainer"));
div.ul(classs("menuItems"));div renders to: <div id="menuContainer1" class="menuContainer"> <ul class="menuItems"></ul> </div> Special method text adds text node to elementTitle title = title();
title.text("Important Page");title renders to: <title>Important Page</title> Special method contains adds existing elements as childrenTr tr = tr().contains(th(), th()); tr renders to: <tr> <th></th> <th></th> </tr> Iteration works well with standard java control sructuresint[] domain = new int[]{1, 2, 3, 4, 5};
Table table = table();
table.tr().contains(th().text("x"), th().text("x + 2"));
for (int x : domain) {
table.tr().contains(td().text(valueOf(x)), td().text(valueOf(x + 2)));
}table renders to: <table>
<tr>
<th>x</th>
<th>x + 2</th>
</tr>
<tr>
<td>1</td>
<td>3</td>
</tr>
<tr>
<td>2</td>
<td>4</td>
</tr>
<tr>
<td>3</td>
<td>5</td>
</tr>
<tr>
<td>4</td>
<td>6</td>
</tr>
<tr>
<td>5</td>
<td>7</td>
</tr>
</table>Elements work well as method return valuesclass AddTwo {
Tr headerRow() {
return tr().contains(th().text("x"), th().text("x + 2"));
}
Tr bodyRow(int x) {
return tr().contains(td().text(valueOf(x)), td().text(valueOf(x + 2)));
}
}
int[] domain = new int[]{1, 2, 3, 4, 5};
AddTwo function = new AddTwo();
Table table = table();
table.contains(function.headerRow());
for (int x : domain) {
table.contains(function.bodyRow(x));
}table renders to: <table>
<tr>
<th>x</th>
<th>x + 2</th>
</tr>
<tr>
<td>1</td>
<td>3</td>
</tr>
<tr>
<td>2</td>
<td>4</td>
</tr>
<tr>
<td>3</td>
<td>5</td>
</tr>
<tr>
<td>4</td>
<td>6</td>
</tr>
<tr>
<td>5</td>
<td>7</td>
</tr>
</table>
|