Design Well-Formed HTML for Java or PHP using Gagawa

Gagawa is an HTML Generator library coded in Java and PHP. It allows developers to simply and dynamically build well-formed HTML in web or non-web applications. In Java, Gagawa is particularly useful when earlier HTML generation engines like Java Server Pages (JSP’s) are unavailable. In PHP, Gagawa frees developers from hand-writing HTML in PHP web applications. While you can build a complete site, most developers use it together with other libraries to develop relatively small blocks or components of HTML. By way of example, Gagawa is ideal each time a developer should return a tiny well-formed HTML in the AJAX response.

Gagawa uses Objects to represent each HTML element, or tag. For instance, an element is represented by using a Div object. Similarly, developers can set attributes on these nodes by using a setAttribute() method, or function, per object. An HTML page, or DOM tree, is made by creating some  element objects and appending them together within the correct order.

To change a Gagawa object hierarchy in a String which is often provided for a web-site-browser, the developer should call the write() method, or function, in the top-level element resulting in a well-formed HTML. It simply eliminates missed closing tags and also other mundane HTML typos that frequently haunt web-developers.

To use Gagawa with your Java project, add someJAR files on your Classpath. In Eclipse this implies importing the JAR file into the Java Project and after that adding the JAR on your Build Path select the JAR file, to develop the Path.
To work within your PHP web-application, simply include (require_once preferred) at the top of your PHP application:
require_once(“gagawa.php”)
Below is a detailed example that illustrate how to use Gagawa which show. In both examples shows how to create a

 
Java:
Div div = new Div();
div.setId(“mydiv”).setCSSClass(“myclass”);
A link = new A();
link.setHref(“http://www.example.com”).setTarget(“_blank”);           
div.appendChild( link );          
Img image = new Img( “some alt”, “some-image.png” );
image.setCSSClass( “frame” ).setId( “myimageid” );
link.appendChild( image );
               
System.out.print( div.write() );
This Java code produces the following HTML:

 
 
   some alt
 

PHP
PHP:
<?php
require_once(“gagawa.php”);
$d = new Document( Doctype::HTMLStrict );
$div = new Div();
$div->setId(“mydiv”)->setCSSClass(“myclass”);
$link = new A();
$link->setHref(“http://www.example.com&#8221;)->setTarget(“_blank”);
$div->appendChild( $link );
$image = new Img(“some alt”,”some-image.png”);
$image->setCSSClass(“frame”)->setId(“myimageid”);
$link->appendChild( $image );
$title = new Title();
$title->appendChild( new Text(“Page title!”) );
$d->head_->appendChild( $title );
$d->body_->appendChild( $div );
echo $d->write();
?>
This PHP code produces the following HTML:
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01//EN” “http://www.w3.org/TR/html4/strict.dtd”&gt;
<html>
  <head>
    <title>Page title!</title>
  </head>
  <body>

 
     
        some alt
     
   
  </body>
</html>

Advertisements

Comments are closed.

%d bloggers like this: