XHTML version of my pages

Continuing in the interest of providing multiple file formats for my web pages, I now have my home page available in XHTML5 format. URL’s just need .xhtml tacked on the end, except for the home page, which needs index.xhtml. This makes use of Symfony’s _format URL parameter.

I already generally write my HTML in XHTML format anyways, so I only needed needed to make a few minor changes to my markup to make things work. I already had closings for all of my tags (like </li> for every <li> and a slash at the end of self-closing tags, like <meta />). Most of my attributes had values, but I did have to add them for async and defer on a <script> tag, like <script async="async" defer="defer"…>. I had to add xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" on the <html> element. I had to remove any <noscript> elements (these cannot be used in XHTML because browsers don’t know how to handle them in XML). Finally, I had to change the comments around inline script tag content to look like:


See this Drupal discussion for a bit of information on the comment thing.

I just used the same template for HTML and XHTML, so I used conditionals to wrap anything that differed between the two. The Symfony Request object has a getRequestFormat() method that will return the _format string. In Twig templates, that is accessed with app.request.requestFormat, so I can do things like:

<html lang="en"{% if app.request.requestFormat == 'xhtml' %} xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"{% endif %}>


{% if app.request.requestFormat != 'xhtml' %}<noscript>…</noscript>{% endif %}

One other thing that has to be done for this to work with Symfony is set up the proper mime-type, like:

   xhtml: 'application/xhtml+xml'

My controllers are set up to render templates named based on the _format, like:

    public function myAction(Request $request){
        return $this->renderPage('MyBundle:folder:template.' . $request->getRequestFormat() . '.twig');

I generally just symlink the ‘.xhtml’ one to the ‘.html’ one.