...

View Full Version : Passing "{" as variable in php



forpals
03-14-2008, 02:48 PM
Hi,
I'm a bit puzzled by this, how do I pass a "{" (curly brackets) in php?

I'm using smarty and I wanted to define a variable that has some smarty variables in it but they use "{" and it gets translated into html when passed.

Here's my current code:


if ($modrewrite == 1) {
$url_link = '/play/game-{$randomgamId}/{$randomgamName|replace:" ":"-"}.html';
} else {
$url_link = '/play.php?gamId={$randomgamId}';
}
$smarty->assign('url_link', $url_link);


{$randomgamId} is a variable used by smarty.

Any help is very much appreciated....

Thanks

whizard
03-14-2008, 03:18 PM
Try escaping it with a backslash ( \ )

in other words:

$url_link = '/play.php?gamId=\{$randomgamId\}';

HTH
Dan

_Aerospace_Eng_
03-14-2008, 03:37 PM
PHP variables aren't parsed if they aren't in double quotes.

if ($modrewrite == 1) {
$url_link = "/play/game-{$randomgamId}/{$randomgamName|replace:' ':'-'}.html";
} else {
$url_link = "/play.php?gamId={$randomgamId}";
}
$smarty->assign('url_link', $url_link);

forpals
03-14-2008, 03:38 PM
Try escaping it with a backslash ( \ )

in other words:

$url_link = '/play.php?gamId=\{$randomgamId\}';

HTH
Dan

Hi. Thanks for the quick reply.
I tried as you had suggested (see below):


if ($modrewrite == 1) {
$url_link = '/play/game-\{$randomgamId\}/\{$randomgamName|replace:" ":"-"\}.html';
} else {
$url_link = '/play.php?gamId=\{$randomgamId\}';
}
$smarty->assign('url_link', $url_link);


Unfortunately... It's still getting passed as this on the link:


http://website.url/play/game-/%7B$randomgamId/%7D//%7B$randomgamName%7Creplace:


Any idea?
Thanks

forpals
03-14-2008, 03:57 PM
PHP variables aren't parsed if they aren't in double quotes.

if ($modrewrite == 1) {
$url_link = "/play/game-{$randomgamId}/{$randomgamName|replace:' ':'-'}.html";
} else {
$url_link = "/play.php?gamId={$randomgamId}";
}
$smarty->assign('url_link', $url_link);

Hi. I tried as you had suggested and now I get the following error:


Parse error: syntax error, unexpected '|', expecting '}' in C:\xampp\htdocs\test.com\index.php on line 38


which I suspect is from this line:


$url_link = "/play/game-{$randomgamId}/{$randomgamName|replace:' ':'-'}.html";


Thanks for the help...

GJay
03-14-2008, 07:20 PM
the point of using a templating language is so you don't have to mess about with things like this, rethink your design, it makes no sense. Passing Smarty syntax to smarty won't result in it being parsed by Smarty.

take a look at what Smarty lets you do with custom functions and modifiers, and see if that would make things easier for you.

forpals
03-14-2008, 08:33 PM
the point of using a templating language is so you don't have to mess about with things like this, rethink your design, it makes no sense. Passing Smarty syntax to smarty won't result in it being parsed by Smarty.

take a look at what Smarty lets you do with custom functions and modifiers, and see if that would make things easier for you.

Actually, you're right... I was trying to find a shortcut and not go back to the drawing board, but I think that leaves me with no choice :( Off to recoding I go... Problem was I didn't take mod_rewrite in consideration when I developed the script but now it's a must have... Thanks for setting me straight :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum