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.

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

And the java code to create the HTML 

HtmlCanvas html = new HtmlCanvas();
html
 .html()
  .body()
   .h4()
    .content("Two rows and three columns:")
     .table(border("1"))
      .tr()
       .td()
        .content("Haruki Murakami")
       .td()
        .content("Gregory Roberts")
      ._tr()
      .tr()
       .td()
        .content("A Wild Sheep Chase")
       .td()
        .content("Shantaram")
      ._tr()
     ._table()
    ._body()
  ._html();

HTMLCanvas

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.

HtmlAttributes

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

.a(href("http://goto.com")) 
                .img(src("picture.png").alt("some picture")) 
                ._a(); 

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..

Advertisements

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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s