CGI: VBScript Banner Footer: Difference between revisions

From The Uniform Server Wiki
Jump to navigation Jump to search
(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...")
 
(Punctuation and grammatical changes; some clarification.)
 
Line 3: Line 3:
'''VBScript Banner and Footer'''
'''VBScript Banner and Footer'''


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


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


__TOC__
__TOC__
Line 14: Line 14:
|
|
===Basic page template===
===Basic page template===
Basic page template is shown on right.
A sbsic page template is shown on right.


* Every page starts with a shebang line followed by a Content-type line.
* 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 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'''
* 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.
* The 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""
'''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.
* Create a new file test4.vbs with content as shown on right.
Line 53: Line 53:
|
|
The script produces output as shown on right.
The script produces output as shown on right.
Admittedly nothing spectacular a real page banner may include
Admittedly it's nothing spectacular. A real page banner may  
such items as images, text or even news announcements.
include such items as images, text or even news announcements.
Similarly the footer can be as complex as you like.
Similarly, the footer can be as complex as you like.


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


Line 72: Line 72:
|}
|}


'''''[[#top | Top]]'''''
 
==File Includes==
==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.
Changing either the banner or footer requires editing each page in your site. This is time consuming and prone to errors. The ideal solution is to save the banner and footer code in external files and include these with appropriate include statements.


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


{|
{|
Line 85: Line 85:




'''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.  
'''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.  
|
|
<pre>
<pre>
Line 103: Line 103:
</pre>
</pre>
|}
|}
'''''[[#top | Top]]'''''
 
==Test Script 5==
==Test Script 5==
In test folder UniServer\www\vbs_test create three new files as shown blow:
In test folder UniServer\www\vbs_test, create three new files as shown below:


{|
{|
Line 129: Line 129:
|
|
===Test script===
===Test script===
The test page includes the above function and original banner and footer text have been replaced with calls to this function.
The test page includes the above function, and the 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.


These calls pull in the appropriate files and run the code they contain.
===Run test script===
===Run test script===
* Start Apache if not already running
* Start Apache if not already running
* Enter: <nowiki>http://localhost:8081/vbs_test/test5.vbs</nowiki>
* Enter: <nowiki>http://localhost:8081/vbs_test/test5.vbs</nowiki>
* Result shown below
* Result is shown below
===Result===
===Result===
Result shown below:
Result shown below:
Line 148: Line 148:
<span style="font-weight:bold;font-size:16px;color:green">Footer CGI Test 5</span>
<span style="font-weight:bold;font-size:16px;color:green">Footer CGI Test 5</span>
</div>
</div>
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.
A slight disadvantage with this solution is that 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  
'''Note:''' Included files are not restricted to the file extension .vbs . Any other suitable extension can be used, such as .inc or .txt .


|
|
Line 192: Line 192:
|}
|}


'''''[[#top | Top]]'''''
== Summary ==
== 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.
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 [[CGI: JavaScript CGI|JavaScript]] section.
Although the above include file function can be used, there is a better alternative. This is covered in the [[CGI: JavaScript CGI|JavaScript]] section.


'''''[[#top | Top]]'''''
==Where to next==
==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. [[CGI: URL Encoding|Next page]] covers URL Encoding.
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. The data sent is in a special format, referred to as URL encoding. The [[CGI: URL Encoding|next page]] covers URL Encoding.


'''''[[#top | Top]]'''''
----
----



Latest revision as of 21:22, 9 November 2011

VBScript Banner and Footer

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

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

Test Script 4

Basic page template

A sbsic page template is shown on right.

  • Every page starts with a shebang line followed by a Content-type line.
  • The 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
  • The 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 it's 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


File Includes

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

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
'==============================================================================

Test Script 5

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

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 the 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 is 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 is that 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, such as .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

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.

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. The data sent is in a special format, referred to as URL encoding. The next page covers URL Encoding.