Question

Can someone explain why this is useful if you can never use the HtmlHelper to close a tag?

// No text specified.
<?php
echo $this->Html->tag('span', null, array('class' => 'welcome'));
?>

// Output
<span class="welcome">

In an ideal world there would be some way to create a closing tag, such as </span>

Was it helpful?

Solution

Well, the "usefulness" depends on how it's been used.

First, there's no HtmlHelper->closeTag function, because the tag function surrounds the string (or element) that's inside. Example

echo $this->Html->tag('span', "i'm a span", array('class' => 'welcome'));

// Output
<span class="welcome">i'm a span</span>

There's really no point on having a closeTag function if the HtmlHelper opens and closes tags inside the function.

Clearly an unclosed <span> tag isn't that useful either, and that example isn't the best one to show some use (I know it's in the cake docs, not blaming you).

Since HTML isn't XML, then there are cases where unclosed tags may appear (check the HTML specs). In that link is a list of what is defined as void elements, which are:

area, base, br, col, command, embed, hr, img, input, keygen, link, meta, param, source, track, wbr

Little ways down that link reference, it is written

Void elements only have a start tag; end tags must not be specified for void elements.

Soooo.... you may want to do that. You can write <br> with a complicated and more character-count expensive command. Maybe you want to write the whole .cpt view in php and no echos of normal strings, just pure php with the advantages of HtmlHelper (like escaping html characters).

So, in theory, you can do

echo $this->Html->tag('br', null, array());
echo $this->Html->tag('link', null, array());

and that would be valid tags. Why would you do that? * shrug * Who knows. But isn't it nice you have the option?

Also, HtmlHelper probably uses that function internaly to write other tags. I mean, maybe HtmlHelper->img() uses HtmlHelper->tag('img', null), and since the tag function is public, well, you get that seemingly useless option as well.

I know it doesn't sound as much "usefulness" still, but it's the little things that count...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top