Jump to content

Archived

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

Liquid Fire

XHTML Class?

Recommended Posts

I am in the process of building a Web API to help speed along my coding of my projects.  One thing that i am going to build and a html_element class which will create any html element and allow you to add the data/attributes and whatever.  The initial force behind this was so i could build table just by passing an array array to a class(it would take care of all the looping and redundant crap) and i though i might as well abstract the html_element into it own class.  for example my old way of code a news list would be:

<?php
public function print_news()
{
	$news = $this->get_news(null, null, null, 1, "ORDER BY n.date DESC", 15);

	$html = "<div>";

	foreach($news as $news_piece)
	{
		$html .= "<div class=\"date\">{$news_piece['date']}</div>";
		$html .= "<div class=\"title\">{$news_piece['title']}</div>";
		$html .= "<div class=\"story\">{$news_piece['story']}</div>";
		$html .= "<div class=\"author\">Posted by: {$news_piece['author']} - {$news_piece['time']}</div>";
	}

	$html .= "</div>";
	print $html;
}
?>

but the new way with the html_element class is:

<?php
public function print_news()
{
	$news = $this->get_news(null, null, null, 1, "ORDER BY n.date DESC", 15);

	$html = new html_element("div", false, array("id" => "news"));

	foreach($news as $news_piece)
	{
		$date = new html_element("div", false, array("class" => "date"), $news_piece['date']);
		$title = new html_element("div", false, array("class" => "title"), $news_piece['title']);
		$story = new html_element("div", false, array("class" => "story"), $news_piece['story']);
		$author = new html_element("div", false, array("class" => "author"), "Posted by: {$news_piece['author']} - {$news_piece['time']}");

		$html->add_element($date);
		$html->add_element($title);
		$html->add_element($story);
		$html->add_element($author);
	}

	$html->print_html();
}
?>

Now in terms of number of characters the old way is 509 and the new way is 768 which is a bit more code but this is really a simple example(i mean with the table class it is going to save you 100's even 1000's of characters you need to code).  Even tho it is more to code for this example there are 2 reason i would use this way over the older way.  1 is that this way will generate valid XHTML code every time since the process of creating this code is the same and has been tested.  The second goes along with the first and that is there are not going to be any little mistake the every does every now and then.  i mean it is not going to forget to close a tag or close the quotes in the attribute, basically you limit the number of ways you can make a small mistake that is sometimes hard to find.

 

What do you think?  Would you use this method of creating xhtml code most of the time or only in places where it will saving you a lot of characters(like tables from database queries)?

 

Also if anyone has any suggestions on how to optimize the above html_element to limit the number of character need, please let me know.  I mean for instance i am also going to allow add_element() to take an array of html_element would would count the character count from 768 to 721.

Share this post


Link to post
Share on other sites

I don't see any problems in the above.

 

If you want it shorter, then you can do:

<?php
public function print_news()
{
	$news = $this->get_news(null, null, null, 1, "ORDER BY n.date DESC", 15);

	$html = new html_element("div", false, array("id" => "news"));

	foreach($news as $news_piece)
	{
		$html->add_element(new html_element("div", false, array("class" => "date"), $news_piece['date']));
		$html->add_element(new html_element("div", false, array("class" => "title"), $news_piece['title']);
		$html->add_element(new html_element("div", false, array("class" => "story"), $news_piece['story']));
		$html->add_element(new html_element("div", false, array("class" => "author"), "Posted by: {$news_piece['author']} - {$news_piece['time']}"));
	}

	$html->print_html();
}
?>

Share this post


Link to post
Share on other sites

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