Categories > TinyButStrong general >

getting vars name

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: josx
Date: 2007-10-24
Time: 16:44

getting vars name

Is there any way to get variable names from templates?
Not the tplvars property that just get the ones from onload.
By: Skrol29
Date: 2007-10-24
Time: 18:04

Re: getting vars name

Can you give examples of what you mean?
By: josx
Date: 2007-10-25
Time: 14:15

Re: getting vars name

Yes.

Template:
<code>
[pupil.name] is playing in the playground on [pupil.address]
</code>

And from php some method to retrieve an array or something similar to
<code>
array ("pupil"  => array ("name", "address"));
</code>
By: Skrol29
Date: 2007-10-26
Time: 02:03

Re: getting vars name

A plug-in on the MergeField() event can do this.

But you can also do it without plug-in.
[pupil.name] ... [pupil.address]
PHP:
$TBS->MergeField('pupil','f_read',true);
...
function f_read($SubName,$PrmLst) {
  // Manage your list here
}
By: josx
Date: 2007-10-29
Time: 14:33

Re: getting vars name

I am not getting you well or maybe I didnt write well  my problem.
I need to get in php code the name of variables that someone write down on the template file.
$aVar = $tbs->getVars();
print_R($aVar);

Result:
Array
(
    [pupil] => Array
        (
            [0] => name
            [1] => address
        )

)
By: Skrol29
Date: 2007-10-30
Time: 00:21

Re: getting vars name

The snippet I've provided is for what you're describing.

$TBS->MergeField() is reading all "pupil" TBS tags and merge them using the f_read() function.  So you just have to code the f_read() function in order to have it storing all subname in a public array.
By: sheepy
Date: 2007-10-31
Time: 11:44

Re: getting vars name

This is not compliance, this is not safe, this is not nice, but this should be of use to you:

$matches = $result = array();
if (preg_match_all("~\[(\w+(?:\.\w+)*)\s*[\];]~", $TBS->Source, $matches)) { // Find tags
  foreach ($matches[1] as $tag) { // Process each
    if (sizeof($tag = explode('.', $tag)) > 1) { // Breakdown into components
      $tail = array_pop($tag);
      eval('$result[\''.implode("']['", $tag)."'][] = '$tail';"); // Add to $result
    } else
      $result[] = $tag[0]; // Add to $result
  }
}

var_export($result);
By: josx
Date: 2007-10-31
Time: 15:21

Re: getting vars name

Thanks Sheepy, thats what I need.
The Skrol29 reponse doesnt work to me beacuse I dont know what is on this template.
By: josx
Date: 2007-11-07
Time: 20:28

Re: getting vars name

Anyways I have changed the regular expression because I need to match also chars like ñ,á,é,í,ó,ú.
        if( preg_match_all("/\[([^];]*\.[^];]*)\]/", $OOo->Source, $matches)) {
        }