/    Sign up×
Community /Pin to ProfileBookmark

Which do you think is the best approach to outputting HTML code?

I always hated the way I sometimes had to mix strings of HTML with code when outputting something like a navigation list in PHP. In particular I don’t like the way you’re mixing variables with bits of code in a very linear fashion, mixing if statements in between tags etc.

For example:

[code=php]
$html = ‘<ul class=”channel clearfix”>’;
$html .= ‘<li class=”first’;
if ($this->isChannel(1)) $html .= ‘ selected’;
$html .= ‘”><a href=”‘ . $this->getLinkByChannel(1) . ‘”>Channel 1</a></li>’;
$html .= ‘<li’;
if ($this->isChannel(2)) $html .= ‘ class=”selected”‘;
$html .= ‘><a href=”‘ . $this->getLinkByChannel(2) . ‘”>Channel 2</a></li>’;
$html .= ‘</ul>’;

return $html;
[/code]

I’ve started using a class which lets you create tags as objects and add properties/attributes, content, etc. at any stage, which removes the hassle of dealing with strings of HTML mixed with code and makes the code more flexible, easier to manage, and in my opinion, sometimes easier to read.

I was wondering what other’s thoughts were on this – which is the best way in your opinion? I’ve done some benchmarking and obviously the first way (writing out straight HTML) is faster. But for some things it can be much easier to write and update code if you’re using the object-oriented approach.

The examples used here are pretty simple and may seem like overkill but I’ve found it useful for some more complex HTML where what’s generated depends on many factors.

Here’s an example of the object-based code:

[code=php]
$ul = new HtmlElement(‘ul’, NULL, NULL, ‘channel clearfix’);

$li1 = new HtmlElement(‘li’);
($this->isChannel(1)) ? $li1->setClass(‘first selected’) : $li1->setClass(‘first’);
$a1 = new HtmlElement(‘a’, ‘Channel 1’);
$a1->addAttribute(‘href’, $this->getLinkByChannel(1));
$li1->addChild($a1);
$ul->addChild($li1);

$li2 = new HtmlElement(‘li’);
if ($this->isChannel(2)) $li2->setClass(‘selected’);
$a2 = new HtmlElement(‘a’, ‘Channel 2’);
$a2->addAttribute(‘href’, $this->getLinkByChannel(2));
$li2->addChild($a2);
$ul->addChild($li2);

return $ul->getOutput();
[/code]

And here’s what the final HTML looks like:

[code=html]
<ul class=”channel clearfix”>
<li class=”first selected”><a href=”?c=1&amp;d=0&amp;t=4″>Channel 1</a></li>
<li><a href=”?c=2&amp;d=0&amp;t=4″>Channel 2</a></li>
</ul>
[/code]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogNov 10.2006 — I understand what you're doing, and it makes a certain amount of sense. However, when it takes about twice as much code to generate the same result and doesn't seem really any easier for me to read, I'm not sure if you've gained anything by it. I'd probably be more inclined to do something like the following if I wanted to make the original code more readable:
[code=php]
$html = <<<EOD
<ul class="channel clearfix">
<li class="first%s"><a href="%s">Channel 1</a></li>
<li%s><a href="%s">Channel 2</a></li>
</ul>
EOD;

$html = sprintf
(
$html,
$this->isChannel(1) ? ' selected' : '',
$this->getLinkByChannel(1),
$this->isChannel(2) ? ' class="selected"' : '',
$this->getLinkByChannel(2)
);

return $html;
[/code]
Copy linkTweet thisAlerts:
@cdk57authorNov 11.2006 — Nice one, thanks for that NogDog - definitely a cleaner solution and one that still allows you to separate logic and presentation nicely... Think I'll switch over to that!
Copy linkTweet thisAlerts:
@pforsbergfan9Nov 11.2006 — I was having the same problem as the orignal poster. But im curious as to what the EOD is? the rest of it made sense and it helps but I was wondering just what the EOD was. Is it a self made function?
Copy linkTweet thisAlerts:
@pcthugNov 11.2006 — [url=http://au2.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc]Heredoc Syntax[/url].
×

Success!

Help @cdk57 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...