Categories > TinyButStrong general >

TBS and PHP Classes

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Joshua Parker
Date: 2012-01-17
Time: 06:19

TBS and PHP Classes

I am trying to determine if it would be feasible to use TBS for my next project. I am working on creating a simple forum using OOP PHP, and I will be implementing a plugins system as well. If I have the following object $ob->do_hook('chm_head'), how could I use it in HTML according to TBS standards? Thanks for your help.
By: Skrol29
Date: 2012-01-17
Time: 10:37

Re: TBS and PHP Classes

Hi Joshua,

TBS gives some facilities to work with OOP.
Nevertheless less, those facilities are limited because implementing Object coding in the template side is not the philosophy of a template engine.
Keep in mind that the template can only understand simple syntaxes, and all values are converted into string when merged.

TBS has a special property named ObjectRef. You can attach your PHP object (by reference) to this property, and then the object can be simply invoked at the template side using the symbol "~". See the example below.

PHP:
$tbs->ObjectRef['ob'] &= $ob;

HTML:
[onshow.~ob.do_hook(chm_head)]

The feature is described here:
http://www.tinybutstrong.com/manual.php#php_oop

Using this feature, you can invoke only simple calls for your objects. I.e. arguments at the HTML side are taken as strings, and they cannot be a PHP expression.
If you need more complicated expressions, then it is not the job of the Template Engine to perform the calculation anymore. In this case, you have to prepare the result at the PHP side and then mere it as a simple item.

Example : if you have to merge the result of the expression $ob->do_hook('chm_head', $obj2->id);

PHP:
$GLOBALS['result'] = $ob->do_hook('chm_head', $obj2->id);

HTML:
[onshow.result]

In the example above, a global variable is created in order to have it simply available at HTLM using the automatic field [onshow].
In TBS 3.8 (which is still in beta version), there will be a feature for changing the scope of automatic fields [onshow] and [onload].

But you can also use an manual merging :

PHP:
$result = $ob->do_hook('chm_head', $obj2->id);
$tbs->MergeField('result_field', $result)

HTML:
[result_field]

By: Joshua Parker
Date: 2012-01-17
Time: 13:26

Re: TBS and PHP Classes

@Skrol29, awesome. Thank you.