Author Topic: How to parse variables dynamicaly  (Read 2296 times)

0 Members and 1 Guest are viewing this topic.

eresende

  • Guest
How to parse variables dynamicaly
on: Mon, 08. March 2004 - 11:47:48
I need a tip on this.

I have a language system integrated into my PHP projects which uses MySQL to store string expressions from various languages.

On the template files I put the string vars with a prefix 'string_'.
In the php source file I have to include the setVar() function to parse each string variable.

What I want is to create a routine to parse all 'string_' variables existing on the template file.
Then, if I need to add a new string, the only thing I have to do is to add a new variable on the template file and a add a new record on the strings table.
Width this tecnique, I don't have to manualy set the variable on the PHP source file.

Is there any function on vlibTemplate class that returns all the template variables?

If not, what are my options here?

Any sugestions are appreciated.

Thank's in advance.

Offline ClausVB

  • Administrator
  • Hero Member
  • *****
  • Posts: 566
    • Homepage: clausvb.de
How to parse variables dynamicaly
Reply #1 on: Tue, 09. March 2004 - 11:11:02
Do you mean something like this?
Code: [Select]
$select_attribute = SELECT l_id, l_user, sr_attribute.* FROM sr_login_auth, sr_attribute WHERE l_id = a_id AND l_user = '$PHP_AUTH_USER';

$result_attribute = mysql_db_query$db_name, $select_attribute, $verbindung;

$row_attribute = mysql_fetch_assoc$result_attribute;

foreach $row_attribute as $attribut = $attributs_wert



  if empty$attributs_wert

  

   $tmpl-setVar$attribut, '--';

  

  else

  

   $tmpl-setVar$attribut, $attributs_wert;

  



You have to edit the template, though:

{tmpl_var name='a_kst'}
{tmpl_var name='a_sck'}

and so on. I do not think it is possible create a template dynamically. A template is a static thing exept for LOOP's.

You can take a look at all your template vars with
/docs/multihtml/...nc_getvars.html

eresende

  • Guest
How to parse variables dynamicaly
Reply #2 on: Tue, 09. March 2004 - 13:20:55
Thank for the reply ClausVB.

You were almost there.

That code could do the trick, but runing that code for each template could leave to a low performance issue if the table has >1000 records, which is not necessary because I only nead 20 records (strings) for each template.

Anyway the getVars() function only returns the template variables after they had been parsed, not before.

If I could get the list of variables on the template before the pparse() instructon, I could execute a query to return only dose variables if you know what I mean.

Thanks.

Offline ClausVB

  • Administrator
  • Hero Member
  • *****
  • Posts: 566
    • Homepage: clausvb.de
How to parse variables dynamicaly
Reply #3 on: Tue, 09. March 2004 - 15:11:24
In my humble opinion that is not possible, because a function like you need it would have to look through the template and find all vars.

Perhaps Kelvin has an idea in this matter.

eresende

  • Guest
How to parse variables dynamicaly
Reply #4 on: Tue, 09. March 2004 - 15:47:16
I think I could get dose vars using Regular Expressions, but its tricky and I'm not good at that.

If I find any solution to this problem, I'll post it here.

Thanks for the support Claus.

releasedj

  • Guest
How to parse variables dynamicaly
Reply #5 on: Wed, 10. March 2004 - 14:06:08
There is no way to do this.

You could take some code from vlibTemplate/debug.php and create a class of your own that extends vlibTemplate and parses the template file to look for TMPL_VAR tags.

Regards,

Kelvin

eresende

  • Guest
How to parse variables dynamicaly
Reply #6 on: Wed, 17. March 2004 - 10:47:39
After going around vlibTemplate's code I finaly implemented a class that extends vlibTemplate like you said Kelvin.

This class returns an array with all the TMPL_VAR template variables with a $PREFIX. I don't know if its perfect, but it does the job for me.

For my situation this is enough although it's possible to extend it's functionality.

With this class, now I can parse all "STRING_" TMPL_VAR with just a foreach type of code.

Here is the code

Class vlibTemplateStrings
Code: [Select]

?php

/*******************************************************************************

** EUSEBIO RESENDE

** Date 16-03-2004

***/

class vlibTemplateStrings extends vlibTemplate



var $_tagstrings = array;

        

    function GetTemplateStrings$filename,$prefix=null        



     $data = file$filename;        

    



        for $i = 0; $i count$data; $i++

            $file = preg_replace

                  $regex,

                  $this-arrangeTagSringsarray

                    'tag' = strtolower'2',

                    'paramname1' = strtolower'3',

                    'paramval1' = strtolower'4',

                    'paramname2' = strtolower'5',

                    'paramval2' = strtolower'6',

                    'paramname3' = strtolower'7',

                    'paramval3' = strtolower'8',

                    'openclose' = '1',

                    'file' = realpath'$filename',

                    'line' = .$i+1.,

                    'entire_tag' = ''

                    ;

                    ,



               $data$i

               ;

        

        

        $string_vars = array;

        foreach $this-_tagstrings as $key

         if isset$prefix and strstr$key'name',$prefix

          array_push$string_vars,substr$key'name',strlen$prefix,strlen$key'name';        

        

        

        

        return $string_vars;        

    

    

    /**

     * rearranges all vars from below regex

     *

     *

     */

    function arrangeTagSrings $arr

        $temparr = array;

        $temparr'tag' = $arr'tag';

        $temparr'openclose' = $arr'openclose';

        $temparr'file' = $arr'file';

        $temparr'line' = $arr'line';

        $temparr'entire_tag' = $arr'entire_tag';



        for$i=1; $i 4; $i++

            if empty$arr'paramname'.$i && empty$arr'paramval'.$i break;

            empty$arr'paramname'.$i ? $key = 'name' $key = strtolower$arr'paramname'.$i;

            $temparr$key = trim$arr'paramval'.$i;

        



        array_push$this-_tagstrings, $temparr;

    



?



Source PHP file example
Code: [Select]

// Assign new template

$tmpl = new vlibTemplate $MAX_GLOBALSDIR_TEMPLATE.top.htm ;



// Strings no template

$tmplstrings = new vlibTemplateStrings;

$string_vars = $tmplstrings-GetTemplateStrings$tmpl-_tmplfilename,string_;

// SetStrings - Gets string names from database and parses it to template object

SetStrings$string_vars,&$tmpl;





Now the template file example
Code: [Select]

table width=100%  border=0 cellspacing=0 cellpadding=0

          tr align=center

            tda href=/index.php class=fonte_12tmpl_var name=string_inicio/a/td

            td class=fonte_12|/td

            tda href=/empresa.php class=fonte_12tmpl_var name=string_empresa_top/a/td

            td class=fonte_12|/td

            tda href=/localizacao.php class=fonte_12tmpl_var name=string_localizacao_top/a/td

            td class=fonte_12|/td

            tda href=/clientes.php class=fonte_12tmpl_var name=string_clientes_top/a/td

          /tr

        /table



This is the essencial code that I use.

I hope this helps someone like it helped me.

Thanks for the support Kelvin.

Regards,

Eusebio Resende