Building a print link with TS and TV

Making a printable-page template with TemplaVoila is supposed to be easy. The Futuristic Template Building document explains the process in detail and, for the most part, it does appear easy. Even the required TS is quite short:

  1. lib.print_url = TEXT
  2. lib.print_url {
  3.   value = index.php?id={page:uid}&print=1&no_cache=1
  4.   htmlSpecialChars = 1
  5.   insertData = 1
  6. }
  7. [globalVar = GP:print > 0]
  8.   lib.print_url.value = index.php?id={page:uid}
  9. [global]

However, it turns out that the TS provided to make the link to the printable version of the page is entirely unsuitable for any but the simplest sites in TYPO3:

This is because the included print-link typoscript in the FTB document does not use typolink for making links—as it should.

The following sample shows that the typolink function can be used for generating print links with TV. More importantly, it illustrates the principle that typolink can be used for virtually any link-based switching that might be required on a TYPO3 website—including language-switching menus or stylesheet-switchers.

All of these applications can be achieved using the same principle: adding and subtracting GET parameters from the current page's url—and typolink is the best method to add or subtract GET parameters in TYPO3 (note that this is also true inside extensions).

  1. // Create a link to a printable version of the current
  2. // page--complete with all GET parameters needed by
  3. // extensions:
  4. lib.print_link = TEXT
  5. lib.print_link {
  6.   // The link text:
  7.   value = Print view
  8.   typolink {
  9.     // Get the id of the current page:
  10.     parameter {
  11.       data = page:uid
  12.     }
  13.     // Add whatever is in the query string:
  14.     addQueryString = 1
  15.     addQueryString {
  16.       // Account for the fact that RealURL etc
  17.       // may have manipulated the query string:
  18.       method = GET
  19.     }
  20.     // Add the 'print' parameter:
  21.     additionalParams = &print=1
  22.     // Do not cache the print version:
  23.     no_cache = 1
  24.   }
  25. }
  26. // Changes to the link for when we are ON a print page:
  27. [globalVar = GP:print > 0]
  28.   lib.print_link {
  29.     // The link text       
  30.     value = Normal View       
  31.     typolink {
  32.       // Unset the print parameter:
  33.       additionalParams >
  34.       // Make sure we don't turn off
  35.       // the cache for the REGULAR
  36.       // version of the page:
  37.       no_cache = 0
  38.       // Add the entire query string
  39.       // for the current location,
  40.       // EXCEPT for the print and
  41.       // no_cache parameters we
  42.       // introduced:
  43.       addQueryString.exclude  = no_cache,print
  44.     }
  45.   }
  46. [global]
Download this file: Typoscript for TV Print Link