Question

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.

Was it helpful?

Solution

{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.

OTHER TIPS

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>";
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top