renderSnake – Better Way to create HTML

We live in an age where everything is open and innovations spring out every second. All those algorithms, all those concepts and all those frameworks are easily avalable to he who is willing to look. Unless you are not staring down from the tip of the cutting edge, you can pretty much get help on any problem. If someone had a similar problem before and you have the solution available, why re-invent the wheel..

When I was in the task of creating an HTML report generating tool, I did not have to look much. Found the perfect framework – renderSnake. renderSnake is a component based HTML generation framework. Its purpose is to support the creation of Web applications that are better maintainable, allows for easier reuse, have testable UI components and produces compact HTML in an efficient way. Let’s start off with an example.

So, let’s create a plain old HTML.

<h4>Two rows and three columns:</h4>
<table border="1">
<td>Haruki Murakami</td>
<td>Gregory Roberts</td>
<td>A Wild Sheep Chase</td>

And the java code to create the HTML 

HtmlCanvas html = new HtmlCanvas();
    .content("Two rows and three columns:")
        .content("Haruki Murakami")
        .content("Gregory Roberts")
        .content("A Wild Sheep Chase")


Using a Canvas, you can write code that produces HTML. The Canvas has methods for all HTML tags. There are two methods for writing an opening tag, one without and one with attributes. The Canvas maintains a stack of nested tags. Proper closing of tags is required, according to the rules of HTML (some end tags are required, some are optional and some are forbidden).

Each tag method returns the Canvas instance, allowing for a fluent programming interface. Using source indentation you can visually verify the correct structure. In the above example, the variable html is an instance of HtmlCanvas.


Using an HtmlAttributes instance, you can add attribtues to tags such as href, class and id. The Java classes HtmlAttributes and HtmlAttributesFactory provides convenience methods for creating instances. This allows for a fluent programming interface.

//This one is using HtmlAttributes :

html.body(new HtmlAttributes("key","value")); 

//This one is using the HTMLAttributesFactory

                .img(src("picture.png").alt("some picture")) 

All the attributes for a given tag can be set using the corresponding method. In the above example, the href, img and alt properties are set.

I am yet to find a limitation for renderSnake. You can get more examples here and here..

3 thoughts on “renderSnake – Better Way to create HTML

  1. Pingback: Comparing .csv files and generate sweet HTML reports in rendersnake | Super Compiler

  2. can i use to generate text.html from rendersnake and how can i run it
    because i write “html.render(new ViewPages())” and not thing was happened

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s