1

Do calls to render overwrite each other? I mean, if I call render twice in a php file located in pset7/public (was it called a controller?), and both of those calls render() with two different views, where the first requests input and the other displays the result some time after, then both views will eventually get rendered assuming the user complies.

Also, in render(), as far as I can tell, require() is a built-in equivalent of render() which omits error checking as well as printing header and footer, before and after the requested $view. Mentally I see these three calls as copy-pastes of a given view which eventually result in the final HTML code sent to the user (kind of like calling fwrite() trice in C), so these stack upon each other and compose the final page together. So if the last thing I said is true then they do not overwrite each other, well, at least calls to require() do not, but is the same true for render()?

In other words, can I (or do I) call render() twice in a php file? If not, how would I make a controller create two different pages?


I'm quite confused as this is an entirely new world for me, the first two paragraphs reflect that. So I'd like to know where I'm right, and where I am mistaken. Perhaps I should go back and review some of the courseware.

1

When you call render() in a controller, the function is only executed once and then it exits.

No other code in the PHP Controller will execute after the call to render(), so it is not possible for two different render() functions to come into conflict with each other.

Let's take a look inside helpers.php at the source for the render() function:

/**
 * Renders view, passing in values.
 */
function render($view, $values = [])
{
    // if view exists, render it
    if (file_exists("../views/{$view}"))
    {
        // extract variables into local scope
        extract($values);

        // render view (between header and footer)
        require("../views/header.php");
        require("../views/{$view}");
        require("../views/footer.php");
        exit;
    }

    // else err
    else
    {
        trigger_error("Invalid view: {$view}", E_USER_ERROR);
    }
}

So you're executing some Controller logic and it comes upon a call to render(). That sends it out of your Controller and into helpers.php, where this function executes. Then it calls some Views: header.php, the {$view} argument that was passed, and then footer.php. Right after these Views are executed, the next line is simply:

        exit;

And that's the end of the line. After that happens, the flow of execution stops until the user clicks on something else in the browser.

Hope this helps!

EDIT: Can't reply in comments (because mobile.) The answer to your question is that you can have your Controller render a different View depending on a conditional (e.g., whether the Controller is reached via HTTP GET or POST).

There is a good example of this technique in the distribution code provided in the pset7.zip archive -- most likely login.php. Look at how the CS50 Staff accomplish this, and use that for inspiration!

Just keep in mind that no matter what branches of conditional logic you put into your Controllers, the call to render() will be the endpoint.

  • 1
    I now see what I need to do, but not so much how. For "Quote" I'll have to place two views inside the controller, namely the submit form and then the results, I don't see how that can work since quote.php is read from the beginning each time. Any pointers? – Martin Kleiven Oct 1 '16 at 12:48
  • 1
    Thanks a lot. This really helped clear things out! – Martin Kleiven Oct 1 '16 at 18:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .