|
*.^.*.^.*.^.*.^.*.^.*.^.*.^.*.^.*.^.*.^.*.^.*.^.*.^.*.^.*
TinyButStrong
version 3.3
*.^.*.^.*.^.*.^.*.^.*.^.*.^.*.^.*.^.*.^.*.^.*.^.*.^.*.^.* |
|
| |
Template Engine for Pro and Beginners
for PHP version 4.0.6 or higher
|
|
Table of Contents:
TinyButStrong (TBS) is a PHP class useful to develop an application in a clean way, separating PHP scripts and HTML files. With TBS, HTML pages are generated dynamically by merging a template with data. It is called a Template Engine.
The name TBS comes from the fact that this tool contains only 8 functions and yet, it is very powerful. It allows you to merge HTML page templates with your PHP variables or your MySQL, PostgreSQL, or SQLite.
TBS has been engineered so that you can develop your HTML page templates with ease using any visual HTML editors (like Dreamweaver or FrontPage). But if you are used to designing your HTML pages with a text editor, it is nice as well. TBS also enables you to create JavaScript dynamically.
As the name of it tells, TBS is easy to use, strong and fast. It is completely °~° freeware °~°.
On the HTML side:
You design a page which does not necessarily contain any PHP scripts, nor any programming. In this page you place TBS tags in the places where you want to display the dynamic data. This page is called a 'template'.
There are two types of tags: the 'fields' which are used to display dynamic data items, and the 'blocks' which are used to define an area, mostly in order to display records from a data source.
On the PHP side:
You use an object TBS variable to manage the merge of your HTML Template with the data. At the end, TBS shows the result of the merge.
| 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. |
Explanations and technical details:
TinyButStrong is a library written in PHP, it's a component to be referenced in your own PHP programs. In technical terms, TinyButStrong is a PHP 'class' ; the name of this class is clsTinyButStrong.
The variable $TBS that you add at the beginning of your PHP program enables you to execute the merge of your template from your PHP application. In technical terms, the variable $TBS is an 'instance' of the clsTinyButStrong class.
Example 1:
| 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> |
Example 2:
| 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> |
The merging of a template is done in a PHP program using an object variable declared as a clsTinyButStrong class.
Example of statement: $TBS = new clsTinyButStrong ;
This object allows you to load a template, to handle the merging of it with data, and then to show the result.
Example of PHP code:
include_once('tbs_class.php');
$TBS =& new clsTinyButStrong ;
$TBS->LoadTemplate('template.htm') ;
$TBS->MergeBlock('ctry','mysql','SELECT * FROM t_country') ;
$TBS->Show() ;
Here is the list of the TinyButStrong object's properties and methods:
Loads a template for the merging process.
The complete contents of the file is immediately stored in the
Source property of the TBS object, and
[onload] TBS fields are merged.
If the file is not found, then it will also be searched in the folder of the last loaded template (
since TBS version 3.2.0).
Syntax:
$TBS->LoadTemplate(string File{, string HtmlCharSet})
| Argument |
Description |
| File |
Local or absolute path of the file to load. |
| HtmlCharSet |
Optional. Indicates the character encoding (charset) to use for Html conversion of the data when they will be merged. It should be the same as the charset of the template. The default value is '' (empty string) which is equivalent to 'ISO-8859-1' (Latin 1).
If your template uses a special charset, then indicate the Html value for this charset.
In a Html page, the charset is placed at the beginning of the file, in the attribute 'content' of a <Meta> tag. The charsets supported by TBS are the charsets supported by the PHP function htmlentities(). For example: 'BIG5' (Chinese) or 'EUCJP' (Japanese). |
Scope of the file path:
If the file is nout found then its' also searched into the directory of the last loaded template, or the last loaded subtemplate.
Since TBS version 3.3.0, the file also searched into the
include_path.
No Html conversion:
If you use value
False as the parameter
HtmlCharSet, data will to not be converted when merged to the model.
User function:
If your charset is not yet supported by PHP, you can indicate a user function that will perform the Html conversion. For this, use the parameter
HtmlCharSet with the syntax '=myfunction'.
Since TBS version 3.0, it's also possible to indicate a method of a class (see
OOP).
Since TBS version 3.3.0, such a custom function should have a second argument for line break conversion.
Here is an example which gives the expected syntaxe:
function f_StrToXml($Txt string,$ConvBr boolean) {
// Convert a string into an XML text.
$x = htmlspecialchars(utf8_encode($Txt));
if ($ConvBr) {
$x = nl2br($x); // Convert any type of line break
$x = str_replace('<br />', '<text:line-break/>',$x);
return $x;
}
Adding the file at the end of the current template:
You can use the keyword
'+' instead of the the charset to have the file added to the end of the current template. Charset parameter stay the same as for the first template.
Modify the charset of the current template without loading a new one:
Since TBS version 3.3.0, using LoadTemplate(
'',
mycharset) will change the charset without modifying the current template.
Merges one or several
TBS blocks with records coming from a data source.
by default, this method returns the number of merged records (more exactly, it is the number of the last record), but it can also return the full merged record set (see argument
BlockName).
TinyButStrong supports several data source types in native:
Php data: an array, a string, a number.
Databases: MySQL ; PostgreSQL ; SQLite.
You can also add a new one: '
database plug-ins'.
Syntax:
int $TBS->MergeBlock(string BlockName, mixed Source{, string Query})
| Argument |
Description |
| BlockName |
Indicates the name of the TBS block to merge.
You can merge several blocks with the same data by indicating their names separated by commas. If you add '*' as a block name, then the method will return the full merged record set as a PHP array, instead of the number of records.
Versioning: the keyword '*' is supported since TBS version 3.0. |
| Source |
Indicates the data source to merge.
The table below shows the possible values according to the data source type. |
| Query |
Optional. Indicates the SQL statement which returns the records to merge.
The table below shows the possible values according to the data source type. |
Link between the block and the records:
The MergeBlock() method searches in your template for the specified TBS block name. Then, the block is repeated as many times as there are records in the data source.
To display the data of a record, you have to use a linked TBS Field. A TBS Field is linked when the name of it is composed of the block's name followed by a dot and a column's or a key's name in the record set. A linked field must be inside the block.
Example:
Block's name: block1
Columns returned by the query: field1,field2,field3
Linked TBS Fields: [block1.field1], [block1.field2], [block1.field3]
If no block's definition is found in the template, then the MergeBlock() method will merge the first record with all linked fields found in the template.
You can also define more advanced blocks. For more information, refer to chapter
TBS Blocks.
Merging several blocks with the same data:
You can merge several blocks with the same data by indicating their names separated by commas in the BlockName parameter. In this case, the query is opened only one time, and records are buffered to feed blocks.
Example: $TBS->MergeBlock('block1,block2,block3','mysql','SELECT * FROM MyTable');
Returning the full merged record set:
In some cases, it may be useful for you to retrieve the full record set after the merge. For that, you simply have to ad the keyword '*' in the list of block's names. Use this feature with care, because it save the merged data in the memory which can take resources.
Example: $data = $TBS->MergeBlock('bloc1,*','mysql','SELECT * FROM MaTable');
Counting the records:
To display the number of the record in the template, use a TBS Field linked to the virtual column '#'.
If you put this field outside the block, it will display the total number of records.
Example: [block1.#]
The virtual column '$' will display the key of the current record if the data source is a Php array.
Example: [block1.$]
Resource and Request arguments according to the data source type:
| Data Source Type |
Source |
Query |
| Text (*) |
The keyword 'text' |
A text |
| Number (*) |
The keyword 'num' |
A number or a special array (see below) |
| Clear (*) |
The keyword 'clear' |
- |
| Conditional (*) |
The keyword 'cond' |
- |
| PHP Array (*) |
A Php array |
- |
| The keyword 'array' |
A Php Array |
| The keyword 'array' |
A string that represents an array contained or nested in a PHP global variable (see below) |
| MySQL |
A MySql connection identifier or the keyword 'mysql' |
An SQL statement |
| A MySql result identifier |
- |
| PostgreSQL |
A PostgreSql connection identifier |
An SQL statement |
| A PostgreSql result identifier |
- |
| SQLite |
An SQLite connection identifier |
An SQLite statement |
| An SQLite result identifier |
- |
custom
|
A keyword, an object or a resource identifier not mentioned in this table.
See the chapter 'database plug-ins'. |
An SQL statement or something else. |
(*) See explanations in the chapter below.
Php data sources:
Text
The argument Source has to be equal to 'text'.
The whole block is replaced by the text (it must be a string) given as the Query argument. No linked Fields are processed except '#' which returns 1, or 0 if Query is an empty string.
Example: $TBS->MergeBlock('b1','text','Hello, how are you?');
Number
The argument
Source has to be equal to
'num'.
The argument
Query can be either a number or an array.
| arg Query |
Returned Record Set |
| Number: |
This number has to be positive or equal to zero. The returned Record Set consists of a column 'val' where the value goes from 1 to this number. |
| Array: |
This array has to contain a key 'min' and a key 'max' and eventually a key 'step'.
The returned Record Set consists of a column 'val' which goes from the 'min' value to the 'max' value. |
Examples:
$TBS->MergeBlock('b1','num',12);
$TBS->MergeBlock('b2','num',array('min'=>20,'max'=>30));
$TBS->MergeBlock('b3','num',array('min'=>10,'max'=>20,'step'=>2));
Clear
The argument Source has to be the keyword 'clear'.
All blocks and sections are deleted. It is the same thing as merging with an empty array.
Example: $TBS->MergeBlock('b1','clear');
Conditional
The argument
Source has to be the keyword
'cond'.
The block is merged like it was a
conditional blocks onload and
onshow. The block is not merged with data, and so it must have no linked TBS field. Each block section needs a parameter
when or a parameter
default. See
conditional blocks for more details.
Example:
$TBS->MergeBlock('bz','cond');
Array
The argument
Source has to be a PHP Array or the keyword
'array'. If you use the keyword
'array', then the argument
Query has to be a Php Array or a string that represents an array contained or nested in a global variable.
String syntax: 'globvar[item1][item2]...'
'globvar' is the name of a global variable $globvar which must be an array.
'item1' and 'item2' are the keys of an item or a subitem of $globvar.
Example:
$TBS->MergeBlock('block1','array','days[mon]');
This will merge 'block1' with the value $day['mon'] assuming it is an array.
It is possible to represent variable's name without items.
Example:
$TBS->MergeBlock('block1','array','days');
There are two advantages in using a string to represent the array:
-> Items will be read directly in the Array (assigned by reference) instead of reading a copy of the items. This can improve the performance.
-> You can use dynamic queries.
Displaying the key of current record:
You can use the virtual column '
$' which will display the key of the current record. This can be useful especially for
dynamic queries and sub-blocks.
Example: [block1.$]
Structure of supported arrays:
Items of the specified Array can be of two kinds: simple values with associated keys (case 1), or array values for whom items are themselves simple values with associated keys (case 2).
Case 1:
| Example: |
['key1']=>value1
['key2']=>value2
... |
The returned Record Set consists of a column
'key' containing the name of the key, and a column
'val' containing the value of the key.
Case 2:
| Example: |
[0] => (['column1']=>value1-0 ; ['column2']=>value2-0 ; ...)
[1] => (['column1']=>value1-1 ; ['column2']=>value2-1 ; ...)
[2] => (['column1']=>value1-2 ; ['column2']=>value2-2 ; ...)
... |
The returned Record Set consists of the columns
'column1',
'column2',... with their associated values.
Terminates the merge.
Syntax:
$TBS->Show({int Render})
The Show() method performs the following actions:
- Merge
[var] fields,
- Merge
[onshow] fields,
- Display the result (this action can be cancelled by
Render property/argument),
- End the script (this action can be cancelled by
Render property/argument).
The
Render property/argument allows to adjust the behavior of the Show() method. See the
Render property for more information.
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}{, boolean DefTags})
| Argument |
Description |
| BlockName |
The name of the block to search for. |
| Sections |
Optional. The default value is False.
If this parameter is set True the method returns an array that contains the definitions for all the sections of the named block. The first section is returned into the item [1] of the array. |
| DefTags |
Optional. The default value isTrue.
by default, the method GetBlockSource() returns the source of the block including its definition tags. If you'd like those tags to be deleted, then force the argument DefTags to False. If the block is defined with a simplified syntax then the definition tags will not be deleted anyway because they are also Field tags.
Versioning: this argument is supported since TBS version 3.0. |
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.
Replaces one or several TBS Fields with a fixed value or by calling a user function.
Since TBS version 3.0, it's also possible to indicate a method of a class (see
OOP).
Each TBS fields having the specified base name will be merged.
It is also possible to merge the special [var], [onload] and [onshow] (see below).
Syntax:
$TBS->MergeField(string BaseName, mixed X {, boolean FunctionMode})
| Argument |
Description |
| BaseName |
Base name of the TBS Fields. For example 'account'. |
| X |
The value to display (or a string that represent the name of a user function if the argument FunctionMode is set to true). |
| FunctionMode |
Indicates that the value to display is calculated by a user function. The default value is false. If this argument is set to true, then X must be a text string giving the name of the user function. This function must exist and have the syntax described below. |
Merging with a value:
X can be numeric, string, an array or an object. For an array or an object, names of TBS Fields must have suffixes like
[var] Fields.
Example:
$TBS->MergeField('account',array('id'=>55,'name'=>'Bob'));
In this example, the fields [account.id] and [account.name] will be merged.
Merging with a user function:
TBS calls this function for each field found in the template.
This function must have the following syntax:
function fct_user($Subname [, $PrmLst]) {...}
When the function is called, its argument
$Subname has for value the suffix of the field's name (example: for a field named
'ml.title',
$Subname will have the value
'title'). And the optional argument
$PrmLst contains an associative array with the field's parameters. The function must return the value to be merged.
Example:
$TBS->MergeField('ml','m_multilanguage',true);
...
function m_multilanguage($Subname) {
global $lang_id;
$rs = mysql_query("SELECT text_$lang_id AS txt FROM t_language WHERE key='$Subname");
$rec = mysql_fetch_array($rs);
return $rec['txt'] ;
}
In this example, a field such as [ml.title] will be merged with the value returned by m_multilanguage('title').
Merge special fields:
You can use the method MergeField() in order to force the merge of the special fields
[var],
[onload] and
[onshow]. but in this case, only the first argument should be indicated.
Example:
$TBS->MergeField('var');
Versioning: the merge of special fields is supported since TBS version 3.0. It replaces the old method MergeSpecial() which is not supported anymore.
Enables you to call a
TBS plug-in's command, or to install a TBS plug-in.
Syntax:
mixed $TBS->PlugIn(mixed arg1, mixed arg2, ...)
Remind: in order to have your TBS plug-in working, its PHP script must be included in your application before.
Example:
include_once('tbs_plugin_xxx.php');
And aslo, every TBS plug-in should have a key as explained at
Plug-ins.
Calling a plug-in's command:
Use the plug-in's key as the main argument. Next arguments are for the called plug-in's purpose.
Example:
$TBS->PlugIn(TBS_XXX,$arg1,arg2);
In this example, the plug-in identified by the key TBS_XXX is called.
Remark: when you call a plug-in's command for the first time this plug-in is automatically installed on the TBS instance (
$TBS).
Installing a plug-in:
Although some plug-ins are automatically installed, it can be useful in some other cases to make a manual installation. For this, use the constant TBS_INSTALL with the plug-in's key.
Example:
$TBS->PlugIn(TBS_INSTALL,TBS_XXX);
In this example, the plug-in identified by the key TBS_XXX is installed.
Remarks:
* A plug-in is installed relatively to a TBS instance (a variable $TBS for example). If you are using a second TBS instance (for example
$TBS2) then you will also need to install the plug-in on this instance.
*
A plug-in is installed automatically when you call one of its commands using the method PlugIn() (se above).
Versioning: the method PlugIn() is supported since TBS version 3.0.
Enables you to avoid all TinyButStrong error messages for next operations. Default value is
false. This property is dedicated to on line professional sites, because you will have no more indication about the good running of the merge. It is often more judicious to use parameter
noerr which enables you to avoid messages concerning one particular TBS tag.
Syntax:
boolean $TBS->NoErr
Example:
$TBS->NoErr = true; // no more error message displayed.
Versioning: property NoErr is supported since TBS version 3.0.
Indicates how the merging ends.
The value must be a combination of the following constants.
The default value is (
TBS_OUTPUT +
TBS_EXIT).
Syntax:
int $TBS->Render
The Render property changes the behavior the methods
Show() and
CacheAction().
| Constant |
Description |
| TBS_NOTHING |
Indicates that none of the actions below are processed at the end of the merge. |
| TBS_OUTPUT |
Indicates that the result of the merge must be displayed. TBS uses the Php command Echo. |
| TBS_EXIT |
Indicates that we have to quit the script just after the end of the merge. |
This property contains the source of the template currently merged. It is read/write.
When TinyButStrong processes a merging (when using the MergeBlock() method for example), the Source property is modified immediately.
Syntax:
string $TBS->Source
Notes:
- The
LoadTemplate() method loads a file into the Source property and merges the
[onload] fields automatically. Thus, Source may be different from the original template after LoadTemplate().
- The
Show() method merges fields
[onshow] and
[var] before to display the result.
In order to load a template stored into a Php variable, you can code:
$TBS->Source = $my_template;
$TBS->MergeField('onload'); // force the merge of [onload] fields if any
In order to store the result at the end of the merging, you can code:
$TBS->Show(TBS_NOTHING) // terminate the merging without leaving the script nor to display the result
$result = $TBS->Source;
Contains the array of template variables corresponding to current template.
Syntax:
array $TBS->TplVars
You can define template variables using one or several
onload automatic fields with parameter
tplvars.
All parameters that follow parameter
tplvars are added to the TplVars property when the LoadTemplate() method is called.
Example:
[onload;tplvars;template_version='1.12.27';template_date='2004-10-26']
This TBS tag will create two items equivalent to the PHP code:
$TBS->TplVars['template_version'] = '1.12.27';
$TBS->TplVars['template_date'] = '2004-10-26';
Remarks:
- Parameter
tplvars works only with
onload automatic fields.
- You can use parameter
tplvars several times in the same template.
Object Oriented Programming (OOP):
TinyButStrong integrate a technique to call methods or properties of objects that you've coded at the PHP side.
Calling methods of a class without created object:
The following TBS features support the call to the methods of a class without created object.
| Feature |
Example |
| Parameter ondata |
[blk1.column1;block=tr;ondata=MyClass.methA] |
| Parameter onformat |
[blk1.column2;onformat=MyClass.methB] |
| Method LoadTemplate() |
$TBS->LoadTemplate('mytemplate.htm','=MyClass.methC'); |
| Method MergeField() |
$TBS->MergeField('myfield','MyClass.methD',true); |
Remark: Methods call using this technique must respect the function syntax expected by the feature (see the description of the corresponding feature).
Calling created objects:
TBS has an ObjectRef property that is set to
false by default, and that you can use to reference your objects already created. You can reference an object directly on the ObjectRef property, or you can reference some using PHP arrays.
Example:
$TBS->ObjectRef =& $MyObject1;
You can use an array if you have several objects reference:
$TBS->ObjectRef['item1'] =& $MyObject1;
$TBS->ObjectRef['item2'] =& $MyObject2;
You can use as many levels as you wish:
$TBS->ObjectRef['item3']['a'][0] =& $MyObject4;
Remarks:
* Think to use the assignment by reference using "=&" instead of "=", otherwise a copy of the object will be created.
* Since an object is referenced under ObjectRef, its sub objects will also be accessible by the TBS syntax.
• Using ObjectRef in [var] fields:
Use the symbol '~' to call what is referenced under ObjectRef.
For example:
| The field |
|
Will call |
| [var.~propA] |
|
$TBS->ObjectRef->propA |
| [var.~propA.propB] |
|
$TBS->ObjectRef->propA->propB |
| [var.~item2.propA] |
|
$TBS->ObjectRef['item2']->propA |
| [var.~item2.methX] |
|
$TBS->ObjectRef['item2']->methX() |
| [var.~item2.methY(a,b)] |
|
$TBS->ObjectRef['item2']->methY('a','b') |
Remark:
TBS proceeds to a coherence control, it will determine itself whether your [var] field definition is calling to ObjectRef via an array's item, an object's property or an object's method. Anyway, take care that your [var] field must call a value at the end, not an object.
• Using ObjectRef in other TBS features:
The following TBS features support the call to the methods of objects referenced under ObjectRef.
| Feature |
Example |
| Parameter ondata |
[blk1.column1;block=tr;ondata=~item1.methA] |
| Parameter onformat |
[blk1.column2;onformat=~item1.methB] |
| Method LoadTemplate() |
$TBS->LoadTemplate('mytemplate.htm','=~item1.methC'); |
| Method MergeField() |
$TBS->MergeField('myfield','~item1.methD',true); |
| Method MergeBlock() |
$TBS->MergeBlock('blk1','~mydb','SELECT * FROM t_table'); |
Remark: Methods call using this technique must respect the function syntax expected by the feature (see the description of the corresponding feature).
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.
A TBS field is a TBS tag which has to be replaced by a single data item.
A TBS fields must have a name to identify it (which does not have to be unique) and can have parameters to modify the displayed value.
Syntax:
HTML ... [
FieldName{;
param1}{;
param2}{;
param3}{...}]
... HTML
| Element |
Description |
| FieldName |
The name of the Field.
Warning: names that begin with var. , onload and onshow are reserved. They are respectively used for [var] fields, and Automatic fields. |
| param1 |
Optional. One or more parameters from the list below and separated with ';'.
Some parameters can be set to a value using the equal sign '='.
Example: frm=0.00
If the value contains spaces, semicolons or quotes, then you can use single quotes as delimiters.
Example: frm='0 000.00'
Use two single quotes to define a normal single quote character in a delimited string.
Example: ifempty='hello that''s me'
It is possible to embed TBS fields. It means you can write this: [var.v1; if [var.v2]=1]. But:
- for [var] fields, you have to make sure that v2 will be merged before v1.
- for block fields, you have to make sure that column v2 is before column v1. |
Field's parameters:
| Parameter |
Description |
| htmlconv=val |
Enables you to force or prevent the conversion of the data item to Html text.
The value val can be one of the following keywords:
| yes: |
(default value) Force the conversion to Html including new lines. |
| no: |
Prevent the conversion to Html. Useful to modify Javascript code or to modify the Html source. |
| nobr: |
Force the conversion to Html but new lines (useful for <pre> tags for example). |
| wsp: |
Preserve white spaces (useful for spaces at the beginning of lines). |
| esc: |
No Html conversion and double the single quote characters ('). |
| js: |
Convert the data item to a string that can be inserted between JavaScript text delimiters. |
| look: |
Convert the data item to Html only if no Html entities are found inside the data item. |
You can specify several values using seperator '+'. Example : htmlconv=yes+js |
| . (dot) |
If the data item is empty, then an unbreakable space is displayed. Useful for cells in tables. |
| ifempty=val |
If the data item is empty, then it is replaced with the specified value. |
magnet=tag
or
magnet=expr |
Assign a magnet Html zone to the TBS field. A magnet tag is kept as is when the field has a value, and is deleted when the field is null or empty string. Parameter magnet supports the same syntax as parameter block, i.e. that expr must be an Html tag or a TBS extended block expression.
Example:
(<a href="[var.link;magnet=a]">click here</a>)
Result for $link='www.tbs.com': (<a href="www.tbs.com">click here</a>)
Result for $link='': ()
By default, the magnet Html zone should be delimited by a pair of opening-closing tags (like <a></a>) which first tag is placed before the TBS fields. But this can be changed using parameter mtype (see below).
Since TBS version 3.3.0, parameters ope=nif:x and ope=minv can be used to improve parameter magnet.
Remark: the parameters if then else are processed before parameter magnet. |
| mtype=val |
To be used with parameter magnet. Define the magnet type.
| Value |
Magnet behavior when field is null or empty string |
| m*m |
That's the default value. Delete the pair of tags that surrounds the TBS field. Everything that is between them is deleted also. The field can be put inside one of the tags.
Example:
(<a href="[var.link;magnet=a]">click here</a>)
Result for $link='www.tbs.com': (<a href="www.tbs.com">click here</a>)
Result for $link='': () |
| m+m |
Delete the pair of tags that surrounds the TBS field, but keeping everything else that is between the tags.
Example:
(<a href="mailto:[blk.email;magnet=a;mtype=m+m]">[blk.name]</a>)
Result for $email='me@tbs.com': (<a href="mailto:me@tbs.com">MyName</a>)
Result for $email='': (MyName) |
| m* |
Delete the single tag that is before the field, and everything that is between the tag and the field.
Example 1: <img href="[var.link;magnet=img;mtype=m*]">
Example 2: <br> [var.address;magnet=br] |
| *m |
Delete the single tag that is after the field, and everything that is between the tag and the field.
Example: [var.address;magnet=br;mtype=*m]<br> |
|
| comm |
Widen the bounds of the TBS Field up to the bounds of the commentary Html tags which surround it, or up to another specified couple of Html tags.
Example:
xxx <!-- [myfield;comm] here some comments --> yyy
or
xxx <div> [myfield;comm=div] here some comments </div> yyy
are strictly identical to:
xxx [myfield] yyy
This parameter is particularly useful for the template designing when you are using a Visual HTML Editor (such as Dreamweaver or FrontPage).
Versioning: Support for none commentary Html tags was added in TBS 3.0. |
| noerr |
Avoid some of the TBS Error messages. When a message can be cancelled, it is mentioned in the message. |
| file=filename |
Replace the field with the contents of the file.
Filename can be a string or an expression.
You can use the keyword [val] inside the expression to insert the current data item.
You can use [var] fields inside the expression.
Examples:
[onload;file=header.html]
[onload;file=[var.filename]]
If the file is not found, then it will also be searched in the folder of the last loaded template (since TBS version 3.2.0).
If filename is an empty string, then no error message is displayed, it is like parameter file is ignored. This can be used to manage conditional insertion.
Example:
[onload;file=[var.insert;if [val]=1;then 'header.html';else '']]
You will found more details about this paremater in the chapter Subtemplates.
See also: getbody script |
| getbody |
To be used with parameter file or script. Indicates that not all the file contents is loaded but only the body part delimited with tags <body> and </body>. It is possible to precise another couple of tag using the syntax getbody=tag.
Example:
[onload;file=header.htm;getbody]
Versioning: parameter getbody is supported since TBS version 3.0.
In previous versions, it was automatically processed when using parameter file. Now it becomes explicit. |
| script=filename |
Execute the Php script just before replacing the TBS field.
Filename can be a string or an expression.
You can use the keyword [val] inside the expression to insert the current data item.
You can use [var] fields inside the expression.
| * |
Take care that in your script variables will be obsiously local instead of global. This is because the script is called from a TBS method. In order to define or reach global variables in your script, you have to use the Php instruction global or the array $GLOBAL. |
| * |
TBS gives to you predefined local variables that can be used in your script:
- $CurrVal refers to the current value of the field. It can be modified.
- $CurrPrm refers to the array of field's parameters.
- $this refers to the current TBS instance. (See parameter subtpl for good usage) |
| * |
Parameter script is sensible to the if parameter. If there is a parameter if in the field, then the script is executed only if the condition is verified. |
See chapter 'Subtemplates' for more details about how to use this parameter in subtemplate mode. |
| subtpl |
To be used with the parameter script or parameter onformat.
Activate the subtemplate mode during the script or function execution.
See chapter 'Subtemplates' for more details. |
| if expr1=expr2 |
Display the data item only if the condition is verified, otherwise display nothing unless parameter then or else are used.
Supported operators are:
| = or == |
equal |
| != |
not equal |
| +- |
greater than |
| +=- |
greater than or equal to |
| -+ |
less than |
| -=+ |
less than or equal to |
| ~= |
match the regular expression (since TBS version 3.0) |
Both expr1 and expr2 must be string or numerical expressions.
You can use the keyword [val] inside the expression to insert the current data item.
You can use [var] fields inside the expression.
The expressions may contain other TBS fields, but you have to make sure that they are merged before the containing field.
Since TBS version 3.0, it is also possible to define several couples of if/then in the same field.
See parameters then and else for some examples. |
| then val1 |
If the parameter if is defined and its condition is verified, then the data item is replaced with val1.
Since TBS version 3.0, it is also possible to define several couples of if/then in the same field.
Examples:
[var.image;if [val]='';then 'image0.gif']
[var.x;if [val]=1;then 'one';if [val]=2;then 'two';else 'more']
You can use the keyword [val] inside the expression to insert the current data item.
You can use [var] fields inside the expression. |
| else val2 |
If the parameter if is defined and its condition is not verified, then the data item is replaced with val2.
Example:
[var.error_id;if [val]=0;then 'no error';else 'error found']
You can use the keyword [val] inside the expression to insert the current data item.
You can use [var] fields inside the expression. |
| onformat=fct_name |
Indicates the name of a user Php function that will be executed at the time when the value is formated for the merge. This allows typically to modify the text to display.
Since TBS version 3.0, it's also possible to indicate a method of a class (see OOP).
The function fct_name must have the following syntax:
function fct_name($FieldName,&$CurrVal,{&$CurrPrm,{&$TBS}}) { ... }
| |
Argument |
Description |
| |
$FieldName |
Gives the name of the current field (read only). |
| |
$CurrVal |
Gives the value of the current field (read/write ; don't forget the & character in the statement). |
| |
$CurrPrm |
Optional. Gives the PHP array containing the parameters for the current field (Don't forget the & character in the statement). |
| |
$TBS |
Optional. Gives the current TBS instance. (Don't forget the & character in the statement).
Use this argument with lot of care. It is used sometimes for the subtemplate mode. |
See chapter 'Subtemplates' for more details about how to use this arguments in subtemplate mode. |
| protect=val |
Enables you to protect or unprotect the data item to be merged by replacing the characters '[' with their corresponding Html code '['. The value val can be one of the following keywords:
yes: (default value) data item is protected.
no: data item is not protected.
By default, all data merged with a template is protected except if it's a file inclusion. It is strongly recommended to protect data when it comes from free enter like on a forum for example. |
| ope=action |
Makes one or several operations on the value to merge. You can define several operations to be processed in order by separating them with coma (,).
Example:
[var.x;ope=add:-1,mod:10]
Supported operations are:
| max:n |
Limit the text string to a maximum of n characters. If the string is cut, then its end is replaced with dot lines '...'.
Example: [var.caption;ope=max:10]
Add parameter maxhtml to indicate that the value before merging can contain Html characters.
Add parameter maxend to change the cutting symbol.
Example:
[var.caption;ope=max:10;maxhtml;maxend='+'] |
| mod:n |
Apply the modulo n to the value to merge. Example: [var.numlig;ope=mod:7] |
| add:n |
Add the numeric n to the value to merge. Example: [var.number;ope=add:-1] |
| mul:n |
Mutlyplies the value to merge by the numeric n. |
| div:n |
Divises the value to merge by the numeric n. |
| list |
If the value before merging is a Php Array, then its items are displayed separated with a coma (,).
Example: [var.myarray;ope=list]
Add parameter valsep in order to change the item separator.
Example: [var.myarray;ope=list;valsep='+'] |
| nif:x |
If the value is equal to 'x' then it is replaced with '' (empty string). This operation is designed to make parameter magnet to work with other values than ''. (supported since TBS version 3.3.0) |
| minv |
Replace the value with '' (empty string) but parameter magnet will consider the old value. This operation is designed to make totaly invisible a TBS field which do magnet stuffs. (supported since TBS version 3.3.0) |
Versioning:
-
Parameter ope is supported since TBS version 3.0.
It replaces parameter max which doesn't exist since this version.
- Multiple operations and 'mul" and "div" are supported since TBS version 3.2 |
| frm=format |
Specify a format to display for a data item which type is date/time or numeric. It is possible to use a conditional format which changes depending on the sign of the value. The format is considered as numeric type as soon as it contains the character 0, otherwise it is considered as date/time type.
Date-time format:
It is a VisualBasic like format. The following keywords are recognized:
| d, dd, ddd, dddd: |
number of the day, number of the day in two digits, short name of the day, full name of the day. Use parameter locale to display locale names. |
| xx |
displays st, nd, rd or th depending to the number of the day. |
| w |
number of the day in the week (from 0 to 6) |
| m, mm, mmm, mmmm: |
number of the month, number of the month in two digits, short name of the month, full name of the month. Use parameter locale to display locale names. |
| yy, yyyy: |
year in two digits, full year. |
| hh, rr, nn, ss: |
hour-24, hour-12, minutes, seconds forced on two digits. |
| h, r |
hour-24, hour-12 |
| hm, ampm, AMPM : |
12h format of the hour, "am" or "pm" signal , "AM" or "PM" signal. |
Other characters are kept.
It is possible to protect the strings inside by putting them between double quotes (").
Examples:
[fld;frm=mm/dd/yyyy] will display 12/21/2002
[fld;frm='yyyy-mm-dd hh:nn:ss'] will display 2002-12-21 15:45:03
Versioning:
- Keywords ampm and AMPM are supported since TBS version 3.0.
- Keyword hm was supported since TBS 3.0 and is deprecated since TBS version 3.2.
- Keywords rr, r, and h are supported since TBS version 3.2.
Numeric format:
To define the decimal part, use an expression like '0x0...' where 'x' is the decimal separator , and '0...' is a continuation of zeros corresponding to the number of decimals.
If there is no decimal, use the format '0.' (with a dot).
To define a thousand separator, use an expression like '0z000x...' where 'z' is the thousand separator. If there is no decimal, use the format '0z000.' (with a dot).
If the format contains the character '%', then the value to display will be multiplied by 100. The character '%' is displayed too.
The numerical format may contain other strings. But only the expression with one or more zeroes placed to the right will be taken as a format, other characters will be kept.
Examples:
| Value |
Field |
Display |
| 2456.1426 |
[fld;frm='0.000'] |
2456.143 |
| |
[fld;frm='$ 0,000.00'] |
$ 2,456.14 |
| |
[fld;frm='$ 0,000.'] |
2,456 |
| 0.2537 |
[fld;frm='0.00 %'] |
25.37% |
| |
[fld;frm='coef 0.00'] |
coef 0.25 |
Conditional formats:
You have the possibility to define up to 4 conditional formats when the value is respectively positive, negative, zero or null (or empty string). Conditional formats must be separated by a '|' character. Each conditional format is optional.
Examples:
| Value |
Field |
Display |
| 2456.1426 |
[chp;frm='+0.00|-(0.00)|*|empty'] |
+2456.14 |
| -156.333 |
[chp;frm='+0.00|-(0.00)|*|empty'] |
-(156.33) |
| 0 |
[chp;frm='+0.00|-(0.00)|*|empty'] |
* |
| null |
[chp;frm='+0.00|-(0.00)|*|empty'] |
empty |
| -8.75 |
[chp;frm='+0.00|-(0.00)'] |
-(8.75) |
|
| locale |
To be used with the parameter frm.
Indicates that the format specified with frm must display locale day and month's names.
Locale information can be set using the PHP function setlocale().
Example:
[chp;frm='dd mmmm yyyy';locale] will display 21 décembre 2002 if you have defined before setlocale(LC_TIME,'fr'); at the PHP side.
Remark:
* Parameter locale works only if local parameters have been configured on your server. If it's so, then the function setlocale() used with valid arguments will return the value true.
* For reasons due to PHP, the keyword xx for frm does not work with parameter locale.
* For reasons due to PHP, the keyword d fro frm works like dd with parameter locale. |
| tplvars |
Enables you to define variables in the template that you can retrieve in the Php programm using TplVars property. Works only with onload automatic fields. |
Order of processing parameters:
When you whant to use several paremeters in the same TBS field, then it can be intersing to undertsand in which order they are processed.
Each parameter is changing the value to be merged, but the source from where the value is is taken is not changed.
Order:
1) -> Retreiving the value of the field to be merged.
2) -> Changing the value using parameters:
2.1) ->
onformat
2.2) ->
ope / plug-ins OnOperation
2.3) ->
frm / Html or other text conversion
2.4) ->
if
2.5) ->
file
2.6) ->
script
2.7) -> . /
ifempty /
magnet
3) -> Inserting the value in the template.
A [var] field is a TBS Field which displays a Php variable.
The name of it must be composed by the keyword '
var.' followed by the name of the Php variable.
The parameters for standard TBS Fields are available for [var] fields.
For example [var.
x] will be replaced by the value of
$x. User variables and the predefined variables can be merged
only if they are global.
You can also merge array's items, object's properties or object's methods using a dot (".") as separator.
Resource variables are ignored.
For example:
| [var.arr.item1] |
|
will display |
|
$arr['item1'] |
| [var.arr.item2.a.0] |
|
will display |
|
$arr['item2']['a'][0] |
| [var.obj.prop1] |
|
will display |
|
$obj->prop1 |
| [var.obj.methA] |
|
will display |
|
$obj->methA() |
| [var.obj.methB(x,y)] |
|
will display |
|
$obj->methB('x','y') |
| [var.arr.item3.prop2.item4 |
|
will display |
|
$arr['item3']->prop2['item4'] |
Versioning: methods with arguments inside [var] fields are supported since TBS version 3.0.
When are [var] fields merged?
[var] fields are merged when you are calling the
Show() method. Except for parameters
file,
script,
if,
then,
else and
when. [var] placed inside one of those parameters will be merged when the parameter is processed.
Examples:
The field [var.x] will be merged during Show().
But in [onload;when [var.x]=1;block=div] it will be merged during LoadTemplate() because of the [onload] field.
You can also force the merge of [var] fields or other types at anytime using the
MergeField() method.
Versioning: [var] fields are processed inside parameters "then" and "else" only since TBS version 2.02.
Embedded [var] Fields
The TBS tags which contain embedded [var] fields may not be merged as you could wish. You have to remember that [var] fields are merged only during Show() (except for few parameters named above).
Example:
[b1.name;block=tr;headergrp=[var.x]]
in this example, [var.x] will not be merged yet when you call
$TBS->MergeBlock('b1',...)
The header group will then be badly defined.
You have to do one of the following :
- use a
onload [var] field supported since TBS 3.2.0 :
[b1.name;block=tr;headergrp=[onload.x]]
-
call
$TBS->MergeField('var') before
$TBS->MergeBlock('b1',...)
- use a customized type of field
[b1.name;block=tr;headergrp=[zzz]] merged using
$TBS->MergeField('zzz',$x)
Security: how to limit [var] fields usage in templates?
You can limit the [var] fields usage by defining an Allowed Variable Prefix when you create the TinyButStrong object.
Example :
$TBS = new clsTinyButStrong('','x1_');
In this example, only PHP global variables prefixed by 'x1_' are allowed in the template. Other [var] fields will produce an explicit error message when merging.
[var.x1_title] will be merged if the global variable $x1_title exists.
[var.x2_title] will produce an explicit error message.
NB: the first parameter '' de clsTinyButStrong() in the example above is used to define TBS tag delimiters. But this is not described in this manual.
An onload [var] field is like a [var] field but it will be merged during LoadTemplate() instead of during Show().
It is coded by using the prefix onload instead of var.
Example: [onload.x]
Onload [var] fields are very useful to optimize your template when a PHP variable has to be merged inside a block, or ensure that a PHP variable is merged when you call MergeBlock().
Please not that onload [var] fields are different from
[onload] automatic blocks.
Versioning: onload [var] are supported since TBS version 3.2.0
A Special [var] field is a TBS Field which displays data provided by the TinyButStrong system.
The name of a Special [var] field has to begin with '
var..', followed by a keyword in the list below.
The parameters for standard TBS Fields are available for Special [var] fields.
Example:
Date of the day : [
var..now;frm='mm-dd-yyyy']
| Name |
Description |
| var..now |
Date and hour of the server. |
| var..version |
The version of TinyButStrong. |
| var..script_name |
The name of the PHP file currently executing. |
| var..template_name |
The name of the last loaded template file.
It is the name given to the LoadTemplate() method. |
| var..template_date |
The creation date of the last loaded template file. |
| var..template_path |
The directory of the last loaded template file.
It is the directory given to the LoadTemplate() method. |
| var..tplvars.* |
The value of an item set in the TplVars property.
('*' must be the key of an existing item in the array) |
| var..cst.* |
The value of a PHP constant.
(* must be the name of an existing constant) |
| var..tbs_info |
Information about TBS and installed plug-ins. |
Versioning: special var fields "cst" and "tbs_info" are supported since TBS version 3.2.0
When are Special [var] fields merged?
Special [var] fields are merged with normal [var] fields. That is 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
MergeField() method.
A TBS block enables you define a zone and to display data from a record source.
You can define a TBS block using one or two TBS tags (see below).
Merging with data:
Merging a block with data is done using the
MergeBlock() method. When a TBS block is merged with data, it is repeated as many times as there are records; and the associated TBS fields are replaced by the value of the columns stored in the current record.
A TBS field associated to a block is identified by its name which should be made of the name of the block followed by the name of the column to display and separated by a dot.
Examples:
- [Block1.ColA] This field will display the value of column ColA when block Block1 is merged.
- [Blokc1.ColB;frm='dd-mm-yyyy'] Another field but with a TBS parameter.
Remark: when two separated blocks have the same name, then they will be considered has two sections of the same block. All content placed between those two sections of a block will be
ignored and deleted during the merging. See
sections of blocks to know more about sections.
Block syntaxes:
There are three possible syntaxes to define a TBS block:
Explicit Syntax:
Two TBS tags are used. One for the beginning of the block and another for the end of the block.
Example:
HTML...[BlockName;block=begin;params]...HTML...[BlockName;block=end]...HTML
Those TBS tags for the block definition will be deleted during the merging.
Relative Syntax:
The block is defined by a pair of opening-closing Html tags which is given by a single TBS tag.
Example:
HTML...<tag_name...>...[BlockName;block=tag_name;params]...</tag_name...>...HTML
This TBS tag for the block definition must be placed between the pair of Html tags.
This TBS tag will be deleted during the merging.
Remark: You can aslo define a block's zone by a combination of HTML tags. See parameter
block for more details.
Simplified Syntax:
An associated TBS field is used to define the block in a relative way (see the relative syntax above).
Example:
HTML...<tag_name...>...[BlockName.ColumnName;block=tag_name;params]...</tag_name...>...HTML
The TBS tag for the block definition (i.e. the
block=
... parameter) must be placed between the pair of Html tags. You are nor obliged to put the parameter
block on the first field, it can be any of them inside the zone defined by the block.
Remarks:
•
You s