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:
- lib.print_url = TEXT
- lib.print_url {
- value = index.php?id={page:uid}&print=1&no_cache=1
- htmlSpecialChars = 1
- insertData = 1
- }
- [globalVar = GP:print > 0]
- lib.print_url.value = index.php?id={page:uid}
- [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:
- the link will not respect simulateStaticDocuments, RealURL or CoolURI
- the link will not retain any GET parameters such as those used by tt_news or other extensions
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).
- // Create a link to a printable version of the current
- // page--complete with all GET parameters needed by
- // extensions:
- lib.print_link = TEXT
- lib.print_link {
- // The link text:
- value = Print view
- typolink {
- // Get the id of the current page:
- parameter {
- data = page:uid
- }
- // Add whatever is in the query string:
- addQueryString = 1
- addQueryString {
- // Account for the fact that RealURL etc
- // may have manipulated the query string:
- method = GET
- }
- // Add the 'print' parameter:
- additionalParams = &print=1
- // Do not cache the print version:
- no_cache = 1
- }
- }
- // Changes to the link for when we are ON a print page:
- [globalVar = GP:print > 0]
- lib.print_link {
- // The link text
- value = Normal View
- typolink {
- // Unset the print parameter:
- additionalParams >
- // Make sure we don't turn off
- // the cache for the REGULAR
- // version of the page:
- no_cache = 0
- // Add the entire query string
- // for the current location,
- // EXCEPT for the print and
- // no_cache parameters we
- // introduced:
- addQueryString.exclude = no_cache,print
- }
- }
- [global]
