|
*.^.*.^.*.^.*.^.*.^.*.^.*.^.*.^.*.^.*.^.*.^.*.^.*.^.*.^.* |
|||||||
Template Engine for Pro and Beginners for PHP version 4.0.4 or higher |
Introduction: |
Basic principles: |
Installation: |
1. | Copy the file tbs_class.php in a directory of your Web site. |
2. | At the beginning of your PHP program, add the lines: include_once('tbs_class.php'); $TBS = new clsTinyButStrong ; Remark: if the TBS file tbs_class.php is in a different directory than your application, then you have to precise the directory in front of the TBS file name. |
Mini examples: |
Html Template | Php Program | Result |
<html> <body> [var.message] </body> </html> |
<? include_once('tbs_class.php'); $TBS = new clsTinyButStrong ; $TBS->LoadTemplate('template.htm') ; $message = 'Hello' ; $TBS->Show() ; ?> |
<html> <body> Hello </body> </html> |
Html Template | Php Program | Result |
<table> <tr><td>[blk.val;block=tr]</td></tr> </table> |
<? include_once('tbs_class.php'); $TBS = new clsTinyButStrong ; $TBS->LoadTemplate('template.htm') ; $list = array('X','Y','Z') ; $TBS->MergeBlock('blk',$list) ; $TBS->Show() ; ?> |
<table> <tr><td>X</td></tr> <tr><td>Y</td></tr> <tr><td>Z</td></tr> </table> |
PHP side: |
include_once('tbs_class.php'); |
method LoadTemplate(): |
Loads a template for the merging process. The complete contents of the file is stored in the Source property of the TBS object. Syntax: $TBS->LoadTemplate(string File{, string HtmlCharSet})
|
method MergeBlock(): |
Merges a TBS block with
records coming from a data source. Returns the number of the last displayed record (the first is number 1). TinyButStrong supports several data source types in native: Php data: an array, a string, a number. Databases: MySQL ; PostgreSQL ; ODBC ; SQL-Server ; ADODB. You can also add a new one: 'adding a data source type'. There is a display 'By Page' mode, described below. Syntax: int $TBS->MergeBlock(string BlockName, mixed Source{, string Query}{, int PageSize, int PageNum}{, int RecCount})
Link between the block and the records:
Counting the records:
Resource and Request arguments according to the data source type:
(2) This is the ADODB data access method from Microsoft, available through the Php COM class. Php data sources:
By Page mode:
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
method Show(): |
Terminates the merge. Syntax: $TBS->Show() The Render property allows to adjust the behaviour of the Show() method. By default, Show() processes the Special TBS Fields (Php variable Fields, System Fields, include file, conditional blocks,...), displays the result of the merge, and then quits the program. |
method CacheAction(): |
Activates the Cache System or starts
another operation on cache files. Syntax: bool $TBS->CacheAction(string CacheId {, int ActTimeOut}{, string Dir})
Instead of the time-out, you can use one of the constants below in order to start a special action of the Cache System.
The Cache System enables you to speed up the display of HTML pages by proceeding the merge at regular times instead of at each call of the page. For this, you must prepare a unique string identification for each page that should be saved (we call it 'cache file'), and also a refresh period (we call it time-out). When you call the CacheAction() method the System will look for an existing cache file and get its creation time. If the creation time is shorter than the time-out then the contents of the cache file are loaded and the merge ends. If the creation time is longer than the time-out then the cache file is ignored but it will be updated at the next call of the Show() method by saving the result of the merge in this cache file. If the cache file is loaded then the method returns True, otherwise it returns False. By default, if the file is loaded then the contents are displayed and the script is ended but you can change this behaviour using the Render property. |
method GetBlockSource(): |
Returns the source of the TBS Block. Only the definition of the first section of block will be returned, unless the Sections argument is set to True. If no block is found, the method returns False. Syntax: string $TBS->GetBlockSource(string BlockName {, boolean Sections})
This method enables you to get the source of a block in order to manually handle the merging. After that, if you need to replace the block with text, you can use the MergeBlock() method with the 'text' parameter. |
method MergeField(): |
Replaces a TBS Field in the template with a
value. Syntax: $TBS->MergeField(string FieldName, mixed Value)
If several TBS Fields have the same name in the template, they will all be processed. |
method MergeNavigationBar(): |
Displays a navigation bar based on specific TBS block
and TBS fields. Syntax: $TBS->MergeNavigationBar(string NavName, int NavSize, int PageNum [, int RecCount, int PageSize])
|
method MergeSpecial(): |
Replaces the special blocks and fields of the specified type. Syntax: $TBS->MergeSpecial(string Type)The argument Type has to be one of the following values:
Remark: By default, the Show() method replaces all the special fields and blocks just before showing the merge result. That's why it is rare to use MergeSpecial() in a program. |
property Render: |
Indicates how the merging ends. Syntax: int $TBS->Render
|
property Source: |
Get or set the HTML source on which the merge process is applied. After the call to the LoadTemplate() method, this property contains the HTML source of the template. This property enables you to read or modify the result of the merge, in your code. Syntax: string $TBS->Source |
Global TBS variables: |
TinyButStrong provides global
variables that you can use for your PHP program. |
Adding a data source type: |
You can add another data source type not yet
supported in native by TinyButStrong. For that, you have to code three functions with specific statements, and names corresponding to the type to add. Do not add the functions in the TBS source file, code them in your application or in an external Php script. You can find additional data source types at the TinyButStrong web site. TBS identifier: The $Source argument that you pass to the MergeBlock() method has a specific TBS identifier that you must use for the function naming.
The TBS identifier may be arranged by TBS to make it fit for a function name. Example:
Statements of the functions: The three functions to add in your application must have the following syntax: Replace the keyword 'customdb' with the TBS identifier of your data source type. function tbsdb_customdb_open(&$Source,&$Query) {...} This function must open the required query and return a Record Set identifier. In case of error, the function should return the value False, and can display a message.
Example:
function tbsdb_customdb_fetch(&$Rs{,$RecNum}) {...} This function has to return an associative array corresponding to the current record, with columns' names and values. The function has to return the value False when there is no record left.
Example:
If your data source needs to know the number of the expected record, you can add the argument $RecNum to your function's statement. But in other cases, this argument is optional because all records are called in order anyway. function tbsdb_customdb_close(&$Rs) {...} This function has to close or free the Record Set identifier. It doesn't have to return a value.
Example:
|
HTML side: |
You design your template by placing TBS tags in the places where data items should appear.
There are two types of TBS tags: Fields and Blocks.
A TBS Field is a TBS tag which has to be replaced by a single data item.
It is possible to specify a display format and also other parameters. The syntax
for TBS Fields is described below.
A TBS Block is an area which has to be repeated. It is defined using
one or two TBS fields.
Most often, it is the row of an HTML table. The syntax for TBS Blocks is
described below.
TBS Fields: |
A TBS Field is a TBS tag which
has to be replaced by a single data item. It has a name which enables you to identify it and parameters can be supplied in order to change the display behaviour. Syntax: HTML ... [FieldName;params] ... HTML
|
PHP variable Fields: |
A Php variable Field is a TBS Field which display
a Php variable. The user variables and the predefined variables can be merged but
they must be global variables. Object or Resource variables
are ignored. |
System Fields: |
A System Field is a TBS Field which displays
data provided by the TinyButStrong system.
When are System Fields merged? System Fields are merged in the Show() method, this means just before the display of the merge result. But you can force the merge at any time with the MergeSpecial() method. |
TBS Blocks: |
A TBS block enables you to display data from a
record source. The merging between the block and the data is done using the MergeBlock() method. During the merge, the TBS block is repeated as many times as there are records; and the associated TBS fields are replaced by the value of the columns. A TBS field associated to Block1 and displaying the value of the column ColumnA must be named Block1.ColumnA Example: [Block1.ColumnA;frm='mm-dd-yyyy'] Two blocks with the same name will be regarded as two sections of the same block (see sections of blocks). Block syntaxes: There are three possible syntaxes to define a TBS block: Explicit Syntax:
Which syntax to use? The 'absolute' syntax is rarely used with Visual Editors because TBS tags have often to be placed between two Html tags. On the other hand, it is convenient for textual editors. The 'relative' syntax enables you to indicate a block using only one TBS tag. Furthermore, there is no need to hide the TBS tag because it will be deleted during the displaying. This syntax is quite practical. The 'simplified' syntax is really simple. It enables you to define a TBS block and a TBS Field with only one TBS tag. This syntax is the most current and the most practical. Tip: You can use the 'relative' or the 'absolute' syntax with custom tags having the Html standard. Example: <custom_tag>Hello [blk1.column1;block=custom_tag], how are you?</custom_tag> Block's parameters:
|
Sections of block: |
Different blocks having the same name will be regarded
as sections of the same block. Sections can be used to: - alternate the display (normal sections), - display something if there is no data (NoData section), - display a header each time the value of a column changes (HeaderGrp section). Normal sections: When you define several normal sections, they will be used alternatively for each record.
NoData section: Display the section if the data source has no records. The NoData section is defined by adding the parameter nodata.
HeaderGrp section: Display a header section every time a column's value in the record set changes. A Header is defined by adding the parameter headergrp=column_name.
|
Serial display (in columns): |
The serial display enables you to display several
records inside a block. For this, you have to use a main block and secondary
blocks.
Syntax: The main block and its secondary blocks are merged using only one call to the MergeBock() method. The main block must be defined using the parameter serial. The secondary blocks must be nested into the main block. The secondary block's names must be the name of the main block followed by "_" and a number indicating display order.
Empty secondary block: You can specify a special secondary block that will be used to replace unused secondary blocks (without records). This "Empty" secondary block must have the index 0. It can either be placed inside the main block with the normal secondary block, or alone inside another serial block. The "empty" secondary block is optional.
Remark: The serial display also works with sections of block and dynamic queries. |
Dynamic queries / sub-blocks: |
Principles of the dynamic queries: It is possible to use the MergeBlock() method with a dynamic query. In your template, you have to define a block by adding the parameters p1, p2, p3,... with their values. The query given to the MergeBlock() method has to contain marks such as %p1%, %p2%, %p3%, ... in order to welcome the values of the parameters p1, p2, p3,... . Each section of the block to be merged that contains a parameter p1 will be computed as a separate block for which the dynamic query is re-executed. The sections of the block that have no parameter p1 are combined with the previous section with a parameter p1.
Use with sub-blocks: Dynamic queries enable you to easily build a system of a main-block with sub-blocks. Here is how you can do it: - Create a main block, and then a sub-block inside the main block. - Link them by adding to the sub-block a parameter p1 whose value is a field from the main block. - At the PHP side, merge the main block first, and then the sub-block.
Remarks: - The parameter htmlconv=esc enables you to pass protected string values to the query. - The dynamic queries also work with sections of block and serial display. |
Display a navigation bar: |
TinyButStrong is able to display a navigation bar
based on specific blocks. The navigation bar is merged using the MergeNavigationBar() method.
Here are the elements your can use for building your navigation bar: - Use a normal TBS block to display the common page numbers. - Use another TBS block with the same name but with the parameter currpage to display the current page with a different presentation. - Use the following fields wherever you want (inside a block or outside a block):
Tip: If you use the parameter endpoint on those fields, then when the current page is the first or the last page, the fields will return an empty string ('') instead of the page number. This enables you to manage display exceptions with the parameter friend for example.
|
Include a sub-template: |
If a TBS field has the parameter file,
then this field will be replaced by the contents of the indicated file
during the merging of this file. The value of the parameter file can
be a string value or an expression made by Php variable fields [var.*]
and keywords [val] (see definition of TBS fields). TinyButStrong retrieves the contents of the file as is. If it's a Php script, then this script won't be executed. To insert the result of a Php script, see 'Include the result of another PHP script'. If the file to include is an Html file, then TinyButStrong will keep only the body (delimited by a pair of tags <body> and </body>. The parameter htmlconv (optional) enables you to precise if the contents of the file have to be converted to Html or not. By default, it is not converted to Html.
How to precise when the sub-template is included: Use the special fields named tbs_include.onload and tbs_include.onshow to precise when the field is merged. - A field named tbs_include.onload is automatically merged when the LoadTemplate() method is called, just after the template is loaded. - A field named tbs_include.onshow is automatically merged when the Show() method is called.
|
Include the result of another PHP script: |
If a TBS field has the parameter script,
then the script will be executed at the field merging. The value of the
parameter script can be a string value
or an expression made by Php variable fields [var.*] and keywords [val]
(see
definition of TBS fields).
You can use global TinyButStrong variables into your script. Variable scope: To use global variables into your script, you have to use the global statment or the variable $GLOBALS because the script is executed like it was a function. Redirecting the display statements: If your Php script contains display statements (such as echo), then the text will be displayed normally; that is instantly and thus without waiting for the result of the template merging. To avoid this behaviour, you can use the parameter getob which enables you to redirect the text to replace the TBS field. With getob: texts passed to echo statements will be displayed at the place of the TBS fields. Without getob: texts passed to echo statements will be displayed normally, that is instantly before the result of the merge. Prevent the script from being executed several times: If the name of the script appears several times in your TBS fields, you can use the parameter once in order to limit the script to one execution. How to precise when the script is executed: You can use the special fields named tbs_include.onload and tbs_include.onshow to precise when the script is executed. For more details on those special fields, see 'Include a sub-template'. |
Conditional display overview: |
TinyButStrong offers several tools for conditional
display of fields and blocks. For a field that returns a value, you can use the common field parameters, repeated below. For other cases, you can use special TBS tags named tbs_check. They don't return a value and are dedicated to conditional display. Their use is detailed below. Conditional display parameters for a field that returns a value: For more information about the definition of fields, see TBS Fields.
How to use the tbs_check tags: The TBS tags named tbs_check are special tags which are processed automatically during the Show() and MergeSpecial() methods. They can be used for a block or field. -> Without suffix: A tbs_check tag without suffix represents a field. It should be used with the parameters if, then and else in order to display a value or another depending on the condition.
-> With suffix: A tbs_check tag with a suffix represents a block. They must have a 'block' parameter. tbs_check blocks with the same suffix are in the same group. In a group, the first block with a verified condition is displayed, and others are deleted. Using the 'else' parameter, you can have an extra block which is displayed if no condition is verified in the group.
|
Summary: |
TBS Field's parameters: |
|
TBS Block's parameters: |
|
Fields and parameters for Navigation bar: |
|
Names of Special Fields and Blocks: |
|
.:*~*:._.:*~*:._.:*~*:._.:*~*:._.:*~*:._.:*~*:._.:*~*:._.:*~*:._.: