Categories > Your tips & tricks >

Using constants

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Matt Puchlerz
Date: 2006-01-24
Time: 04:25

Using constants

This is a trick I just figured out today, so I apologize if this is common knowledge.

I was wondering about using constants with TBS.  Searching the forum turned up adding another function and using MergeBlock and so forth to gain access to constants. (http://www.tinybutstrong.com/forum.php?msg_id=2114)

But I realized there was a much much easier way. PHP has a built-in function called get_defined_constants() that returns an assoc array of (surprise!) all defined constants.  It is available in PHP4.1 and later.  If you use this function to create a global array, or property if you're using the object-oriented features of TBS, you can access these just like you would any other variable. For example...



////////////////////////////////////////////////////////////////////////////
// PHP code...

require_once 'tbs_class.php';
$TBS = new clsTinyButStrong;

define('MY_CST', 'Constants constants constants');

$cst = get_defined_constants();

$TBS->LoadTemplate('template.html');
$TBS->Show();



////////////////////////////////////////////////////////////////////////////
// HTML code...

<div>Here is an easy to use constant: [var.cst.MY_CST]</div>



There you have it.  Easy to use constants with no extra constant parsing function, no MergeBlock, no fuss, no mess. Again, if this was an obvious technique, I apologize.  Figured it deserved a place on the forum.

- Matt
By: Skrol29
Date: 2006-01-24
Time: 14:49

Re: Using constants

Smart !
I transfert your post into the Tips & Tricks forum