Archive of UserLand's first discussion group, started October 5, 1998.

Scalable content via mainResponder

Author:Samuel Reynolds
Posted:4/22/1999; 2:32:18 PM
Topic:Scalable content via mainResponder
Msg #:5205
Prev/Next:5204 / 5206

MainResponder allows a neat trick to switch templates on the fly, so you can get pages formatted for different purposes--such as for printing or for particular browsers--in your dynamic site.

You can change the template from your firstFilter, so you can use a simple, "hard-coded" link on any page to generate a printable version of the current page. Or any other alternate version, as far as that goes.

Or you can select a specific template based on other information included in the request. For example, you could select one template if the request comes in from an Internet Explorer 2.0 browser, and a different one if it comes from Netscape 5.

HOW TO DO IT

You can add formatted-for-printing pages to any mainResponder site by doing the following:

1. In your page template, add the following link (wherever you think is appropriate, and change the link text as you like, of course):

   \

2. In your FirstFilter, add the following block:

   if ( adrPageTable^.searchArgs contains "printable=true" ) {
      table.assign( @adrPageTable^.template, adrPageTable^.templates^.print )}

3. Finally, create a new template called "print" in your #templates subtable, with minimal additions to the page (maybe just your copyright statement and a go-back-to-the-real-page link).

Now load the (real) page into your browser. Click on the new link to get the print-optimized version.

Look, ma--no clutter!

A MORE GENERAL APPROACH

Or, for a more general technique, replace ?printable=true" with "?tmpl=print" (or any other template), parse the searchArgs out to a table with webserver.parseArgs...

   webserver.parseArgs(adrPageTable^.searchArgs, @adrPageTable^.argTable )
...and switch on the value of argTable.tmpl to select which template to render with:
if defined( adrPageTable^.argTable.tmpl ) {
   local ( maybeTemplate = @adrPageTable^.templates^.[ adrPageTable^.argTable.tmpl ] );
   if defined( maybeTemplate^ ) {
      table.assign( @adrPageTable^.template, maybeTemplate^ )}}

DIFFERENT TEMPLATES FOR DIFFERENT BROWSERS

Instead of looking at the searchArgs, you might look at requestHeaders.User-Agent, which identifies the browser, and switch on it. For example:

local {
   userAgent = string.firstWord( adrPageTable^.requestHeaders.["User-Agent"] );
   browser = string.nthField( userAgent, '/', 1 );
   browserVersion = string.nthField( userAgent, '/', 2 )};
if ( double(browserVersion) < 4.0 ) {
   table.assign( @adrPageTable^.template, adrPageTable^.templates^.pre40browsers )}

OR YOU CAN REDIRECT

If you want, instead of changing the template, you can redirect to a different URL--perhaps a static version of the site, or whatever. To do that, replace the table.assign() calls in any (or all) the above sample code with a redirect scriptError (for immediate redirect):

   scriptError( "!redirect http://url.to.redirect.to" )

DO IT ALL!

Left as an exercise to the reader: Combine the above techniques in a single website. You'll end up with a chain of if and case statements and a very adaptable website!

Voila! Scalable content via the mainResponder--in your choice of flavor!

- Sam


There are responses to this message:



This page was archived on 6/13/2001; 4:49:28 PM.

© Copyright 1998-2001 UserLand Software, Inc.