Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing through template file with PHP variables in it?

Status
Not open for further replies.

mikerobb

Programmer
Nov 7, 2001
13
0
0
US
Hi-

I have a template file that has some PHP variables inside of it. I would like to pass through the template and evaluate any variables inside the template file. I have "tags" around the PHP variables.

<<TEMPLATE FILE>>
blah html code
<MARKER>$variable1<REKRAM>
blah html code


I can write PHP code to go line by line through the code to find the &quot;marked&quot; code.

$myLine=preg_replace(&quot;/(.*?)<MARKER>(.*?)<REKRAM>(.*?)/&quot;,&quot;$1$2$3&quot;,$myLine);

The above code works fine in removing the &quot;markers&quot; but it does not evaluate the PHP variable.

How do I eval() $2 (the PHP variable)?

Is there an easier way?

-TIA

--Mike Robb
 
Are you just using $1, $2, etc... as pseudocode for this post, or are those actually your variable names? A variable cannot be (or start with) a number.


Unless there is some arcane numbered variable object in preg_replace that I don't know about? -------------------------------------------

&quot;Calculus is just the meaningless manipulation of higher symbols&quot;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-unknown F student
 
the perl like method does allow $1 to be used instead of the normal php \\1 ($3 for \\3 ...etc.)

the $1 is only usuable inside that method call. so you can not reference it on the subsequent php code statement.

i've tried variations on those and none of them will work - mainly due to the variable complexity.

$class_obj->methodName(&quot;string parm&quot;);

it always evaluates the &quot;$class_obj->methodName&quot; to null string and returns (&quot;string parm&quot;)

the interesting part is that if you have a variable like:
$class_obj->variable it works fine (as does just $variables)

what i ended up doing was using preg_match and use the $matches (3rd parm) array object to rebuild the entire string and use the eval with slashes added.

 
I was wondering about that. Guess I need to spend some more time with PCRE...

So you are rebuilding the entire string without markers, and making PHP eval that string for variable substitution? There's got to be a better way. Have you looked at how any of the existing PHP template libraries work?

If I read you right, your template, before parsing, might look like this:

blah html code
<MARKER>$class_obj->methodName(&quot;string parm&quot;)<REKRAM>
blah html code

If you end up eval-ing the whole string, I'm not sure I see the benefit of the template system in the first place. The way most templating systems work, you don't place any PHP style variables in the template itself; you just have an unquoted marker word inside curly braces {MY_VARIABLE}, and then use regex to replace it with the already-substituted PHP value. -------------------------------------------

&quot;Calculus is just the meaningless manipulation of higher symbols&quot;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-unknown F student
 
LOL. Yes I am sure there is an easier way to do this. I often do things the hard way and then go DOH!

What I have now is a template file that looks like this:

blah html<MARKER>XXXXX(VAR1)</REKRAM>blah html
blah html<MARKER>XXXXX(VAR1)</REKRAM>blah html<MARKER>XXXXX(VAR1)</REKRAM>blah html
blah html
blah html<MARKER>XXXXX(VAR1)</REKRAM>blah html

where &quot;XXXXX&quot; is the name of the object of a class.
(there are several different classes/objects)

so i might have a class that is &quot;WORDS&quot; with a method called LookUp that takes the word to look up.
This class also has a language variable (set by another method) that is used to determine what language the user is wanting - and returning the correct word in that language for the word desired.

so my php reads in the marker to get the varible.
if the variable has a &quot;(&quot; and &quot;)&quot; in it, then it is going to build a string of the class-object->method(&quot;var1&quot;) and then eval it. (if no parens it will just treat it like a normal variable and evalulate it - no problem)

if the class is WORDS_CLASS and the object is $WORDS
then I read in:
WORDS(HELLO)
and rebuild the variable as
$WORDS->LookUp(&quot;HELLO&quot;) and it will evaluate down to
either &quot;Hi&quot; for english, ..etc. for other languages.

clearly this is not the easiest approach.

i am interested in hearing more about the &quot;regex&quot; replace of the {VAR} you mentioned. given the complexity of this I wonder if it really needs to be so much so. :)

and thanks for the inputs!
 
If you are interested in researhcing these things further, here is an interesting template library to examine: I have not used it extensively yet, but I have found it quite enjoyable to work with. -------------------------------------------

&quot;Calculus is just the meaningless manipulation of higher symbols&quot;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-unknown F student
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top