Skip to content

Commit 9dc63e5

Browse files
author
Skrol29
committed
TBS 3.13.0
1 parent 6079f2e commit 9dc63e5

File tree

3 files changed

+169
-24
lines changed

3 files changed

+169
-24
lines changed

CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
66

7-
## [3.12.3] - 2021-12-03
7+
## [3.13.0] - 2022-02-07
88

99
### Enhancements
1010

11-
- PHP 8.1 compatibility
11+
- Compatibility with PHP 8.1
12+
13+
### Added
14+
15+
- New methods SetVarRefItem() and GetVarRefItem() : uniformize and ensure compatibility to set a VarRef items.
1216

1317
## [3.12.2] - 2020-11-03
1418

tbs_class.php

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
*
44
* TinyButStrong - Template Engine for Pro and Beginners
55
*
6-
* @version 3.12.3-beta-1 for PHP 5, 7, 8
7-
* @date 2021-12-03
6+
* @version 3.13.0 for PHP 5, 7, 8
7+
* @date 2022-02-07
88
* @link http://www.tinybutstrong.com Web site
99
* @author http://www.tinybutstrong.com/onlyyou.html
1010
* @license http://opensource.org/licenses/LGPL-3.0 LGPL-3.0
@@ -655,7 +655,7 @@ class clsTinyButStrong {
655655
public $ExtendedMethods = array();
656656
public $ErrCount = 0;
657657
// Undocumented (can change at any version)
658-
public $Version = '3.12.3-beta-1';
658+
public $Version = '3.13.0';
659659
public $Charset = '';
660660
public $TurboBlock = true;
661661
public $VarPrefix = '';
@@ -822,6 +822,66 @@ public function ResetVarRef($ToGlobal) {
822822

823823
}
824824

825+
/**
826+
* Get an item value from VarRef.
827+
* Ensure the compatibility with PHP 8.1 if VarRef is set to Global.
828+
*
829+
* @param string $key The item key.
830+
* @param mixed $default The default value.
831+
*
832+
* @return mixed
833+
*/
834+
public function GetVarRefItem($key, $default) {
835+
836+
if (is_null($this->VarRef)) {
837+
838+
if (array_key_exists($key, $GLOBALS)) {
839+
return $GLOBALS[$key];
840+
} else {
841+
return $default;
842+
}
843+
844+
} else {
845+
846+
if (array_key_exists($key, $this->VarRef)) {
847+
return $this->VarRef[$key];
848+
} else {
849+
return $default;
850+
}
851+
852+
}
853+
854+
}
855+
856+
/**
857+
* Set an item value to VarRef.
858+
* Ensure the compatibility with PHP 8.1 if VarRef is set to Global.
859+
*
860+
* @param string $key The item key.
861+
* @param mixed $value The item value. Use NULL in order to delete the item.
862+
*/
863+
public function SetVarRefItem($key, $value) {
864+
865+
if (is_null($this->VarRef)) {
866+
867+
if (is_null($value)) {
868+
unset($GLOBALS[$key]);
869+
} else {
870+
$GLOBALS[$key] = $value;
871+
}
872+
873+
} else {
874+
875+
if (is_null($value)) {
876+
unset($this->VarRef[$key]);
877+
} else {
878+
$this->VarRef[$key] = $value;
879+
}
880+
881+
}
882+
883+
}
884+
825885
// Public methods
826886
public function LoadTemplate($File,$Charset='') {
827887
if ($File==='') {

tbs_us_manual.htm

Lines changed: 100 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,15 @@
169169
border: 1px dotted #6699CC;
170170
}
171171
.note {
172-
background-color: #D9FFD9;
172+
background-color: #D9FFD9; /* green */
173173
border: thin solid #00EC9F;
174174
padding: 2px;
175175
margin-top: 3px;
176176
margin-left: 20px;
177177
}
178+
.warning {
179+
background-color: #f4dfdf; /* light red */
180+
}
178181
-->
179182
</style>
180183
</head>
@@ -188,13 +191,13 @@
188191
</tr>
189192
<tr>
190193
<td>Date:</td>
191-
<td>2021-08-27</td>
194+
<td>2022-02-07</td>
192195
</tr>
193196
</table>
194197
</div>
195198
<div style="margin:auto; width:500px; text-align:center;">
196199
<h1>TinyButStrong Documentation</h1>
197-
<div class="txt-tiny">version 3.12</div>
200+
<div class="txt-tiny">version 3.13</div>
198201
<div style="margin-top:10px;">Template Engine for Pro and Beginners<br>
199202
PHP 8<br>
200203
PHP 7<br>
@@ -270,7 +273,19 @@ <h2>Table of Contents:</h2>
270273
</tr>
271274
<tr>
272275
<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#php_getoption"><span class="txt-small">method</span> GetOption()</a></td>
273-
<td>get TBS options</td>
276+
<td>get TBS options</td>
277+
</tr>
278+
<tr>
279+
<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#php_resetvarref"><span class="txt-small">method</span> ResetVarRef()</a></td>
280+
<td>reset the VarRef property</td>
281+
</tr>
282+
<tr>
283+
<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#php_getvarrefitem"><span class="txt-small">method</span> GetVarRefItem()</a></td>
284+
<td>get an item in the VarRef property</td>
285+
</tr>
286+
<tr>
287+
<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#php_setvarrefitem"><span class="txt-small">method</span> SetVarRefItem()</a></td>
288+
<td>set an item in the VarRef property</td>
274289
</tr>
275290
<tr>
276291
<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="#php_replacefields"><span class="txt-small">method</span> ReplaceFields()</a></td>
@@ -1322,6 +1337,45 @@ <h3><a id="php_getoption"></a>Method GetOption():</h3>
13221337
<p class="versioning">Versioning: methods GetOption() is supported since TBS version 3.8.0. </p>
13231338
</div>
13241339

1340+
<h3><a id="php_resetvarref"></a>Method ResetVarRef():</h3>
1341+
<div class="norm">
1342+
<p>Reset the <a href="#php_varref" class="opt-name">VarRef</a> property.</p>
1343+
<p>By default, VarRef refers to the <a href="https://www.php.net/manual/en/reserved.variables.globals.php">PHP global variable $GLOBALS</a>.</p>
1344+
<p> Syntax: <span class="txt-code"><span class="opt-type">mixed</span> <span class="opt-name">$TBS</span>-&gt;ResetVarRef(<span class="opt-type"><span class="opt-value">boolean</span></span> <span class="opt-name">$to_globals</span>)</span></p>
1345+
<table border="0" cellpadding="5" cellspacing="0">
1346+
<tr>
1347+
<th>Argument</th>
1348+
<th>Description</th>
1349+
</tr>
1350+
<tr>
1351+
<td align="left" valign="top" class="opt-name"><span class="txt-code">$to_globals</span></td>
1352+
<td align="left" valign="top">Indicates if the property VarRef refers to $GLOBALS or is a new empty array.
1353+
<br>- if <span class="opt-name">$to_globals</span> is <span class="txt-code"><span class="opt-type">true</span></span> then VarRef refers to $GLOBALS.
1354+
<br>- if <span class="opt-name">$to_globals</span> is <span class="txt-code"><span class="opt-type">false</span></span> then VarRef is a new empty array.</td>
1355+
</tr>
1356+
</table>
1357+
</div>
1358+
1359+
<h3><a id="php_getvarrefitem"></a>Method GetVarRefItem():</h3>
1360+
<div class="norm">
1361+
<p>Get an item value in the <a href="#php_varref" class="opt-name">VarRef</a> property.</p>
1362+
<p>This function uniformizes the way to get a <a href="#php_varref" class="opt-name">VarRef</a> item, whether VarRef refers to <a href="https://www.php.net/manual/en/reserved.variables.globals.php">$GLOBALS</a> or not.</p>
1363+
<p> Syntax: <span class="txt-code"><span class="opt-type">mixed</span> <span class="opt-name">$TBS</span>-&gt;GetVarRefItem(<span class="opt-type"><span class="opt-value">string</span></span> <span class="opt-name">$key</span>, <span class="opt-type"><span class="opt-value">mixed</span></span> <span class="opt-name">$default</span> = <span class="opt-value">null</span>)</span></p>
1364+
<p>If VarRef if a dedicated array, then this function is the same as doing <span class="txt-code">$TBS->VarRef[$key] ?? $default;</span></p>
1365+
<p class="versioning">Versioning: methods GetVarRefItem() is supported since TBS version 3.13.0.</p>
1366+
</div>
1367+
1368+
<h3><a id="php_setvarrefitem"></a>Method SetVarRefItem():</h3>
1369+
<div class="norm">
1370+
<p>Set an item value in the <a href="#php_varref" class="opt-name">VarRef</a> property.</p>
1371+
<p>This function uniformizes the way to set a <a href="#php_varref" class="opt-name">VarRef</a> item, whether VarRef refers to <a href="https://www.php.net/manual/en/reserved.variables.globals.php">$GLOBALS</a> or not.</p>
1372+
<p> Syntax: <span class="txt-code"><span class="opt-type">mixed</span> <span class="opt-name">$TBS</span>-&gt;SetVarRefItem(<span class="opt-type"><span class="opt-value">string</span></span> <span class="opt-name">$key</span>, <span class="opt-type"><span class="opt-value">mixed</span></span> <span class="opt-name">$value</span>)</span></p>
1373+
<p>If VarRef if a dedicated array, then this function is the same as doing <span class="txt-code">$TBS->VarRef[$key] = $value;</span></p>
1374+
<p class="versioning">Versioning: methods SetVarRefItem() is supported since TBS version 3.13.0.</p>
1375+
</div>
1376+
1377+
1378+
13251379
<h3><a id="php_replacefields"></a>Method ReplaceFields():</h3>
13261380
<div class="norm">
13271381
<p>Replace a set of simple TBS fields in the template with new definitions prepared at the PHP side. Note than this function does not merges any field ; it only replaces the definition of fields.</p>
@@ -1388,7 +1442,7 @@ <h3><a id="php_getattvalue"></a>Method GetAttValue():</h3>
13881442
<li><span class="txt-code opt-value">null</span> if the TBS field is not found or if it has no parameter <a href="#html_field_prm_all" class="opt-name">att</a>,</li>
13891443
<li><span class="txt-code opt-value">false</span> if the target XML/HTML element is not found or if the asked attribute is not found.</li>
13901444
</ul>
1391-
<p class="versioning">Versioning: methods GetAttValue() is supported since TBS version 3.11.0. </p>
1445+
<p class="versioning">Versioning: methods GetAttValue() is supported since TBS version 3.11.0. </p>
13921446
</div>
13931447

13941448
<h3><a id="php_plugin"></a>Method PlugIn():</h3>
@@ -1416,29 +1470,56 @@ <h4>Installing a plug-in: </h4>
14161470
</ul>
14171471
<p class="versioning">Versioning: the method PlugIn() is supported since TBS version 3.0. </p>
14181472
</div>
1473+
14191474
<h3><a name="php_varref"></a>Property VarRef:</h3>
14201475
<div class="norm">
1421-
<p>This property enables you to set the scope of variables availables for <a href="#html_field_auto">automatic fields</a> ([onload], [onshow] and [var]).</p>
1422-
<p>By default, VarRef is a <a href="http://www.php.net/manual/en/language.references.whatare.php">reference</a> to the <span class="opt-name">$GLOBALS</span> PHP variable, which means all global PHP variables are available for automatic field.</p>
1476+
<p>Property VarRef is the array of the items that will be merged with <a href="#html_field_auto">automatic fields</a> ([onload], [onshow] and [var]).</p>
14231477
<p> Syntax: <span class="txt-code"><span class="opt-type">array</span> <span class="opt-name">$TBS</span>-&gt;VarRef</span></p>
1478+
<p>
1479+
By default <span class="txt-code">$TBS->VarRef</span> refers
1480+
to the special <a class="opt-name" href="https://www.php.net/manual/en/reserved.variables.globals.php">$GLOBALS</a> variable of PHP.
1481+
This is for compatibility reasons, but it is recommended to use another array instead (see below).
1482+
</p>
1483+
<p>You can set <span class="txt-code">$TBS->VarRef</span> to be a new empty array using the method <span class="txt-code">$TBS->ResetVarRef(false);</span></p>
1484+
<p>You can also set <span class="txt-code">$TBS->VarRef</span> to be a reference to your own array : <span class="txt-code">$TBS->VarRef =& $my_array;</span></p>
1485+
1486+
<div class="decal warning">
1487+
<strong>Warning</strong>
1488+
<br>As of TBS version 3.13.0, is not possible to access directly to the VarRef items when the property refers to $GLOBALS. This is because of the PHP restriction on $GLOBALS that occurs with PHP 8.1.
1489+
<br>In order to ensure the compatibility to any situation, you can manage the items of VarRef using the methods <a href="#php_setvarrefitem" class="txt-code">$TBS->SetVarRefItem()</a> and <a href="#php_setvarrefitem" class="txt-code">$TBS->GetVarRefItem()</a>.
1490+
</div>
1491+
14241492
<p>Notes:</p>
14251493
<ul>
1426-
<li> Method<span class="txt-code"> ResetVarRef(<span class="opt-name">$ToGlobal</span>)</span> is a shorthand for reseting property VarRef to <span class="opt-name">$GLOBALS</span> or to an empty array.</li>
1494+
<li> Method <a href="#php_resetvarref" class="txt-code">ResetVarRef(<span class="opt-name">$ToGlobal</span>)</a> is a shorthand for resetting property VarRef to <a class="opt-name" href="https://www.php.net/manual/en/reserved.variables.globals.php">$GLOBALS</a> or to an empty array.</li>
14271495
<li> If property VarRef is changed by a sub-template, the change is available only for the sub-template.</li>
14281496
</ul>
1429-
<p>Example for setting VarRef to global variables :</p>
1430-
<pre class="decal txt-code"><span class="opt-name">$TBS</span>-&gt;VarRef = &amp;<span class="opt-name">$GLOBALS</span>;
1497+
1498+
<p>Example when VarRef is a custom array (recommended): </p>
1499+
<pre class="decal txt-code"><span class="opt-name">$TBS</span>-&gt;ResetVarRef(false); <span class="opt-html">// VarRef is now a new empty array</span>
1500+
<span class="opt-name">$TBS</span>-&gt;VarRef['my_item'] = "my value";
1501+
<span class="opt-name">$TBS</span>-&gt;SetVarRefItem('my_item', "my value"); <span class="opt-html">// same as above but ensure the compatibility with any situation</span></pre>
14311502

1432-
which is the same as:
1503+
<p>Example when VarRef is attached to $GLOBALS (by default): </p>
1504+
<pre class="decal txt-code"><s><span class="opt-name">$TBS</span>-&gt;VarRef['my_item'] = "my value";</s> <span class="opt-html">// this will raise an error as of TBS version 3.13.0</span>
1505+
<span class="opt-name">$GLOBALS</span>['my_item'] = "my value";
1506+
<span class="opt-name">$TBS</span>-&gt;SetVarRefItem('my_item', "my value"); <span class="opt-html">// same as above but ensure the compatibility with any situation</span></pre>
1507+
1508+
<div class="versioning">
1509+
<p>Versioning:</p>
1510+
<ul class="list-mini">
1511+
<li>Property VarRef is supported since TBS version 3.8. Before this version, the scope of automatic fields was PHP global variables.</li>
1512+
<li>
1513+
Since TBS version 3.13.0, you cannot directly use the property <span class="txt-code">$TBS->VarRef</span> when it refers to $GLOBAL. This is because of a PHP 8.1 restriction. Use SetVarRefItem() and GetVarRefItem() instead.
1514+
<br>Before TBS version 3.13.0, property <span class="txt-code">$TBS->VarRef</span> used to be by default a PHP reference the $GLOBAL variable.
1515+
</li>
1516+
</div>
1517+
1518+
</div>
1519+
1520+
1521+
<h3><a id="php_assigned"></a>Property Assigned:</h3>
14331522

1434-
<span class="opt-name">$TBS</span>-&gt;ResetVarRef(true)<span class="opt-name"></span>;
1435-
</pre>
1436-
<p>Example for setting VarRef to a custome scope of variables: </p>
1437-
<pre class="decal txt-code"><span class="opt-name">$TBS</span>-&gt;ResetVarRef(false); <span class="opt-html">// VarRef is now a new empty array</span>
1438-
<span class="opt-name">$TBS</span>-&gt;VarRef['x'] = 'This is value X';</pre>
1439-
<p class="versioning">Versioning: property VarRef is supported since TBS version 3.8. Before this version, the scope of automatic fields was PHP global variables.</p>
1440-
</div>
1441-
<h3><a id="php_assigned"></a>Property Assigned:</h3>
14421523
<div class="norm">
14431524
<p>Enables you to define information for a subsequent merging which can be automatic or manual. </p>
14441525
<p> Syntax: <span class="txt-code"><span class="opt-type">array</span> <span class="opt-name">$TBS</span>-&gt;Assigned</span></p>

0 commit comments

Comments
 (0)