alamin ahmed: a typical geek 2.0

ideas.experiments.thoughts.life

home blog about me

Archive


Topics:


My projects

home » blog

Representing HTML Elements in php object PART 1 (php5)

posted on: Thursday 28th May 2009
In this article, we will create a HTML element wrapper class in php 5, that can represent any HTML tag such as '<p>', '<A>', '<div>'. 

Sometimes this is necessary, especially when are working with MVC design pattern.  This gives Controller class access to View elements for manipulating before that HTML tag/element is rendered.

Here are list of the benefits of wrapping HTML tags in PHP class object:
  1. Various parts of application can access View elements
  2. Work with HTML code as you would for php objects.
  3. Customize and reuse HTML tags
  4. Easier and useful to work with nested and complex tags such as lists (<ul>,<ol>, <dl>), forms (<form>)
  5. Create compound HTML elements, such as a input control with a label.  This requires two different tags, but using php object we can encapsulate them into one object.

How to use HTML element object in php

Lets first look at how we will use the HTML element objects.  Then we will explore how to write the class file.  We will name our class 'HtmlElement'.   

<?php 
// create the paragraph tag 
$p = new HtmlElement('p'); 
$p->align "center"// adding html tag attribute 
$p->append('This is a paragraph.'); // add the inner text of the tag 

// create a div section  
$content = new HtmlElement('div'); 
$content->style 'padding:4px'
$content->id 'main_section'
$content->append($p); // add the p tag previously created. 

// finally outputting 
print $content

// this will output 
// <div style="padding:4px;" id="main_section"><p align="center">This is a paragraph</p></div> 
?>


In the real world, you may not want use this technique to render all of your HTML tags.  However lets say you want plugins or other parts of application to be able to modify and customize the $content element.  In such case you can simply pass the $content variable among various parts of your application. When they are all done, you can then simply render the $content variable.

Things will start to get more interesting when we extend the HtmlElement and start building customized controls and views.  We will look into that in the next part of this series.  Now lets get started with the code.

Coding HtmlElement

HtmlElement will be our base class for rendering all HTML tags.  All compound HTML objects will inherit from this. There are three part in the implementation of the HtmlElement
  1. HTML tag name
  2. attributes for that HTML tag
  3. finally rendering
Let's get started.

class HtmlElement 

    
protected  
        $_attributes 
= array(),    // holds html tag attributes 
        
$_tag null,              // name of the tag 
        
$_content '';            // contents of the tag 

    /** 
     * constructing the tag element 
     * we enforce to specify the tag name when creating to make things logically accurate.   
     */ 
    
