APGen Documentation Previous Topic: APGScript.StartInDebugger Next Topic: APGScript.Abort() Parent Topic: APGScript Object    APGScript Object
APGScript.Globals
See Also:

The Globals object is an IDispatch object containing script global variables and procedures.

Syntax

VBScript:

oAPGScript.Globals.[variable|procedure]

JScript:

oAPGScript.Globals.[variable|procedure];

Object

oAPGScript An APGScript object.

Notes

The Globals object provides a way to run script procedures and access script global variables from outside of the script.

Before the APGScript object is run, the Globals object only contains the intrinsic script objects.  After the APGScript object has been Run(), the Globals object gives access to all global variables and global procedures in the script.  The procedures can be called, and the variables can be read and written.

Example

This example solves a common problem when building web sites:  Consider a situation where you want to write and maintain web site content in a WYSIWYG HTML editor.  You also want to use page templates, so all the pages on the site have a common look and feel.  And, you want to keep page content separate from site-wide templates.  The problem is, how do you extract parts of your (many) content pages to be inserted into the template content when building your site.

This example solves the problem of extracting content from a web page.  The idea is to wrap the content to be extracted in an APG script procedure.  Then, the procedure is called, the content is written to the output stream.  The APGScript.Globals object is used to call the procedure from outside of the script.

To add a new page, a content page is written in an HTML editor.  The content in real-world pages is often long and involved, but to save space, we'll use a simple content page.  Next, wrap the content to be extracted in an APG script procedure.  HTML editors can still be used to develop page content after you've inserted the APG script markers.

content_page.htm (editable in Front Page, HomeSite, etc.):

<HTML><HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
<TITLE>Example content page</TITLE>
<STYLE>
BODY={
     font-size: x-small;
     font-family: verdana, arial, helvetica;
     }
</STYLE>
</HEAD>

<BODY LINK="#0000ff" VLINK="#800080">
<H2>Content Page Heading</H2>
<%#
Sub PageBody
#%>
<P>First paragraph in content page body.</P>
<P>Second paragraph in content page body.</P>
<%#
End Sub
#%>

</BODY>
</HTML>

This next APG script contains a routine that extracts the PageBody content from the rest of the page, and writes it to the current output file.  The trick is to run the content page as an APG script, with buffering on.  All the global page content is discarded using Output.ClearBuffer().  Then, the output stream of the content page is directed to the output stream of the calling (template) page, and the PageBody sub in the content page is called.  See Pluggable Streams for more on this technique.

writepagebody.apg:

<%#

' ------------------------------------------------------------------------
' WritePageBody
'
' Call this sub to extract the body content from a content page, and
' write it to the Output.Stream.
'
' Note: The file sContentPath must have a PageBody subroutine
'  defined.
'

Sub WritePageBody(sContentPath)

     ' Open the APG script sContentPath
     Dim oContentScript
     Set oContentScript = APGen.OpenScript(sContentPath)

     ' Turn buffering on
     oContentScript.Output.Buffer = True

     ' Run oContentScript - this generates all the global content
     oContentScript.Run

     ' Discard all the global content
     oContentScript.Output.ClearBuffer()

     ' Now, turn buffering off
     oContentScript.Output.Buffer = False

     ' And share our Output.Stream
     oContentScript.Output.Stream = Output.Stream

     ' Call the PageBody sub in oContentScript
     ' This writes PageBody content to our current output file.
     oContentScript.Globals.PageBody
     
End Sub

#%>

The next APG script is an example template page.  Normally template content will be fairly complex, and may contain a bit of APG script code to generate the proper template content.  For this example, we save space by using header and footer comments.

Template.apg includes writepagebody.apg to use the WritePageBody() routine.  For real-life use, the content_page path, output_page path, and other variables would be passed into the template script instead of hard-coded.

template.apg:

<!-- #include file="writepagebody.apg" -->
<%#

' -------------------------------------------------------
' This file would normally contain standard web page
' header and footer HTML where the comments are.
' -------------------------------------------------------

Output.Filename = "output_page.htm"

#%> <!-- Header HTML Content -->
<%#

WritePageBody("content_page.htm")

#%> <!-- Footer HTML Content -->

After running template.apg, the file output_page.htm is generated:

 <!-- Header HTML Content -->

<P>First paragraph in content page body.</P>
<P>Second paragraph in content page body.</P>
 <!-- Footer HTML Content -->

Applies To

APGScript Object