Welcome Guest, Not a member yet? Register   Sign In
Best way to parse HTML
#1

[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>

<body>

<div>
<h1>Login</h1>
&lt;form&gt;
&lt;input type="text" name="username" /&gt;
&lt;input type="password" name="password" /&gt;
&lt;/form&gt;
</div>

&lt;/body&gt;

&lt;/html&gt;

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:
&lt;html&gt;

&lt;body&gt;

<div>
&lt;?php echo $login_html; ?&gt;
</div>

&lt;/body&gt;

&lt;/html&gt;

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>
&lt;form&gt;
&lt;input type="text" name="username" /&gt;
&lt;input type="password" name="password" /&gt;
&lt;/form&gt;

Or, when the user is logged in:

Code:
<h1>Welcome, dude!</h1>
<a href="http://somesite/logoff">Log off</a>

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
#2

[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.
#3

[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:
&lt;html&gt;

&lt;body&gt;

&lt;?php if ($this->session->userdata('logged_in') != '1'): ?&gt;
    <div>
    <h1>Login</h1>
    &lt;form&gt;
    &lt;input type="text" name="username" /&gt;
    &lt;input type="password" name="password" /&gt;
    &lt;/form&gt;
    </div>
&lt;?php else: ?&gt;
    <div>
    <p>You are logged in.</p>
    </div>
&lt;?php endif; ?&gt;

&lt;/body&gt;

&lt;/html&gt;

Only the appropriate HTML code will be generated at run time because the view passes through the PHP parser.
#4

[eluser]Jay Logan[/eluser]
In the CONTROLLER, I would make a variable like this...

Code:
if ($this->session->userdata('user_id')) {

$data['user_name'] = $this->log_in_model->get_user_name();

} else {

$data['user_name'] = "Guest";

}

So in your template, you can now have this:

Code:
<h1>Welcome, &lt;?= $user_name ?&gt;!</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.
#5

[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:
&lt;?php include('layout/header.php'); ?&gt;
<h1>Heading</h1>
<p>Page elements</p>
&lt;?php include('layout/footer.php'); ?&gt;

Might be what you want to do? Smile
#6

[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 Tongue ) 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: !




Theme © iAndrew 2016 - Forum software by © MyBB