HTML template componentallows to create HTML, XML, SGML and text reports in Delphi and C++ Builder application |
This is a component for Delphi and C++ Builder.IntroductionNeed to create HTML, XML or text reports ?! Use this !
It works like well known ASP, JSP and PHP technologies and allows to produce HTML (XML, SGML, text) page from template.
However it works at application side without Web server engine and can access application's objects and data.
To design report view it is possible to use your favorite editor. For example, Netscape Composer, MS FrontPage, MS Word, Notepad and any other.
Report creation process contains two steps:Template page script may contain absolutely all what you want and what the scripting language accept.
- creation of template script from template page source
- execution of the script to receive the end report
It may, for example, iterate through dataset, move to any dataset position, make SQL query and so on.
Step 1: creating template script
Step 2: executing script and receiving report
|
Available versions, downloading
I. Customized template code tags.
By default it is "<template" and "/>".
You can setup own tag, for example "<%" and "%>" as is used by Microsoft's ASP and Sun's JPS.See below in detail.
II. Customized text output functions formats.
By default it is write_hex("%s") and write_str(%s).
You can setup, for example, report.out_x("%s") and report.out(%s) or any other in dependence of scripting language and application's objects hierarchy.See below in detail.
III. Template text can be loaded from any URL. By default only "file://" URL type is implemented.
You can provide method to load from other URL types like:It is very flexible to load report templates from database BLOB fields.
- http://
- ftp://
- database://
- any other
Database URL example:You need only to execute that query and get BLOB field text.
- database://select report_text from report_templates where report_type = "Report 1"
See below in detail.
IV. The "@ include file=" instruction allow to prepare report from any number of pieces.
For example, it will be standard report header, company logo, report footer etc.See below in detail.
V. Component is compatible with any type of script engines and scripting languages.
See below how to use with MS ActiveX scripting and the "ActiveX Shell" technology.
Available compiled versions for:How to installCompiled versions for other Delphi and C++ Builder releases is not available now.
- CBuilder 3
- Delphi 3
- Delphi 4
- Delphi 5
Use the source code with those releases.Source code compatible with Delphi 3 or higher and C++ Builder 3 or higher.
Download now latest version of htmltmpl.zip (~100 Kb).
Template page syntax
1. Unzip archive htmltmpl.zip with subdirectories. 2. Directory CB3\ is for CBuilder 3 users.
Directory D3\ is for Delphi 3 users.
Directory D4\ is for Delphi 4 users.
Directory D5\ is for Delphi 5 users.Install html_template_reg.pas from Menu > Component > Install Component.
By default HTML_Template will installed to "Apelseen" components page.
3. Just drop HTML_Template component to the form at Designer when need it. Examples source code is located in the corresponding directory ( CB3\, D3\, D4\, D5\ ) too.
Also see examples below.
Description | Syntax | Example | In script |
Contain a code fragment valid in the scripting language. Including any declaration and expressions. | <template code fragment /> | <template
set dataset = Query1 while not dataset.eof /> |
set dataset = Query1 while not dataset.eof |
Contain an expression code fragment valid in the scripting language. Only one expression is allowed. | <template = expression /> | <template =
dataset.tablefield("name") /> |
write_str(dataset.tablefield("name")) |
Contain one page instruction. It works like compiler directives. Supported instructions:
|
<template @ instruction /> | <template
@ include page = "file://page_header.html" /> <template @
|
(script from included page) (nothing) |
Any other text and tags. | text <tag > text text ... | This is text that will in end report as is.
It has hexadecimal representation in script. |
write_hex("3C7461626C653E0D0A") |
Syntax notesUsage
- You can setup own tags instead of "<template" and "/>" tags. For example "<%" and "%>" as is used by Microsoft's ASP and Sun's JPS. It is possible to use ANY other. For example, "<template>" and "</template>" pair will not so bad. Or <?template and ?> will be all right too.
- You can setup own text output functions formats instead of write_hex("%s") and write_str(%s) that will in corresponding to scripting language syntax and application's objects hierarchy.
Properties
Name | Type | Default value | Description |
URL | String | file://template.html | URL to the report template page source. It is used by Prepare function. |
Code_begin | String | <template | Template code open tag. |
Code_end | String | /> | Template code close tag. |
Expr_prefix | String | = | Expression prefix. |
Write_Hex | String | write_hex("%s") | Format for hexadecimal string output function. |
Write_str | String | write_str(%s) | Format for expression result output function. |
Line_length | Integer | 50 | Maximum length of one piece of long string which will be
placed as hexadecimal string. It is needed if used script engine can not
support too long lines. Real script line length will be
equal:
length(Write_hex) + Line_length * 2.because one char can be saved in two hexadecimal digits. |
Language | String | It is readonly property which @page language= instruction can set
only. It is available when OnScriptRun is called. It is flexible if there is templates with different script languages. | |
RunScript | Boolean | True | If value is False then OnScriptRun event is not called. It is flexible for getting script without its auto execution. |
Methods
Name | Description |
Create | constructor Create(AOwner : TComponent);
This is constructor. |
Prepare | function Prepare : AnsiString;
It executes script preparation process with template page given as URL property. It returns script as result. |
Prepare_URL | function Prepare_URL (URL :
AnsiString) : AnsiString;
It executes script preparation process with template page given as URL parameter. It returns script as result. |
Prepare_Text | function Prepare_Text (text : AnsiString) :
AnsiString;
It executes script preparation process with template page given as single String given as text parameter. It returns script as result. |
DefaultOpenURL | procedure DefaultOpenURL(URL : AnsiString; var PageText
: AnsiString);
This is default URL open procedure. It returns document from URL as single String in PageText variable. OnOpenURL event implementation may replace it. Note: only "file://" URLs are implemented now. |
Events
Name | Description |
OnOpenURL | procedure (Sender : TObject; URL : AnsiString; var
PageText : AnsiString) of object;
Use it to implement your own URL types.
|
OnScriptRun | procedure (Sender : TObject; script : AnsiString) of
object;
Use it to execute script by used script engine.
|
Professional version's advantages
How to create template pagestep 1.
Design HTML page using your favorite editor like Netscape Composer, MS Fron Page, MS Word etc. If it will report from dataset then add and edit table columns. Add company logo, report name and others.
This is HTML code you have.
<html>
<head>
<title>Report example 1</title>
</head>
<body>
<h3>Company: </h3><table BORDER WIDTH="90%" >
<!-- this is table's header -->
<tr>
<td><b>Sale date</b></td>
<td><b>Items total</b></td>
<td><b>Amount paid</b></td>
</tr><!-- this is table's first row -->
<tr>
<td></td>
<td></td>
<td></td>
</tr></table>
<br>
<b>AmountPaid total: </b></body>
</html>step 2.
Add template tags to the page using your HTML editor or manually in HTML code.
For example, in Netscape Composer use Menu > Insert > HTML Tag.
This is example of manually added VBScript code.
<html>
<head>
<title>Report example 1</title>
</head>
<body>
<h3>Company: <template= report.paramfield("Company") /></h3><table BORDER WIDTH="90%" >
<!-- this is table's header -->
<tr>
<td><b>Sale date</b></td>
<td><b>Items total</b></td>
<td><b>Amount paid</b></td>
</tr>
<!-- this is table's first row -->
<template
AmountPaid_sum = 0
Set dataset = report
dataset.first()
do while not dataset.eof()
/>
<tr>
<td><template = dataset.tablefield("SaleDate") /></td>
<td><template = dataset.tablefield("ItemsTotal") /></td>
<td><template = dataset.tablefield("AmountPaid") /></td>
</tr>
<template
AmountPaid_sum = AmountPaid_sum + dataset.tablefield("AmountPaid")
dataset.next()
loop
/></table>
<br>
<b>AmountPaid total: <template= AmountPaid_sum /></b>
</body>
</html>step 3.
Now you have template page. Place it as file to disk or as BLOB field into database.
step 1.
Place HTML_Template component to the form at Designer.
Edit component's URL property. Only "file://" URLs are implemented now. It is possible to implement other URL types by using OnOpenURL event. See OnOpenURL implementation.step 2.
Implement OnScriptRun event. This is needed to execute prepared script. See OnScriptRun example implementation.
step 3.
Call component's Prepare method. Prepare method do this:
- loads template page text from URL
- prepares script
- calls OnScriptRun implementation
- returns prepared script as result
OnOpenURL implementationThis is implementation for URLs like "database://...".
procedure Form1.html_template1OpenURL(Sender : TObject; URL : AnsiString;
var PageText : AnsiString);var sql_text : AnsiString;
sql_query : TQuery;
begin// It is possible to use "abbrev" function defined in html_template module
// to check that one string begin like anotherif abbrev(URL, 'database://' ) then begin
with TQuery.Create(nil) do begin
SQL.Add( copy(URL, length('database://') + 1), length(URL) );
try
Open();
if FieldCount > 0 then PageText := Fields[0].AsString;
except
PageText := '';
end;
Close();
Free;
end;
end
else begin
(Sender as THTML_Template).DefaultOpenURL(URL, PageText);
end;end;
How to run script ? It is your choice. It is possible your application already uses some script engine.
As you see in example above, some additional methods are used by template code: "first", "eof", "next", "tablefield" and "paramfield". Moreover we have to provide two methods named "write_str" and "write_hex" to output result from the script.
The example below uses the "ActiveX Shell" technology to enable scripting on Deplhi application. It is available for downloading. In detail about the "ActiveX Shell" technology for Delphi and C++ Builder see its manual.In that manual MS ScriptControl downloading and usage is described too. Note that you need MS ScriptControl installed to run example. Also see: http://msdn.microsoft.com/scripting/
It is possible with "ActiveX Shell" technology to extend any Delphi component to add ActiveX methods. But in the example one object is used to provide all functionality to script. It is THTML_Report. Its source code is provided in file all \ html_report.pas.
This is OnScriptRun implementation example with it.
// Query1 : TQuery;
// Query2 : TQuery;
// SaveDialog1 : TSaveDialog;uses
ShellApi, ComObj, HTML_report, activex_shell;procedure Form1.HTML_Template1ScriptRun(Sender : TObject; script : AnsiString);
var this_form, scripting, module: Variant;
report : THTML_Report;
begin// creating object which will provide all functionality to the script
report := THTML_Report.Create(Self);
report.Name := 'Report';try
// assigning datasets to that object
report.ParamDataSet := Query1;
report.TableDataSet := Query2;
// assigning output stream to that object
if not SaveDialog1.Execute() then Exit;
report.Stream := TFileStream.Create(SaveDialog1.FileName, fmCreate);// creating instance of MS Script Control
scripting := CreateOLEObject('ScriptControl');
scripting. AllowUI := False;
scripting. Language := 'VBScript';// creating "ActiveX Shell" for the object
this_form := obj_to_variant(Self);// creating script module for the object
module := scripting.Modules.Add('report_module', this_form );// executing script and receiving the report
module.ExecuteStatement(script);// opening report
ShellExecute(Application.Handle,PChar('open'),PChar(SaveDialog1.FileName),
nil,nil,SW_SHOWNORMAL);// freeing of object
finally
if report.Stream <> nil then report.Stream.Free;
report.Free;
end;end;
The "ActiveX Shell" technology is used in this example to create ActiveX object from Delphi object. Of course it is possible to use HTML component separately. Just create ActiveX component by using Delphi ActiveX framework (File > New and then ActiveX > AutomationObject ). And then make OnScriptRun event handler as in example above.
It is necessary to provide a correct script execution. The ActiveX object must have methods that you decide to use in script, in example above it was - "first", "eof", "next", "tablefield" and "paramfield". Plus two methods that write to the stream. In example above it was - "write_str" and "write_hex".
Implementation will like this:
... // creating instance of MS Script Control
scripting := CreateOLEObject('ScriptControl');
scripting. AllowUI := False;
scripting. Language := 'VBScript';// creating your ActiveX object
report := CreateOLEObject('Your ActiveX object');// assigning some parameters to that object
// such as datasets, output stream
// ...// adding object to script control's name space
scripting.AddObject('report', report, True);// executing script and receiving the report
scripting.ExecuteStatement(script);...
Now all page instructions like @ include page and other @ instructions works only in professional version.Source code buy online
The source code of professional version costs only 30 $.Contact information
Click the image and buy it online.After receiving the payment, maximum per two days we send you source texts with comments by e-mail or you can download it from our secure web site.
Thank you for support !
Note that you are buying only HTML template component without "ActiveX Shell" technology used in examples. Buy it separately here if you need.
Contact person: Karim YusupovUseful Internet linksSend your questions and comments to mailto:apelseen@mail.com?Subject=About HTML template component
Apelseen software website is http://www.apelseen.da.ru/
Version 1.2
Copyright (c) 1999, Apelseen software. All Rights Reserved.