Categories > TinyButStrong general >

Call of a custom function?

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: EllisGL
Date: 2008-06-08
Time: 01:09

Call of a custom function?

I've read several posts - but I'm not getting it.
I have a function:
function translate($word)
{
  global $lang;
 
  $sql = 'SELECT `text` FROM `'.DB_PREFIX.'trans` WHERE `key` = \''.mysql_real_escape_string($word).'\' AND `lang` = \''.mysql_real_escape_string($lang).'\' LIMIT 1';
  $qry = mysql_query($sql) or die('Error with Translation SQL "'.$sql.'": '.mysql_error());
 
  if(mysql_num_rows == 1)
   {
    // Found a match!
    $text = mysql_fetch_row($qry);
    return($text[0]);
   }
  else
   {
    // No match found - check default language
    $sql = 'SELECT `text` FROM `'.DB_PREFIX.'trans` WHERE `key` = \''.mysql_real_escape_string($word).'\' AND `lang` = \''.mysql_real_escape_string(DEFAULT_LANG).'\' LIMIT 1';
    $qry = mysql_query($sql) or die('Error with Translation SQL "'.$sql.'": '.mysql_error());
 
    if(mysql_num_rows == 1)
     {
      // Found a match!
      $text = mysql_fetch_row($qry);
      return($text[0]);
     }
    else
     {
      // NO MATCH
      return('No translation for "'.$word.'"');
     }
   }
}
And I want use it with attributes on my template
IE [translate.word]

how can I do this?
By: Skrol29
Date: 2008-06-08
Time: 17:32

Re: Call of a custom function?

Hi Ellis,

You can use parameter "onformat" on each TBS tag you want to translate.
Example:  [var.word;onformat=f_translate]
For this solution, you have to code a simple f_translate() function that use your translate() function and fits with the onformat syntax.

Another solution is to merge all [translate] tags with the command
  $TBS->MergeField('translate','f_translate',true);
This will merge all tags like [translate.*] but you also have to code a small custom function that fits with the TBS syntax and use your own function.
By: EllisGL
Date: 2008-06-09
Time: 15:28

Re: Call of a custom function?

[so translate.whateverwordthisis] would send the second part as an argument?
By: EllisGL
Date: 2008-06-10
Time: 05:59

Re: Call of a custom function?

Ok I got it.

After my loadtemplate call i put $TBS->MergeField('translate','translate',true);

then my [translate.*] works. thanks!