PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Fri Dec 13, 2019 10:25 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Type Less HTML
PostPosted: Mon Dec 05, 2011 3:39 pm 
Offline
Forum Commoner

Joined: Mon Nov 21, 2011 1:16 am
Posts: 69
I have gotten really tiered of typing html, it's all over the place, and it's a mess; so I made this a minute ago:
Syntax: [ Download ] [ Hide ]
<?php
//********************************************************
//********************************************************
//********************************************************
//***
//***  echo_html.php v0.1a by Hermit TL
//***  Description: I'm so tiered of typing HTML
//***
//***  (If it's not obvious to you what this does:
//***   then don't use it!)
//***
//********************************************************
//********************************************************
//********************************************************

function echo_form1($method=NULL, $uri=NULL, $class=NULL){
        if(!$method){ $method = 'POST'; }
        if(!$uri){ $uri = 'index.php'; }
        echo "<form method=\"$method\" action=\"$uri\" class=\"$class\">\n";
}
function echo_form0(){
        echo "</form>\n";
}
        function echo_table1($class=NULL){
                echo "<table class=\"$class\">\n";
        }
        function echo_table0(){
                echo "</table>\n";
        }
                function echo_tr1($class=NULL){
                        echo "\t<tr class=\"$class\">\n";
                }
                function echo_tr0(){
                        echo "\t</tr>\n";
                }
                        function echo_td1($class=NULL){
                                echo "\t\t<td class=\"$class\">\n";
                        }
                        function echo_td0(){
                                echo "\t\t</td>\n";
                        }
function echo_hr(){
        echo "<HR>\n";
}
function echo_br(){
        echo "<br />\n";
}
 

I'm not really looking for help on making this better (just wanted to share for anyone who wants it) but,
If you have any suggestions on how to improve the code; please provide your improvements in the form of modified source code posted in your responses for other members.


Last edited by Hermit TL on Mon Dec 05, 2011 6:17 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: I type too much HTML
PostPosted: Mon Dec 05, 2011 5:56 pm 
Offline
Forum Commoner

Joined: Mon Nov 21, 2011 1:16 am
Posts: 69
I've modified quite a bit a source of code with this function now. All my includes are much more readable. I've also expanded the function with additional common elements I initially forgot I was using.

Syntax: [ Download ] [ Hide ]
<?php
//********************************************************
//********************************************************
//********************************************************
//***
//***  echo_html.php v0.2a by Hermit TL
//***  Description: I'm so tiered of typing HTML
//***
//***  (If it's not obvious to you what this does:
//***   then don't use it!)
//***
//********************************************************
//********************************************************
//********************************************************

function echo_form1($method=NULL, $uri=NULL, $class=NULL){
        if(!$method){ $method = 'POST'; }
        if(!$uri){ $uri = 'index.php'; }
        echo "<form method=\"$method\" action=\"$uri\" class=\"$class\">\n";
}
function echo_form0(){
        echo "</form>\n";
}
        function echo_table1($class=NULL){
                echo "<table class=\"$class\">\n";
        }
        function echo_table0(){
                echo "</table>\n";
        }
                function echo_tr1($class=NULL){
                        echo "\t<tr class=\"$class\">\n";
                }
                function echo_tr0(){
                        echo "\t</tr>\n";
                }
                        function echo_td1($class=NULL){
                                echo "\t\t<td class=\"$class\">\n";
                        }
                        function echo_td0(){
                                echo "\t\t</td>\n";
                        }
function echo_link($uri, $text=NULL, $class=NULL){
        if (!$text){ $text = $uri; }
        echo "<a class=\"$class\" href=\"$uri\">$text</a>\n";
}

function echo_p($paragraph, $class=NULL){
        echo "<p class=\"$class\">$paragraph</p>\n";
}
        function echo_p1($class=NULL){
                echo "<p class=\"$class\">\n";
        }
        function echo_p0(){
                echo "</p>\n";
        }


function echo_select($arr, $name=NULL, $class=NULL){
        echo "<select class=\"$class\" name=\"$name\">\n";
                foreach($arr as $val) {
                    echo "\t<option>$val</option>\n";
                }
        echo "</select>\n";
}

function echo_input($type, $name, $value, $min=NULL, $max=NULL, $text=NULL, $class=NULL){
        echo "<input class=\"$class\" type='$type' name='$name' value='$value' minlength=$min maxlength=$max>$text</input>\n";
}
function echo_image($uri, $text=NULL, $alt=NULL, $class=NULL){
        echo "<img class=\"$class\" src=\"$uri\" alt=\"$alt\">$text</img>\n";
}
function echo_hr(){
        echo "<HR>\n";
}
function echo_br(){
        echo "<br />\n";
}

function echo_tab($tabs=NULL){
        if(!$tabs){
                echo "\t";     
        }else{
                for ($i = 1; $i <= $tabs; $i++){
                        echo "\t";
                }
        }
}


.


Top
 Profile  
 
 Post subject: Re: Type Less HTML
PostPosted: Mon Dec 12, 2011 11:58 am 
Offline
DevNet Resident

Joined: Tue Nov 18, 2003 2:09 pm
Posts: 1826
Location: Middlesbrough, UK
In terms of your actual code it goes completely against all coding standards, you dont mix html, your use of backslashes is messy that could be solved by doing

Syntax: [ Download ] [ Hide ]
echo 'Now i can "quote things" without slashes';
 


If your looking for a 'templating engine of sorts', which again i feel is overkill, then look at the likes of smarty.

Alternatively i just use view files, pass arrays of information to them and echo it all out in a foreach loop, clean and easy to edit.

One of the problems your going to have is.... what happens when you want to add an table ID? or use thead / tfoot like your supposed to. You would need to massively extend this class. its simpler (especially for people having to work with your code) to just write plain HTML and escape properly, for eg

Syntax: [ Download ] [ Hide ]
<?php
// in foreach looping over $item
// YMMV with shorthand
// most frameworks auto-convert
<table class="<?=$class;?>">
<tr>
<td><?=$item->name;?></td>
</tr>
// etc
 


Althought it seems like a good idea now as writing tables is pretty boring, your gonna cause yourself more problems (and more time) in the future.


Top
 Profile  
 
 Post subject: Re: Type Less HTML
PostPosted: Tue Dec 13, 2011 7:15 pm 
Offline
Forum Commoner

Joined: Mon Nov 21, 2011 1:16 am
Posts: 69


Top
 Profile  
 
 Post subject: Re: Type Less HTML
PostPosted: Wed Dec 14, 2011 8:04 am 
Offline
DevNet Resident

Joined: Tue Nov 18, 2003 2:09 pm
Posts: 1826
Location: Middlesbrough, UK
Hi hermit,

As mentioned earlier what your trying to do.... well its just not the way its done. If this is solely for you and you enjoy using it then just go for it. If, however, your contributing code as part of a team then stuff like this just isnt the way you do things.

Personally ik wouldnt allow code like this to go anywhere near any of my projects, im sure in time you will understand why this is the case, until then, haoppy learning :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group