Welcome Guest, Not a member yet? Register   Sign In
What is allowed in views exactly?
#1

[eluser]felyx[/eluser]
Well I have parts of code in my views which I need to load based on session variables and on validation what my auth lib does.

Here is the example code:
Code:
<div class="headerright">
&lt;?php if ($this->auth->logged_in()): ?&gt;
<strong class="greeting">Hello,&nbsp;&lt;?php echo $this->session->userdata('username'); ?&gt;</strong>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="?c=adm_logout" title="Logout" class="logoutlink">Logout</a>
&lt;?php endif; ?&gt;
</div>

Is this allowed at all? I mean it works but I am not sure if I should do this.
#2

[eluser]Colin Williams[/eluser]
Might help to move the access check into a helper function. I try to avoid using the $this reference from within views.

Code:
<div class="headerright">
&lt;?php if (is_authenticated()): ?&gt;
<strong class="greeting">Hello,&nbsp;&lt;?php echo $this->session->userdata('username'); ?&gt;</strong>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="?c=adm_logout" title="Logout" class="logoutlink">Logout</a>
&lt;?php endif; ?&gt;
</div>
#3

[eluser]obiron2[/eluser]
in terms of what CAN be done in the view the answer is anything. - not very helpful I know.

In terms of what SHOULD be done it is probably easier to say what SHOULDN'T be in each part of MVC.

the view should not have to make any decisions about what to display; all of that should have been done in the controller. e.g. don't have the view decide whether to show the 'not logged in' message. Do that in the controller and pass a variable in the $data that indicates the page is not logged in (or redirect to a different view if that is more relevant)

The controller should not have any say in how the page is built, it should just decide which view to call and what information is required to generate it. Don't put HTML in your controller unless you absolutely have to. sometimes this can seem counter intuitive. e.g. as part of the FORM data your controller may need to tell the view which CSS class to use. The extension of this logic is that formatting helpers (FORM, TABLE, Calendar, etc) should all be called in the view as they help to render the data.

Obiron
#4

[eluser]helmutbjorg[/eluser]
Your controller...
Code:
$data['logged_in'] = $this->auth->logged_in();
$data['username'] = $this->session->userdata('username');
// Load view...

Then in your view
Code:
<div class="headerright">
&lt;?php if ($logged_in): ?&gt;
<strong class="greeting">Hello,&nbsp;&lt;?php echo $username; ?&gt;'); ?&gt;</strong>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="?c=adm_logout" title="Logout" class="logoutlink">Logout</a>
&lt;?php endif; ?&gt;
</div>
#5

[eluser]felyx[/eluser]
Thank you all for the answers, I guess I will do what helmutbjorg suggested. Thanks once again Smile




Theme © iAndrew 2016 - Forum software by © MyBB