CodeIgniter Forums
Create HTML Elements - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: Create HTML Elements (/showthread.php?tid=15978)

Pages: 1 2


Create HTML Elements - El Forum - 02-21-2009

[eluser]thinkigniter[/eluser]
View orginal script at
[url="http://davidwalsh.name/create-html-elements-php-htmlelement-class]davidwalsh.name/create-html-elements-php-htmlelement-class[/url]

/**
* element Class
* Original concept/script David Walsh
* Converted/modified to CI class by Donn Hill on 21-02-09
* Function:
*
*
* @author David Walsh <http://davidwalsh.name>
* @converted to CI Class Donn Hill <http://thinkigniter.com>
* @version 0.8
* @copyright 2009
*/

How this Class works
When you instantiate the class, you feed it the element type.
Some instructions
Code:
$a = new element('a');
Now you can "set" and "get" attributes to you "<A>" tag.
Code:
$a = new element('a');
$a->set("href","http://codeigniter.com/");
$a->text("CodeIgniter Home");
$a->output();
/* <a href="http://codeigniter.com/">CodeIgniter Home</a> */
You use get() to retrieve values and set() (key and value OR array key=>value) to set values.
You can get rid of a specified attribute using remove() or all attribute key => values using clear().
You can combine html_elements into other html_elements by using...
Code:
$p = new element('p');
$a->set("href","http://codeigniter.com/");
$a->text("CodeIgniter Home");
$p->set( 'class','nice_p');
$p->text( 'This is a great site' );
$p->text( $a );
$p->output();
/*<p class="nice_p">This is a great site<a href="http://codeigniter.com/">CodeIgniter Home</a></p> */
Yes, It recognises objects.

Html_test.php
Code:
&lt;?php
class Html_test extends Controller {

    function html_test(){
        parent::Controller();
        $this->load->library( array('element') );
    }
    
    function index(){
        
        // Create elements in bulk
        $elements = array('table','td','tr','a','form','input');
        foreach( $elements as $element )
        {
            $$element = new element($element);
        }

        /* Create codeigniter search feature*/
        $table->set( array("cellpadding"=>"0",
                           "cellspacing"=>"0",
                           "border"=>"0",
                           "style"=>"width:100%")
                    );
        
        $a->set( "href","http://codeigniter.com/");
        $a->text('CodeIgniter Home');
        
        $td->set( 'id',"breadcrumb" );
        // Build '<A>' tag to '<TD>' tag
        $td->text($a);
        // Clear '<A>' tag for another go        
        $a->clear();
        
        $a->set("href","../index.html");
        $a->text('User Guide Home');    

        $td->text($a);
        
        $td->text('Element Class');
        
        $tr->text($td);
        $td->clear();
        
        $td->set("id","searchbox");
        $form->set( array("method"=>"get",
                          "action"=>"http://www.google.com/search")
                   );
        
        $input->set( array("type"=>"hidden",
                           "name"=>"as_sitesearch",
                           "id"=>"as_sitesearch",
                           "value"=>"ellislab.com/codeigniter/user-guide/")
                    );
        
        $form->text("Search User Guide&nbsp;");
        $form->text($input);
        $input->clear();
        
        $input->set( array("type"=>"text",
                           "class"=>"input",
                           "style"=>"width:200px;",
                           "name"=>"q",
                           "id"=>"q",
                           "size"=>"31",
                           "maxlength"=>"255",
                           "value"=>"")
                    );
        $form->text($input);
        $form->text('&nbsp;');
        $input->clear();
        
        $input->set( array("type"=>"submit",
                           "class"=>"submit",
                           "name"=>"sa",
                           "value"=>"Go")
                    );
        $form->text($input);

        
        
        $td->text($form);
        
        $tr->text($td);
        
        $table->text($tr);
        
        $data =  array('breadcrumb'=>$table->output() );
        
        
        
        // Destroy elements in bulk
        foreach( $elements as $element )
        {
            unset($$element);
        }
        
        $this->load->view('htmlelements',$data);
    }
}
?&gt;



Create HTML Elements - El Forum - 02-21-2009

[eluser]Colin Williams[/eluser]
Sort of neat, as a concept. But I just can't see the practicality of it. And it's definitely not anything you should use in a controller.


Create HTML Elements - El Forum - 02-21-2009

[eluser]xwero[/eluser]
Colin i think you mean a view, it goes perfect in a controller as demonstrated. But i have to agree the class is more a way to show you can make everything an object. I think it would be useful if the output method allows to add and/or overwrite, then you can do something like:
Code:
// controller
$a = new element('a');
$a->set("href","http://codeigniter.com/");
$a->text("CodeIgniter Home");

$data['ciLink'] = $a;
$this->load->view('view',$data);
// view
&lt;?php echo $ciLink->output('class="external"') ?&gt;



Create HTML Elements - El Forum - 02-21-2009

[eluser]Colin Williams[/eluser]
no. I absolutely meant the controller. The controller shouldn't generate markup. Period. What if tomorrow the app needs to output SVG, or JSON? Should the logic of the application change? No. This is the whole point of separating roles


Create HTML Elements - El Forum - 02-21-2009

[eluser]xwero[/eluser]
you are right but on the other hand i don't think the code should be in a view file either, except if it are views that are not touched by designers.


Create HTML Elements - El Forum - 02-21-2009

[eluser]garymardell[/eluser]
Sorry, i fail to see the usefulness. What is wrong with just writing HTML? Why would you want to make your application even slower and messy.


Create HTML Elements - El Forum - 02-21-2009

[eluser]brianw1975[/eluser]
I applaud your effort and the thought that you have evidently put into your work; unfortunately, I must concur with the census. I can't say how much use it will get, as you have seen above, people have varying beliefs on where final output should be generated at -- in some cases it would appear that there should be a 4th layer in MVC to work with the final output that is generated -- but who am I?

In my opinion, a Controller should not actually generate the output or content, but merely be used to decide which view should be used to display the content.

Moving on...

My only real suggestion as to your code is that I find that it appears to be long winded.

Code:
//7 commands
$p = new element('p');
$a->set("href","http://codeigniter.com/");
$a->text("CodeIgniter Home");
$p->set( 'class','nice_p');
$p->text( 'This is a great site' );
$p->text( $a );
$p->output();
/*<p class="nice_p">This is a great site<a href="http://codeigniter.com/">CodeIgniter Home</a></p> */

why not something more along the lines of:
Code:
//3 commands which could be whittled down to 1, albeit contrived, command

$link_data = array("href"=>'http://www.site.com',
                   "text"=>"link text",
                   "class"=>'link class',
                   "target"=>'_blank')
                   ; //etc...

$links['home'] = new Element('a',$link_data);
//assumes that the Element function could actually return text ready for output
$p_attribs = array("class"=>"nice_p");
echo new Element('p',$p_attribs,$links['home']);

Of course I must admit that everyone has their own coding style and that ultimately you will probably be the one to get the most use out of your own code - otherwise you wouldn't have made it; so feel free to ignore me.

Again, good job. You obviously put in a lot of time and effort with it.


Create HTML Elements - El Forum - 02-21-2009

[eluser]xwero[/eluser]
thinkigniter just CIed the class so it wasn't a whole lot of work.


Create HTML Elements - El Forum - 02-21-2009

[eluser]brianw1975[/eluser]
d'oh... Confusedhut:


Create HTML Elements - El Forum - 02-21-2009

[eluser]Colin Williams[/eluser]
I don't see a practical use of it anywhere.