Type Less HTML

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
Hermit TL
Forum Commoner
Posts: 69
Joined: Mon Nov 21, 2011 12:16 am

Type Less HTML

Post by Hermit TL »

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:

Code: Select all

<?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 5:17 pm, edited 1 time in total.
Hermit TL
Forum Commoner
Posts: 69
Joined: Mon Nov 21, 2011 12:16 am

Re: I type too much HTML

Post by Hermit TL »

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.

Code: Select all

<?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";
		}
	}
}
.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Re: Type Less HTML

Post by malcolmboston »

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

Code: Select all

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

Code: Select all

<?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.
Hermit TL
Forum Commoner
Posts: 69
Joined: Mon Nov 21, 2011 12:16 am

Re: Type Less HTML

Post by Hermit TL »

You would need to massively extend this class.
Okay. Here's an update.

Code: Select all

<?php
//********************************************************
//********************************************************
//********************************************************
//***  echo_html.php v0.4a by Hermit TL
//***  Description:
//***		This function is intended to make it easier and quicker
//***		to write code that outputs HTML to the browser
//***		properly tabed and with line breaks.
//***		
//***		###This function is not intended to subsitute knowledge of HTML###
//***
//***
//***  Useage:  echo_form1($method, $uri, $class, $alters) 	=> <form $alters method="$method" action="$uri" class="$class">
//***			$method	| {GET,POST}
//***			$uri	| {form action}
//***			$class	| {class name}
//***			$alters	| {inject attribs}
//***		echo_form0()					=> </form>
//***		echo_table1($class, $alters)			=> <table $alters class="$class">
//***			$class	| {class name}
//***			$alter	| {inject attribs}
//***		echo_table0()					=> </table>
//***		echo_status_bar($status)			=> <table><tr><td><p>$status</p></td></tr></table>
//***			$status	| {Display Msg}
//***		echo_tr0()
//***		echo_tr1($class, $alters)			=> <tr $alters class="$class">
//***			$class	| {class name}
//***			$alters	| {inject atrribs}
//***		echo_td0()					=> </td>
//***		echo_td1($class, $alters)			=> <td $alters class="$class">
//***			$class	| {class name}
//***			$alters	| {inject attribs}
//***		echo_link($uri, $text, $class, $alters)		=> <a $alters href="$uri">$text</a>
//***			$uri	| {uri}
//***			$text	| {class name}
//***			$alters	| {inject attribs}
//***		echo_link1($uri, $class, $alters)		=> <a $alters class="$class" href="$uri">
//***			$uri	| {URI}
//***			$class	| {class name}
//***			$alters	| {inject attribs}
//***		echo_link0()					=> </a>
//***		echo_p($paragrah, $class, $alters)		=> <p $alters class="$class">$paragraph</p>
//***		      $paragrah	| {The Paragraph}
//***			$class	| {class name}
//***			$alters	| {inject attribs}
//***		echo_p1($class, $alters)			=> <p $alters class="$class">
//***			$class	| {class name}
//***			$alters	| {inject attribs}
//***		echo_p0()					=> </p>
//***		echo_hr($class, $alters)
//***			$class	| {class name}
//***			$alters	| {inject attribs}
//***		echo_br()					=> <br />
//***		echo_div($class, $text, $alters)		=> <div $alters class="$class">$text</div>		
//***			$class	| {class name}
//***			$text	| {text}
//***			$alters	| {inject attribs}
//***		echo_tab($tabs)					=> {TAB}
//***			$tabs	| {# of Tabs}
//***		echo_html_comment($comment)			=> <--! $comment -->
//***		       $comment | {html comment}\
//********************************************************
//*******************Experiemental************************
//********************************************************
//***		echo_select($arr, $name, $class, $alters, $alters2)
//***			$arr	| {select elements}
//***			$name	| {select name}
//***			$class	| {class name}
//***			$alters	| {inject select attribs}
//***		       $alters2 | {inject option attribs}
//***		echo_input($type, $name, $value, $min, $max, $text, $class, $alters)
//***			$type	| {input type}
//***			$name	| {input name}
//***			$value	| {input value}
//***			$min	| {min}
//***			$max	| {max}
//***			$text	| {text}
//***			$class	| {class name}
//***			$alters	| {inject attribs}
//***		echo_textarea($name, $cols, $rows, $dtxt, $max)
//***			$name	| {textarea name}
//***			$cols	| {# of columns}
//***			$rows	| {# of row}
//***			$dtxt	| {default text}
//***			$max	| {max input}
//***		echo_image($uri, $text, $alt, $class, $alters, $return)
//***			$uri	| {img URI}
//***			$text	| {img text}
//***			$alt	| {img alt txt}
//***			$class	| {class name}
//***			$alters	| {inject attribs}
//***			$return	| {true,false [return string/ echo string]}
//********************************************************
//********************************************************
//********************************************************

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

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


function echo_select($arr, $name=NULL, $class=NULL, $alters=NULL, $alters2=NULL){
	echo "<select $alters class=\"$class\" name=\"$name\">\n";
		foreach($arr as $val) {
		    echo "\t<option $alters2>$val</option>\n";
		}
	echo "</select>\n";
}
function echo_input($type, $name, $value, $min=NULL, $max=NULL, $text=NULL, $class=NULL, $alters=NULL){
	echo "<input $alters class=\"$class\" type='$type' name='$name' value='$value' minlength=$min maxlength=$max>$text</input>\n";
}
function echo_textarea($name, $cols, $rows, $dtxt, $max=NULL){
	echo "<textarea name=$name cols=$cols rows=$rows maxlength=\"$max\">$dtxt</textarea>\n";
}
function echo_image($uri, $text=NULL, $alt=NULL, $class=NULL, $alters=NULL, $return=NULL){
	$echo = "<img $alters class=\"$class\" src=\"$uri\" alt=\"$alt\">$text</img>\n";
	switch($return){
		case true:
			return $echo;
		break;
		default:
			echo $echo;
		break;
	}
}
function echo_hr($class=NULL, $alters=NULL){
	echo "<HR $alters class=\"$class\">\n";
}
function echo_br(){
	echo "<br />\n";
}
function echo_div($class=NULL, $text=NULL, $alters=NULL){
	echo "<div $alters class=\"$class\">$text</div>\n";
}
function echo_tab($tabs=NULL){
	if(!$tabs){
		echo "\t";	
	}else{
		for ($i = 1; $i <= $tabs; $i++){
			echo "\t";
		}
	}
}
function echo_html_comment($comment = NULL){
	echo "<!-- $comment -->\n";
}
}
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Re: Type Less HTML

Post by malcolmboston »

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 :)
Post Reply