Best way to parse HTML |
[eluser]thePiet[/eluser]
Case: I have a simple website with some registration and login stuff. No big deal. I'm just focussing on the art of MVC here :-) For this website, I'm using one view, because the layout is always the same. No I'm facing one difficulty: The view, simplyfied: Code: <html> So far, so good. But, now the user logs in, and ofcourse I do not want to show the login-div again, but some kind of a "Hello $user" thingy. And that's the problem: how do I generate this HTML correctly, depending on the log-in state of the user? I can do something like this: View, simplyfied: Code: <html> Where $login_html is passed via the controller (which gets it's data by some models). $login_html than can be something like Code: <h1>Login</h1> Or, when the user is logged in: Code: <h1>Welcome, dude!</h1> But, I think that's ugly, since no HTML code should sit in Models / Controllers ? Or am I completely wrong? Kind of hard to explain, but I hope you guys understand me :lol: ! Thanks
[eluser]xwero[/eluser]
Just create a not_logged and logged view files and based on the parameters you check if someone is logged in or not you show the appropriate view.
[eluser]bigtony[/eluser]
After you've logged in you can set a session variable of some kind which the rest of your site can check to see if user has logged in or not. So for example say you set session variable 'logged_in' to '1'. Then in your view do something along these lines: Code: <html> Only the appropriate HTML code will be generated at run time because the view passes through the PHP parser.
[eluser]Jay Logan[/eluser]
In the CONTROLLER, I would make a variable like this... Code: if ($this->session->userdata('user_id')) { So in your template, you can now have this: Code: <h1>Welcome, <?= $user_name ?>!</h1> Assuming you have created the function within your log in model that will produce a user name. Now for the show / hide of log in form, you can do it like suggested above. A simple PHP if statement in your view file.
[eluser]Christoffer[/eluser]
What I always do is create a layout folder containing "header.php" and "footer.php". This makes it very easy to change the layout for the entire site and for the individual views. Then every view looks something like this: Code: <?php include('layout/header.php'); ?> Might be what you want to do?
[eluser]thePiet[/eluser]
Thank you guys for your replies! I'm using the solution of bigtony, as that's the most suited solution for the application I guess. Also, I've checked out how Derek Allard (he should be knowing what he's doing with CI ) solves this problem by downloading BambooInvoice (http://bambooinvoice.org/) and checking the source code in the views he built. He uses the same method. Many thanks :lol: ! |
Welcome Guest, Not a member yet? Register Sign In |