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
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)

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.