2

I have put this code in header.tpl but the div is appearing on all pages

{if cart.php}
<div> This is Cart.PHP </div>
{/if}

I want this div to be appear in the header of cart.php page only.

doubleDown
  • 8,048
  • 1
  • 32
  • 48
Fahad Ur Rehman
  • 348
  • 1
  • 5
  • 18
  • `if condition` means there's a test. `if cart.php` implies that `if true, do` and in most languages, without a comparator, `if cart.php` translates to `if "cart.php"`, which will always (or at least usually) be true (yeah, "cart.php" *is*). What is being compared with that? – Jared Farrish Sep 09 '12 at 01:33

2 Answers2

2
{if __FILE__ eq "cart.php"}
    do your thing
{/if}

Your comparison as @JaredFarrish says, will always return true since you are asking if the filename (in this case cart.php is true) and not if it's the current filename as you think you are.

This, however, may not work for includes (like your case) and you may want to use something like this:

{if $smarty.server.PHP_SELF eq "/cart.php"}
    do your thing
{/if}

Note that if your file is in a subdirectory you'd need to compare it to the full path or extract the filename from the string since $smarty.server.PHP_SELF returns the path too.

To do this you can use basename function directly to your variable, so doing basename($smarty.server.PHP_SELF) eq "cart.php" should be enough.

Juan Cortés
  • 20,634
  • 8
  • 68
  • 91
0

Use the server variable PHP_SELF (change the "/cart.php" to the exact path from the root of your site to wherever the cart file is located.

if ($_SERVER['PHP_SELF'] == "/cart.php") {
    echo "<div>This is cart.php</div>";
}
DenverMatt
  • 235
  • 3
  • 7