CGI: VBScript Banner Footer

Revision as of 23:17, 7 November 2011 by Ric (talk | contribs) (Created page with "{{Nav CGI}} '''VBScript Banner and Footer''' Generally web pages consist of three parts a banner, page content and footer. This page covers a very basic page template and a fu...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

VBScript Banner and Footer

Generally web pages consist of three parts a banner, page content and footer.

This page covers a very basic page template and a function to simulate PHP’s file include .

Test Script 4

Basic page template

Basic page template is shown on right.

  • Every page starts with a shebang line followed by a Content-type line.
  • Next three lines are the minimum tags required for an html page. You can add tags such as meta, link and script as required for your application.
  • The page body is split into three sections banner, content and footer
  • Last three lines are html closing tags and an optional script return code.

Note: Replace HTML single parameters quotes with double quotes for example style="color:red" becomes style=""color:red""

  • Create a new file test4.vbs with content as shown on right.
  • Start Apache if not already running
  • Enter: http://localhost:8081/vbs_test/test4.vbs
'!c:/windows/system32/cscript //nologo
Wscript.Echo "Content-type: text/html" & vbLF & vbLF

WScript.Echo "<html>"
WScript.Echo "<title>Test 4</title>"
WScript.Echo "<body>"

'--Banner
Wscript.Echo "<h2 style=""color:red"" >Banner CGI Test 4</h2>"

'--Content
WScript.Echo "<p>VBScript CGI Test 4<br />"
WScript.Echo "Page Content</p>"

'--Footer
Wscript.Echo "<h3 style=""color:green"">Footer CGI Test 4</h3>"

WScript.Echo "</body>"
WScript.Echo "</html>"

Wscript.Quit 0

The script produces output as shown on right. Admittedly nothing spectacular a real page banner may include such items as images, text or even news announcements. Similarly the footer can be as complex as you like.

Generally the same banner and footer are used on every page providing your site with a consistent look.

Banner CGI Test 4

VBScript CGI Test 4

Page Content

Footer CGI Test 4

Top

File Includes

Changing either the banner or footer requires editing each page in your site. This is time consuming and prone to errors. Ideal solution is to save the banner and footer code in external files and includes these with an appropriate include statement.

The following may come, as a surprise unlike PHP there is no way in either VBScript or JavaScript to directly include an external file.

Simulate PHP File Includes

Function shown on right simulates PHP’s file include by using the VBScript ExecuteGlobal function.


Note: You can pass either an absolute path or just a file name to the function. Specifying only a file name requires that file, to exist in the folder where the working script resides.

'==============================================================================
' This sub simulates PHP's include directive
sub includeFile (fSpec)
 dim objFSO,objFile,fileData
 Set objFSO  = CreateObject("Scripting.FileSystemObject")'* Create file obj
 Set objFile = objFSO.OpenTextFile(fSpec)                '* Open file for read
 fileData = objFile.readAll()                            '* Read file to string
 objFile.close                                           '* Close file
 executeGlobal fileData                                  '* Run code in string
 set objFSO  = nothing                                   '* Clean-up remove obj
 set objFile = nothing                                   '* Clean-up remove obj
end sub
'==============================================================================

Top

Test Script 5

In test folder UniServer\www\vbs_test create three new files as shown blow:

This file contains code that generates your site banner.

test5_banner.vbs

Wscript.Echo "<h2 style=""color:red"">Banner CGI Test 5</h2>"

This file contains code that generates your site footer.

test5_footer.vbs

Wscript.Echo "<h3 style=""color:green"">Footer CGI Test 5</h3>"

Test script

The test page includes the above function and original banner and footer text have been replaced with calls to this function.

These calls pull in the appropriate files and run the code they contain.

Run test script

  • Start Apache if not already running
  • Enter: http://localhost:8081/vbs_test/test5.vbs
  • Result shown below

Result

Result shown below:

Banner CGI Test 5

VBScript CGI Test 5

Page Content

Footer CGI Test 5

A slight disadvantage with this solution the simulated include file function must be included on every page of your site. That said it is a relatively small amount of code resulting in a positive advantage of having the ability to include files.

Note: Included files are not restricted to the file extension .vbs any other suitable extension can be used for example .inc or.txt

test5_footer.vbs

'!c:/windows/system32/cscript //nologo
Wscript.Echo "Content-type: text/html" & vbLF & vbLF

WScript.Echo "<html>"
WScript.Echo "<title>Test 5</title>"
WScript.Echo "<body>"

'--Banner
includeFile("test5_banner.vbs")

'--Content
WScript.Echo "<p>VBScript CGI Test 5<br />"
WScript.Echo "Page Content</p>"

'--Footer
includeFile("test5_footer.vbs")

WScript.Echo "</body>"
WScript.Echo "</html>"

'==============================================================================
' This sub simulates PHP's include directive
sub includeFile (fSpec)
 dim objFSO,objFile,fileData
 Set objFSO  = CreateObject("Scripting.FileSystemObject")'* Create file obj
 Set objFile = objFSO.OpenTextFile(fSpec)                '* Open file for read
 fileData = objFile.readAll()                            '* Read file to string
 objFile.close                                           '* Close file
 executeGlobal fileData                                  '* Run code in string
 set objFSO  = nothing                                   '* Clean-up remove obj
 set objFile = nothing                                   '* Clean-up remove obj
end sub
'==============================================================================
Wscript.Quit 0

Top

Summary

The include file function is not limited to loading a banner or footer file it also allows you to have a separate file containing a set of common functions for your site.

Although the above include file function can be used there is a better alternative, this is covered in the JavaScript section.

Top

Where to next

An import reason for using CGI scripts is to process data sent with a page request. For example this can be data entered in a form this data sent is in a special format referred to as URL encoding. Next page covers URL Encoding.

Top