Jump to:

23459 Posts in 18906 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » How to render page as normal if a condition isn't met

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 444 Views
  • kinglozzer
    Avatar
    Community Member
    139 Posts

    How to render page as normal if a condition isn't met Link to this post

    Hi guys,

    EDIT:

    Sorted the first question!

    First question:

    I was creating an imaginary customer login section on my site, and wanted to show them a login form if they didn't have cookies set for username and password, or show them their personal page if they'd logged in & had the credentials stored correctly. Just as a learning task.

    The problem I had was, while I knew how to render a page different from the one I was currently in the controller for (in this case, if they were logged in), but I didn't know how to render the original page manually (my 'if' statement needed an 'else'). Here was the solution:

    if (($email = $cookie->get('ClientEmail')) && ($pass = $cookie->get('ClientPassword')))
    {
       $where = "Client.ClientEmail = '" . $email . "'";
       $where .= " AND Client.ClientPassword = '" . $pass . "'";
       
       if($client = DataObject::get_one("Client", $where, "", "", ""))
       {
          return $this->customise(array('Client' => $client))->renderWith(array('ClientPage', 'Page'));
       }
       else
       {
          $cookie = new Cookie;
          $cookie->forceExpiry('ClientEmail');
          $cookie->forceExpiry('ClientPassword');
          $this->setMessage('bad', 'Invalid session');
          Controller::redirectBack();
       }
    }
    else
    {
    // This is the code I was originally asking for, ClientHolder is the template & controller I am currently working in
       $page = Director::get_current_page();
       return $this->renderWith(array('ClientHolder', 'Page'));
    }

    Bonus question:

    In this 'if' statement

    if (($email = $cookie->get('ClientEmail')) && ($pass = $cookie->get('ClientPassword')))

    I had to evaluate both conditions separately, whereas normally I would remove the extra brackets like this

    if ($email = $cookie->get('ClientEmail') && $pass = $cookie->get('ClientPassword'))

    However, with the brackets removed, $email always returned '1'. Any idea why this would be? Thanks

  • jak
    Avatar
    Community Member
    46 Posts

    Re: How to render page as normal if a condition isn't met Link to this post

    Hi!
    It would have been nice if you had left the original question in place and just added the answer - that way other people can find it.

    Regarding your bonus question: I think it has to do with operator precedence. In addition to the well known "multiplication before addition", there are rules as to which operators are evaluated first. The assignment operator (=) is evaluated after &&. It is one of the last ones, see http://at2.php.net/manual/en/language.operators.precedence.php

    Without brackets, php probably interprets your code as:
    if ($email = ($cookie->get('ClientEmail') && ($pass = $cookie->get('ClientPassword'))))

    If you really want to loose the brackets, you can use "and" instead of "&&", but I would strongly recommend to keep them. If someone else (or you yourself) later comes across the code, he will have no clue why the "and" is there.

  • kinglozzer
    Avatar
    Community Member
    139 Posts

    Re: How to render page as normal if a condition isn't met Link to this post

    Hi Jak,

    Thanks for clearing that up, I always had a feeling there was a difference between '&&' and 'and', but was never sure what! I usually use 'AND' (capitalised for readability) but it seems this would've been below '&&' in the order of precedence anyway.

    I've also edited my original post and readded the first question

    Thanks

  • jak
    Avatar
    Community Member
    46 Posts

    Re: How to render page as normal if a condition isn't met Link to this post

    Your welcome, and thanks for putting the question and answer back!

    I actually think it's a really bad idea that PHP has these subtle differences. It's just not something you would expect if you don't look it up.

    444 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.