Thursday, February 22, 2007

Introducing tvHtmlHelper and TagCreator

I just saw this helper class called pQuery on ajaxian today .written by vikas from delhi ,that offers a very modest way of generating your html code with the necessary attributes for the html elements ,etc .

The good news is that its nice to see someone featured on ajaxian . (both vikas and myself are 23 btw...cheers to that ! ). The bad news ,i knew he could do better .

The API needed a little bit more extensibility and scalability . And thats how i got on a hacking marathon to show how to integrate this class ive made to make ...say a tag creator .Here is vika's code usage :

PHP implementation of pQuery:

  1. =$pquery->form_remote_tag(array('url'=>'index.php?task=ajax','update'=>'#idtoupdate'));?>
  2. Field : "field"
  3. < type = "submit">

My first comments ? Very shabby !

Here my custom built html helper , i write - that would not only go with jQuery ,but heck i use it for spitting out everything from body tags with onload 's , to hr tags , and tables -tr's running in reccursive loops .

Demo of tvhtmlHelper.php:

/**
* @description - A simple Tag creator written in php that uses tvHTMLHelper
* @author Bhasker V Kode
* @date Feb23,2007
* @copyright Some Rights Reserved
*/

class TagCreator extends tvHTMLHelper{
public function
addTag($type=NULL , $tag=NULL){
switch(
$type){
case
'technorati':
/*OUTPUT :will output the html for a tag .
return $this->insert('a','start',array("href"=> "http://www.technorati.com/tags/$tag" ,"rel"=>"tag")) . $tag . $this->insert('a','end') ;
break;
/*
case 'youtubeVideoLink':
// follow similiar style to embed a youtube video , or so on for other widgets of your choice .
*/
}
}


$myInstant = new TagCreator;
echo $myInstant->addTag('html','start');
echo $myInstant->addTag('body','start',array("onload"=>"init();" , "class"="bC");
echo $myInstant->addTag('technorati','tvhtmlhelper');
echo $myInstant->addTag('technorati','bosky101');
echo $myInstant->addTag('technorati','php');
echo $myInstant->addTag('body','end');
echo $myInstant->addTag('html','end');

Notice the following design differences pQuery and tvHTMLHelper:
  1. Inheritance is good for you !
    Primary usage pattern would be to extend this class where you deal with html a lot.
  2. To echo or not to echo !
    If youve noticed ,in pQuery it prints instead of returning . Mine does not echo on its own .but return the html string instead ! .This can be used when you need to generate html to be used later ,like writing concatenating to a string /or sending mail /etc . Being generic never does any harm in helper classes. This i feel is a big pitfall in pQuery .
  3. Clear cut definition boundaries of beginning and ending .
    The API has a startEnd for obvious reasons. This might be a major fallout in pQuery . If you do attempt to seperate presentation from logic , then why make it half way ? do it well or its better off not to do it at all .
  4. Scalability.
    Now tvHtmlHelper does not limit you to jQuery but basically every web page . Heck ,i even use it for a custom build stringToXML class ive written where i could use the API in loops like this :
    • $myInstant->insert('channels','start',array("length"=>1,"foo"=>"bar") );
    • OR links
    • $myInstant->insert('a','start',array("href"=>$link1,"id"=>"foo","class"=>"fadeClass") );
  5. Useful across file types : xml ,html , tagging ,and link generation !
    You could even extend tvHtmlHelper to replace the < > symbols with custom ones or words .like ,what ever you pass through the API will come out as a technorati tag !
Ive put a demo page ,which you can check out . Ive ported tvHTMLHelper to both php4 as well as php5 . Have a look at the sourcecode ( rar file) !

I'd love to get any feedback on this ,or places where you might be able to use this .

Keep Clicking,
Bhasker V Kode



Technorati Tags: , , , , , , , , , ,

1 comment:

Anonymous said...

Hi

Thanx for taking time to review and coming up with problems.Your intial idea about PQuery is wrong , it is not a HTML / Interface Generation library, It is a JQuery Helper library.The tag generation etc is usually taken care by interface/HTML generator classes. So all this is not included in PQuery.For the rest here is what was done -

1. Pquery can be extended easily. Its a class ... extend it.
2 . Echo or not - PQuery returns HTML and does not echo anywhere , not a single echo in the whole class.
3.Start / End - You dont need this in JS.
4. Scalability : Again its JS library.
5. File Types : same as 4.

What you want is a HTML/Form Generation library.Also usually all the HTML is in templates and PQuery only generates JS ( to be executed from AJAX / directly ).