Page 1 of 1

Type Less HTML

Posted: Mon Dec 05, 2011 2:39 pm
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.

Re: I type too much HTML

Posted: Mon Dec 05, 2011 4:56 pm
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";
		}
	}
}
.

Re: Type Less HTML

Posted: Mon Dec 12, 2011 10:58 am
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.

Re: Type Less HTML

Posted: Tue Dec 13, 2011 6:15 pm
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";
}
}

Re: Type Less HTML

Posted: Wed Dec 14, 2011 7:04 am
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 :)