APGen Documentation Previous Topic: Output.Buffer Next Topic: Output.WriteIffChanged Parent Topic: Output Object    Output Object
Output.BufferContents
See Also:

The Output.BufferContents property gives read/write access to the output buffer.

Syntax

VBScript:

Output.BufferContents [= strBuf]

JScript:

Output.BufferContents [= strBuf];

Parameters

strBuf A string that will replace the contents of the output buffer.

Notes

The Output.BufferContents property is useful for post-processing generated output.  If Output.Buffer is True, Output.BufferContents can be used to access and manipulate any content that has accumulated in the buffer. 

If Output.Buffer is False (the default value), Output.BufferContents will be empty.

HTML Compression Example

This example defines a JScript routine named CompressHTMLOutput()CompressHTMLOutput() takes an APG script that has been executed, and compresses its output.  Output.BufferContents is used to retrieve and set the APG script's output.  A series of Regular Expressions are used to delete unneeded HTML comments and get rid of unneeded whitespace.  By using this technique, output file size is reduced, which reduces the bandwidth cost of viewing the web page.

<%# @LANGUAGE="JScript" #%>
<%#

//================================================================
//    CompressHTMLOutput(oAPGScript)
//
//    Removes whitespace and comments from output HTML files.
//
//    oAPGScript - An APGScript that has been executed.  Output
//    buffering must be on, so that Output.BufferContents contains
//    all the output that has been generated.
//================================================================

function CompressHTMLOutput(oAPGScript)
    {

    // Optional: un-comment this to Post-Process ASP files
    /* if (oAPGScript.Output.Filename.search(/\.asp$/i) == -1) */
    {
    // Only post process HTM and HTML files
    if (oAPGScript.Output.Filename.search(/\.htm.?$/i) == -1)
        return;
    }    

    // Compress the output buffer.
    // Note: This handles <script> tags and others acceptably,
    // but does not handle <pre> tags properly.  Text
    // within <pre> tags should not be modified.
    var sContent;
    sContent = oAPGScript.Output.BufferContents;

    // Delete HTML comments
    // This does not delete comments with "#include" or < or [ brackets,
    // or comments immediately inside <script>.
    sContent = sContent.replace(/<!--(?!#include)[^<[]*-->(?!\s*<\/script>)/gi, "");

    // Replace multiple spaces and tabs with a single space
    sContent = sContent.replace(/[ \t]{2,}/g, " ");

    // Replace multiple CR-LFs plus spaces with a single CRLF
    sContent = sContent.replace(/\s*[\r\n][\r\n \t]*/g, "\r\n");

    // Delete whitespace between HTML tags
    sContent = sContent.replace(/>\s*</g, "><");

    // Replace the output buffer with the compressed text
    oAPGScript.Output.BufferContents = sContent;
    }

#%>

Multi-browser, XSL/T Example

This example turns buffering on, generates some XML, loads the XML from Output.BufferContents, then applies multiple XSL/T transforms to create versions of the content targeted for different browsers.

<%#
'=================================================
' multi_browser.apg
'
' Generates XML content, then applies 3 XSL/T
' transforms to the content to generate 3 different
' output files for 3 different classes of browsers:
'    * uplevel   - HTML 4.0 + CSS
'    * downlevel - HTML 4.0, no CSS
'    * WAP       - WAP 1.0
'=================================================

' Buffer output
Output.Buffer = True

' Generate XML content
#%><?xml version="1.0"?>
<body>
     . . .
</body>
<%#

' Load the contents of the output buffer into an XML object
Dim oXML
Set oXML = Script.CreateObject("Microsoft.XMLDOM")
If Not oXML.loadXML(Output.BufferContents) Then
     Log.Write "Error loading XML", apgSeverityFatal
     Script.Abort
End If

' Load 3 XSL stylesheets for different types of browsers
' If these XSL objects are used often, it would make sense
'     to cache them.
Dim oXSLUplevel, oXSLDownlevel, oXSLWAP
Set oXSLUplevel = Script.CreateObject("Microsoft.XMLDOM")
If Not oXSLUplevel.load(Script.Dir & "uplevel.xsl") Then
     Log.Write "Error loading uplevel XSL", apgSeverityFatal
     Script.Abort
End If
Set oXSLDownlevel = Script.CreateObject("Microsoft.XMLDOM")
If Not oXSLDownlevel.load(Script.Dir & "downlevel.xsl") Then
     Log.Write "Error loading downlevel XSL", apgSeverityFatal
     Script.Abort
End If
Set oXSLWAP = Script.CreateObject("Microsoft.XMLDOM")
If Not oXSLWAP.load(Script.Dir & "wap.xsl") Then
     Log.Write "Error loading WAP XSL", apgSeverityFatal
     Script.Abort
End If

' Clear the output buffer before writing transformed content
Output.ClearBuffer()

' Generate 3 different output files - one for each class of browser
Output.Filename = "uplevel.htm"
Output.Write oXML.transformNode(oXSLUplevel)

Output.Filename = "downlevel.htm"
Output.Write oXML.transformNode(oXSLDownlevel)

Output.Filename = "wap.htm"
Output.Write oXML.transformNode(oXSLWAP)

Output.Close()
#%>

Applies To

Output Object