public function __construct($tag$content ''$attributes null
    { 
        
$this->_tag $tag
        
$this->_content $content
        
$this->_attributes $attributes
    } 

    
/** 
     * we add given string after current content 
     */ 
    
public function append($str
    { 
        
$this->_content .= $str
    } 

    
/** 
     * will add given string before the content 
     */ 
    
public function prepend($str
    { 
        
$this->_content $str $this->_content
    } 

    
// ... rest of the code here 

 

Construction is self explanatory.  First argument is the tag name which is required.  Second and third arguments are optional.  They are for content of the tag and it's attributes respectively.  We also added two additional methods to add to the content (inner html/text of the tag).  As name implies, append adds string end of the current text and prepend adds in the begining.

So lets started with attributes.  Attributes are implemented using php's magic __get and __set methods and fairly easy to do so.

class HtmlElement 

    
// ... previous codes 
     
    /** 
     * sets an attribute 
     */ 
    
public function __set($key$value
    { 
        
$this->_attributes[$key] = $value
    } 

    
/** 
     * get attribute value 
     */ 
    
public function __get($key
    { 
        if(isset(
$this->_attributes[$key])) return $this->_attributes[$key]; 
        return 
null
    } 

    
/** 
     * returns all attributes as key=value string 
     */ 
    
public function attributeString() 
    { 
        if(empty(
$this->_attributes)) return; 

        
$str ''
        foreach(
$this->_attributes as $key => $value) { 
            
$str .= "{$key}=\"{$value}\" "
        } 
        return 
trim($str); 
    } 

    ... 
// rest of the code 

 
As you can see, in addition to magic function, we created a special method called attributeString().  This method will return all attributes in key=value text format which is used for HTML tags.  When we render the tag to output HTML, we need this method.  So, let's get started with rendering!

Rendering HTML output


class HtmlElement 

    
// ... previous codes 

    /** 
     * render the html tag and returns html string 
     */ 
    
public function render() 
    { 
        
$output  $this->_renderStartTag(); 
        
$output .= $this->_renderInnerHtml(); 
        
$output .= $this->_renderEndTag(); 

        return 
$output;   
    } 

    
protected function _renderStartTag() 
    { 
        
$attributes $this->attributeString(); 
        if(
attributes) { 
            return 
"<{$this->_tag} {$attributes}>"
        } else { 
            return 
"<{$this->_tag}>"
        } 
    } 

    
protected function _renderInnerHtml() 
    { 
        return 
$this->_content
    } 

    
protected function _renderEndTag() 
    { 
        return 
"</{$this->_tag}>"
    } 

    
public function __toString() 
    { 
        return 
$this->render(); 
    } 

 

Ok, lets see what's going on here.  We have divided rendering into four different methods.  First one is render(), which is public, and the rest three are protected methods responsibles for rendering start of the tag, tag content and end of the tag.  You can think of these protected methods as internal events. When subclassing, these events comes very handly.  This will be discussed in next part of this article.

Finally we implemented another php magic function called __toString().  This method simply calls render method.  By implementing this method, we are able to output and treat this object as a string.

There are lots of room for improvement, but to keep things simple and to focus on the concept, this implementation will be sufficient.  Remember this HtmlElement class will be base class for all further UI elements.
Tagged: php   html   programming   1 comment(s)

Some iPhone app sales data.

posted on: Thursday 28th May 2009
Here are some interesting statistics about iPhone app sales you might like to know. 

Business Applications on Top Paid apps:
  • Ranked top 10-15 paid app will sale about 70 a day
  • Top 25-40 ~ about 35-50 a day.
  • Top 40-55 ~ about 15-20 a day.
  • Top 60-75 ~ about 4-10 a day.

Productivity Applications:
  • Top 30-40 ~ 40-60 a day

Utilities Appliation
  • Top 40 - 50 ~ 70-100 a day

*These data varies from weekdays to weekend and holidays.  Also Productivity and Utilities data are about two months old.
Tagged: iPhone   money   developers   Add comment

Task PRO v1.1 for iPhone - features & video

posted on: Friday 22nd May 2009
Task PRO is truly an amazing task management application for the iPhone.  Task PRO supports sub-tasking, a powerful mechanism to hierarchically organize task/information.  Task PRO also has a very clean & elegant design, and is extremely flexible in nature.  Since version 1.0, there has been tons of emails with positive comments/feedbacks that lifted our spirits. However, there were many feature requests also.

So, after little over a month of initial release, Task PRO v1.1 was released on first week of May.  Almost 70% of the user requested features were implemented.  Lets see whats included and then we can see what left out and when to expect them.

v1.1 features:

  1. Allows to manually sort task lists.  This option is found under sort/group menu.
  2. Application icon badge with due/overdue count.
  3. Lists  show hierarchy levels.
  4. Lists by priority - "high", "normal" or "low".
  5. Now you can add due time along with date.  This will help track daily tasks.
  6. Ability to delete completed tasks
  7. Swipe-to-delete feature.
  8. As may requested, we also added additional symbols/icons for the tag.
  9. Emailing tasks will also include sub tasks in nice formatted text (HTML formatting used)
  10. Application setting allows to change some of the default behaviors.

Clearly this was a huge update. 

Here is a quick video of Task PRO v1.1




We are currently working on version 1.2 to wrap up rest of the requested features.  Some of the notable features will be:
  1. Repeat/duplicate feature.  (One of the most requested).
  2. Desktop application to quickly view/add/edit tasks.  This should work with both Mac and Windows.
  3. Backup/restore database.
  4. Full Toodledo syncing with supporting more fields.

Version 1.2 is expected to be released by early July.


Tagged: iPhone   iPhone apps   Task PRO   Add comment

Windows 7 RC shows Microsoft finally understands Apple

posted on: Wednesday 13th May 2009
So far Microsoft didn't get Apple.  It simply tried to copy some of the key features that makes Apple unique.  But implemented in "Microsoft way", that is unattractive, and boring.

Finally it seems like Microsoft's engineers got what Apple is about.  They got the trick.  Windows 7 completely reflects that.  I simply can't point my finger on it, but there is something about Windows 7 that makes it feel right, elegant and beautiful.  Something that only Apple was able to do.  This is not simply copying Apple, but also understanding what makes Apple different and appealing to users.


source: microsoft.com


Microsoft engineers has done excellent job focusing on task bar.  Yes, task bar reflects Apple's dock and "steals" lots of great ideas. However Microsoft's little twists some how makes it bit better.  Microsoft paid attention to even smaller details such as showing downloading progress bar, thumbnail image of running application when hovering... things that traditionally only Apple would do, and does.

What really made me believe that Microsoft is unto something is when I saw the library of built-in background images provided with Windows 7.  In one word - astonishing

If you still haven't tried yet, go ahead download (http://www.microsoft.com/Windows/Windows-7/download.aspx ) and install Windows 7 RC.  It is available for download until July.  And rumor has it, it will be released in late this year.

Tagged: Microsoft   Apple   Windows 7   Add comment

Signed up for Microsoft Market place for Windows Mobile

posted on: Wednesday 13th May 2009

Following Apple's footstep (tell me something new) Microsoft has created Windows Mobile Marketplace for developers.  Offers similar business model such as $100/year, 70/30 sales cut as well as similar application development and distribution policies.

I just signed up.  Payed $100.  Gave my company (Alifsoft) and banking information.  Now it turns out, I need to purchase Windows Visual Studio  to develop app.  Visual Studio Express (free) edition will not work.  Hmm.
Tagged: Microsoft   Windows Mobile   Add comment

copyright© 2008 alaminahmed.com || powered by me! | v0.40