views:

327

answers:

4

I find this very strange, must be something I'm doing wrong, but still... I'm working on a page using PHP and TPL files. In my TPL file, there's a place in the footer for some extra lines if needed.

For instance, formchecking with Javascript.

so in PHP I did this:

$foot = "<script type=\"text/javascript\">if(document.getElementById){loadEvents();}</script>";

the $foot variable is then parsed and the result in HTML is this:

<script type="text/javascript">if(document.getElementById)</script>

So {loadEvents();} went missing.

Does anybody see what I'm missing here... I'm seriously not finding it. Did I forget to escape a character or something?

+1  A: 

I believe with {} that PHP is expecting a variable within them. I haven't tested this, but try using single quote instead of double-quotes.

michael
-1 because php doesn't parse {var} as a variable. It uses $var, ${var}, or {$var}
epochwolf
I didn't say {var} ... ? All I said is that it expects a variable; in which case yes it would be along the lines of {$var}
michael
+8  A: 

Obviously the template engine you are using eats away the part in curly braces.

Try something like:

$foot = "{literal}<script type=\"text/javascript\">if(document.getElementById){loadEvents();}</script>{/literal}";
Tomalak
not working, I also noticed that the last quotation mark in your solution is a double, fixed that, still not working
Vordreller
Yes. Seen that too late, fixed.
Tomalak
-1 because this is simply not true echo "{1+1}"; will not output "2". The {} within double quotes is only used for disambiguating variables, e.g. "he drank some {$beer}s." The issue here is most likely the templating engine, in which case a fix would depend on which engine was in use.
Greg
This isn't correct - the $foot = "<script... etc. line evaluates correctly
Tom Haigh
That there was a template engine involved was not clear to me. Should have thought of that though.
Tomalak
template engine or not, "1 + 1 = {1+1}" doesn't evaluate as "1 + 1 = 2"
Tom Haigh
True, but since I had to change my answer completely anyway, this is no longer relevant.
Tomalak
-1 removed as you've edited
Greg
removed -1, but I'm not sure that smarty would parse a variable that was created in PHP anyway. surely it would only parse .tpl files.
Tom Haigh
+2  A: 

It looks like you're using a template engine like Smarty, which tries to parse anything it finds within braces.

This page from the smarty documentation explains how to make smarty ignore sections it would otherwise parse.

foxy
+1  A: 

If you are using smarty you could use the {literal}.

literal

rupert0