Forum Moderators: coopster

Message Too Old, No Replies

POST vars in the one line template engine

Not working for me?

         

trillianjedi

10:12 am on Mar 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm using Vince's nice and simple one-line template engine from here:-

[webmasterworld.com...]

But I want to use it with POST vars from a form submission. It doesn't seem to be working. The original from Vince is:-

print preg_replace("/\{([^\{]{1,100}?)\}/e","$values[$1]",file_get_contents("template.tpl"));

... and I'd like to use this:-

print preg_replace("/\{([^\{]{1,100}?)\}/e","$_POST[$1]",file_get_contents("template.tpl"));

... but it generates this error:-

syntax error, unexpected T_CHARACTER, expecting T_STRING or T_VARIABLE or T_NUM_STRING

I'm not quite sure how to reference to global $_POST var or if what I want to do is even possible.

Any ideas gratefully received!

eelixduppy

3:57 pm on Mar 9, 2009 (gmt 0)



"$_POST[$1]" --> $_POST[$1]

Try it without the quotes. Other than that everything looks fine so make sure the syntax error isn't coming from someplace else.

trillianjedi

6:24 pm on Mar 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks eelix.

With that change I now get:-

syntax error, unexpected T_LNUMBER, expecting T_VARIABLE or '$'

eelixduppy

9:06 pm on Mar 9, 2009 (gmt 0)



Sorry, I didn't actually test it before. This time I tested it and the following should work:

print preg_replace("/\{([^\{]{1,100}?)\}/e", "\$_POST['$1']", file_get_contents("template.tpl"));

It seems it was looking for a grouped section in the pattern to replace $_... but that's not correct syntax. I escaped the dollar sign and it seems to work correctly. I'm not sure, however, why the original code worked and not your modified version.

trillianjedi

10:20 pm on Mar 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah, brilliant eelix - that's working perfectly now.

Many thanks for your help.

I'm not sure, however, why the original code worked and not your modified version.

Me either. I'm not 100% sure that it did - could have been some caching that made me think it was working, due to the design of this app.

I'll investigate further...