Contact Us

How-to: Use Html 4.01 in CakePHP 1.2

Posted on 21/2/07 by Felix Geisendörfer

Deprecated post

The authors of this post have marked it as deprecated. This means the information displayed is most likely outdated, inaccurate, boring or a combination of all three.

Policy: We never delete deprecated posts, but they are not listed in our categories or show up in the search anymore.

Comments: You can continue to leave comments on this post, but please consult Google or our search first if you want to get an answer ; ).

Hi folks,

this is just a little tip for those of you who dislike their xhtml to end up as tag soup and still prefer to send out Html 4.01 strict. Here is how to make all helper functions in Cake 1.2 output Html 4.01 compliant markup instead of xhtml:

class AppHelper extends Helper
     * This is the constructor for our AppHelper class that we use to make all helper output html 4.01
     * compatible markup.
     * @return AppHelper

    function __construct()
        // Loop through all tags in this helper
        foreach ($this->tags as $tag => $html)
            // Replace all xhtml style tag closings with html 4.01 strict compatible ones
            $this->tags[$tag] = r('/>', '>', $html);

If you wonder why somebody would prefer the good old html over the all-so-hyped xhtml, read a little bit about it here.

-- Felix Geisendörfer aka the_undefined


You can skip to the end and add a comment.

Nate Klaiber said on Feb 22, 2007:

I pretty much built the same thing this afternoon (See my recent post on CakePHP). One of the things I wished you could change was the output of markup based on the proper doctype that you could select. I use HTML4 strict for my pages, and I didn't want to manually have to change the core or add new tags and a new helper.

Nice job :)

Felix Geisendörfer said on Feb 22, 2007:

Nate Klaiber: Excellent idea. I'll talk with nate (abele) and see what he thinks about this ; ).

jared  said on Apr 12, 2007:

doesn't this seem extremely inefficient/expensive to go through all the tags on a page and strip out these slashes?

Felix Geisendörfer said on Apr 13, 2007:

jared: Sure. Would it make you feel better if I'd use a lambda-style (create_function) function and use array_walk? *g*. No honestly, this is something that should be in the core imho and maybe it will be at some point. For now I see this as a pragmatic work-around and not the most efficient or beautiful solution to the problem. Hope that's ok ; ).

Adeel Khan said on May 13, 2007:

It's not all the tags on the page, it's all the tags generated by helpers.

Sam  said on May 14, 2007:

It would seem much more effective to add your own tags (without the closing slashes in this case). See this ticket:

You can find the default tags in /cake/libs/view/helpers/html.php at the top. Copy that into a new file named tags.php and put it in your /app/config directory. I just tried this out myself and it seems to work quite well.

I also changed the doctype in my layout by calling:


which completes the changeover to HTML 4.01 strict.

Felix Geisendörfer said on May 14, 2007:

Well, I'm lazy, I like to keep things DRY and I don't really care about the 2ms spent on this one way or the other. The only 'proper' way to handle this is by having native support for it in the framework (= your docType change also changes tags). Other then that everything is a hack and this one is the least painful to implement and maintain so it's my favourite : ).

Leandro said on Sep 21, 2007:

thanks really

This post is too old. We do not allow comments here anymore in order to fight spam. If you have real feedback or questions for the post, please contact us.