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?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
99823
  • 2,407
  • 7
  • 38
  • 60

2 Answers2

28

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

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

meze
  • 14,975
  • 4
  • 47
  • 52
  • 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 Nov 26 '11 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.

tekknolagi
  • 10,663
  • 24
  • 75
  • 119
  • I will try this but I believe it may not work unless strpos is a smarty code method - thank you for the reply – 99823 Nov 26 '11 at 19:35
  • It will work, as `strpos` is a PHP method. Good luck, and feel free to accept if it works :) – tekknolagi Nov 26 '11 at 19:36
  • @Loren forgot to tag you, sorry – tekknolagi Nov 26 '11 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 Nov 26 '11 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 Nov 26 '11 at 19:41
  • I am using TPL files that contain the smarty code currently - my code i tested was {if strpos($smarty.get.page, 'product.php') > 0} ..etc etc etc.. {/if} – 99823 Nov 26 '11 at 19:43
  • @Loren see my edit; just wrap exactly what I have in `{php}` and `{/php}` and it should work :) – tekknolagi Nov 26 '11 at 19:44
  • ok great - this is looking like i'm getting closer - is there something special i would need to do if i'm outputting a string where the //found, do something – 99823 Nov 26 '11 at 19:50
  • for example, if the statement isn't false i need to output some javascript code in between the if($pos !== false) { // ouput javascript here } – 99823 Nov 26 '11 at 19:51
  • Should be able to use `echo` :) --> `echo $my_var;` – tekknolagi Nov 26 '11 at 19:51
  • its pretty insane javascript - autogenerated by google for their website optimizer - i'm basically a/b testing two product pages, is there an alternative to the echo as it has many lines and i would have to modify the quotes and stuff =) not sure if i can somehow just output it like u know in PHP how u can like do something like if ( 1 == 1 ) { ?> my string output } > – 99823 Nov 26 '11 at 19:54
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/5378/discussion-between-loren-and-tekknolagi) – 99823 Nov 26 '11 at 19:55