rejetto forum

Content negotiation for HFS templates

0 Members and 1 Guest are viewing this topic.

Offline Unknown8063

  • Occasional poster
  • *
    • Posts: 96
    • View Profile
I've been using a content negotiation script in PHP on my main server so I decided to get working on one for HFS.  This script will check the Accept header of the user-agent to determine if it can accept pages served as application/xhtml+xml.  If it cannot, it will fallback on text/html.  There is also an if block to serve pages as application/xhtml+xml if the user agent happens to be a validator.

To use it, simply place it anywhere in the page you want negotiated.  An obvious place is a header section included in all pages.

Warning! When pages are served as real XHTML they are parsed by the browser using stricter rules than HTML soup.  The RAWR template I use crashes on a parse error and I suspect most of the available templates will.

Code: [Select]
{.comment |
{.set |MIME_TYPE|text/html.}
{.if | {.count substring |application/xhtml+xml|{.header|Accept.}.}
  | {: {.if not |{.count substring |application/xhtml+xml;q=0|{.header|Accept.}.}
          | {: {.set |MIME_TYPE|application/xhtml+xml.} :}
          | {: {.if | {.count substring |application/xhtml+xml;q=0.|{.header|Accept.}.}
                  | {: {.set |MIME_TYPE|application/xhtml+xml.} :}
                .} :}
        .} :}
.}
{.if | {.or
        | {.count substring |W3C_Validator|{.header|User-Agent.}.}
        | {.count substring |WDG_SiteValidator|{.header|User-Agent.}.}
        | {.count substring |W3C-checklink|{.header|User-Agent.}.}
        | {.count substring |Web-Sniffer|{.header|User-Agent.}.}
        | {.count substring |FeedValidator|{.header|User-Agent.}.}
        | {.count substring |Poodle predictor|{.header|User-Agent.}.}
        | {.count substring |Leknor.com gzip tester|{.header|User-Agent.}.}
      .}
  | {: {.set |MIME_TYPE|application/xhtml+xml.} :}
.}
{.add header |Vary: Accept.}
{.add header |Content-type: {.call |MIME_TYPE.}; charset=UTF-8.}
.}

This is my first time using macros, so please let me know if I made a mistake :D It seemed to work in my testing.

8063
« Last Edit: January 16, 2009, 05:59:20 AM by Unknown8063 »


Offline rejetto

  • Administrator
  • Tireless poster
  • *****
    • Posts: 13523
    • View Profile
it seems a good job :)
my advice is to
{.set|ua|{.header|User-Agent.}.}
{.set|acpt|{.header|Accept.}.}

so you can change every {.header.} call in {.^ua.} and {.^acpt.}

"^" is a short for "call"