11

Using Smarty Tags I'd like to determine if an URL contains a word, something like:

{if $smarty.get.page contains "product.php"} .....

I know contains doesn't exist, but how could I easily go about writing something similar to achieve the above code?

2 Answers 2

28

All PHP conditionals and functions are recognized, such as ||, or, &&, and, is_array(), etc.

{if strpos($smarty.get.page, "product.php") !== false}

1
  • this worked very well but i had to accept tekk becuase he spent so much time helping me, but thank you so much for your help as well
    – 99823
    Commented Nov 26, 2011 at 20:01
9

You can use strpos to check if a string has another string inside of it.

$pos = strpos($smarty.get.page, "product.php");

if($pos !== false) {
 // found, do something
}

Except you need to wrap it in {php} and {/php}.

$smarty.get.page translates to $_GET['page'], so you could replace it with the GET variable as well.

12
  • I will try this but I believe it may not work unless strpos is a smarty code method - thank you for the reply
    – 99823
    Commented Nov 26, 2011 at 19:35
  • It will work, as strpos is a PHP method. Good luck, and feel free to accept if it works :)
    – tekknolagi
    Commented Nov 26, 2011 at 19:36
  • @Loren forgot to tag you, sorry
    – tekknolagi
    Commented Nov 26, 2011 at 19:38
  • I have tried this using smarty tags w/ no success - i appreciate your effort however, I believe that strpos is not a supported smarty method -thanks so much for your time
    – 99823
    Commented Nov 26, 2011 at 19:40
  • @Loren What do you currently have as your code? Smarty is a PHP extension, and is $smarty.get.page a string?
    – tekknolagi
    Commented Nov 26, 2011 at 19:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.