Jump to content

HTML lib in functional or OOP style


Awilum

Recommended Posts

Hello)

I write html "library" for my php App in function style.

But I think in OOP style will be better

What do think about first and second variant. Maybe I wrote something wrong in OOP style ?

 

/**
* Create a link in button style. 
* @param string $txt name
* @param stting $url link 
* @param string $title title if is empty then title = $txt
* @param boolean $render if this option is true then render html object else return it
*/
if (!function_exists('uiButton')) {
function uiButton($txt,$url,$title=NULL,$render=TRUE) {
	if(empty($title)) $title = $txt;
	// If $output is true then render else return $output
	if($render) echo '<span class="btn"><a href="'.$url.'" title="'.$title.'">'.$txt.'</a></span>'; else return $output;
}
}

/**
* Create a href link
* @param string $txt name
* @param stting $url link 
* @param string $title title if is empty then title = $txt
*/	
if (!function_exists('uiLink')) {
 function uiLink($txt,$url,$title=NULL,$render=TRUE) {
	if(empty($title)) $title = $txt;		
	$output = '<a href="'.$url.'" title="'.$title.'">'.$txt.'</a>';
	// If $output is true then render else return $output
	if($render) echo $output; else return $output;
}
}


if (!function_exists('uiHeading')) {	
function uiHeading($txt,$h='1',$render=TRUE) {
	$output = '<h'.$h.'>'.$txt.'</'.$h.'>';
	if($render) echo $output; else return $output;
}
}

if (!function_exists('uiBr')) {	
function uiBr($num = 1) {
	echo str_repeat("<br />", $num);
}
}

if (!function_exists('uiNbsp')) {
function uiNbsp($num = 1) {
	echo str_repeat("&nbsp", $num);
}
}



if (!function_exists('uiImg')) {
function uiImg($src,$align=NULL,$alt=NULL,$border=NULL,$width=NULL,$height=NULL,$render=TRUE) {
	$output .= '<img ';
	$output .= 'src="'.$src.'" ';
	if(!empty($align))
		$output .= 'align="'.$align.'" ';
	if(!empty($border))
		$output .= 'border="'.$botder.'" ';
	if(!empty($width))
		$output .= 'width="'.$width.'" ';
	if(!empty($height))
		$output .= 'height="'.$height.'" ';
	if(!empty($alt))
		$output .= 'alt="'.$alt.'" ';
	else 
		$output .= 'alt=""';

	$output .= ' />';	

	// If $output is true then render else return $output
	if($render) echo $output; else return $output;
}
}

 

 

exemple first variant

 

	
uiImg('http://site.com/img/img.png','','','',640,480,$render=TRUE) {

 

 

and second variant

 

 

	
class HTML {

	var $name;
	var $url;
	var $action;
	var $method = 'post';
	var $width;
	var $hight;
	var $alt;
	var $title;
	var $align;
	var $class;
	var $id;
	var $style;
	var $border;
	var $value;


	function link() {
		echo '<a href='.$this->url.'>'.$this->name.'</a>';
	}

	function formOpen() {
		echo '<form action="'.$this->action.'" method="'.$this->method.'">';
	}

	function img() {
		$output = '';
		$output .= '<img ';
		$output .= 'src="'.$this->url.'" ';
		if(!empty($this->align))
			$output .= 'align="'.$this->align.'" ';
		if(!empty($this->border))
			$output .= 'border="'.$this->border.'" ';
		if(!empty($this->width))
			$output .= 'width="'.$this->width.'" ';
		if(!empty($this->height))
			$output .= 'height="'.$this->height.'" ';
		if(!empty($this->alt))
			$output .= 'alt="'.$this->alt.'" ';
		else 
			$output .= 'alt=""';

		$output .= ' />';	
		echo $output;
	}

} 

$img = new HTML();
$img->url = 'http://site.com/img/img.png';
$img->width = 640;
$img->height = 480;
$img->img();

Link to comment
https://forums.phpfreaks.com/topic/219457-html-lib-in-functional-or-oop-style/
Share on other sites

thorpe

 

for exemple this function is helpful

 

	function uiBr($num = 1) {
	echo str_repeat("<br />", $num);
}

 

uiBr(7);

 

better then

 

<br /><br /><br /><br /><br /><br /><br />

 

qustion about my OOP style. Bad or normal ?

 

class HTML {

	var $name;
	var $url;
	var $action;
	var $method = 'post';
	var $width;
	var $hight;
	var $alt;
	var $title;
	var $align;
	var $class;
	var $id;
	var $style;
	var $border;
	var $value;


	function link() {
		echo '<a href='.$this->url.'>'.$this->name.'</a>';
	}

	function formOpen() {
		echo '<form action="'.$this->action.'" method="'.$this->method.'">';
	}

	function img() {
		$output = '';
		$output .= '<img ';
		$output .= 'src="'.$this->url.'" ';
		if(!empty($this->align))
			$output .= 'align="'.$this->align.'" ';
		if(!empty($this->border))
			$output .= 'border="'.$this->border.'" ';
		if(!empty($this->width))
			$output .= 'width="'.$this->width.'" ';
		if(!empty($this->height))
			$output .= 'height="'.$this->height.'" ';
		if(!empty($this->alt))
			$output .= 'alt="'.$this->alt.'" ';
		else 
			$output .= 'alt=""';

		$output .= ' />';	
		echo $output;
	}

} 

$img = new HTML();
$img->url = 'http://site.com/img/img.png';
$img->width = 640;
$img->height = 480;
$img->img();

 

if you know the better variants, offer)

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.