Execute php in .tpl or template tag to check if user is signed in

edited February 2011 in Vanilla 2.0 - 2.8
I'm looking to either find a way to include php into my template file or find a way to determine if a user is logged in. I need to display a few links if the user isn't.

I know in Smarty you can usually use {php}{/php} to execute php. It looks like this is disabled somewhere within Vanilla. Is there a way to turn this back on?

If not, is there anyway to determine if a user is logged in from within a template file?

Comments

  • @matthewkris

    You should not need to use PHP within your Smarty templates for this. Instead, add a {debug} call at the top of your template and look at what is available to Smarty. Most likely you will have $User (which may or may not have the user ID available). Also, there are a handful of functions exposed to Smarty via its plugin architecture (which is very simple to add too). For example there are these functions available:

    {signin_link}
    {signout_link}

    There is also "block_permission" which uses Gdn::Session()->CheckPermission() that might work for your needs (I have not researched how CheckPermission() works as of yet).

    Personally I added a simple plugin to gain access to the session:

    function smarty_function_user_session($Params, &$Smarty) { if(Gdn::Session()->IsValid()) $Smarty->assign('User_Session',Gdn::Session()); }

    Cheers
Sign In or Register to comment.