8

When User already login, i want to show some link in page

<{if(!empty($_COOKIE['USER']))}>
   <a href="domain.com/page-a.html">TEXT A</a>
<{else}> <a href="domain.com/page-b.html">TEXT B</a>
<{/if}>    

but does not work


Found the answer myself, just replace :

<{if(!empty($_COOKIE['USER']))}>

with

<{if $smarty.cookies.USER!=''}>

Yihaaa !!!

0

3 Answers 3

1

For me it's rather not good practice to do it that way.

You should do it in PHP:

if (!empty($_COOKIE['USER'])) {
   $smarty->assign('page','a');
}
else {
   $smarty->assign('page','b');
}

And then in Smarty:

{if $page eq 'a'}
<a href="domain.com/page-a.html">TEXT A</a>
{else}
<a href="domain.com/page-b.html">TEXT B</a>
{/if}

What's the benefit of such attitude? In template you don't care how $page is calculated. It may be calculated by session or by any other reason and template just don't care because it's only care about $page value. If you decide to change implementation you will need only change it in PHP and not in both PHP and Smarty.

You could also consider using $_SESSION instead of $_COOKIE - what if user changes/set his cookie value? He will get access to page b even if he shouldn't have such access.

2
  • 1
    Nothing wrong with using cookies in the template... especially considering, as OP mentioned, you can use $smarty.cookies to access them.
    – Andrew
    Commented Apr 6, 2016 at 17:18
  • Answer is not a solution to the problem, it is a recommendation to not to have problem in general. Sometimes you may need to read cookies in template files, for instance in development to quickly check cookie and dump if debug mode Commented Dec 3, 2019 at 17:00
1

This is what you need.

{$smarty.cookies.$thecookiename} {if $smarty.cookies.foo == bar} Hello world {else} Haha {/if}

0

Get the Cookies in Smarty using the below syntax

{$smarty.cookies.$cookiename}. $cookiename is the name of the cookie variable.

1
  • this answer has already been mentioned, make sure not to repost people's answers. Commented Feb 6 at 5:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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