Welcome Guest, Not a member yet? Register   Sign In
Everytime my template is called, run this...
#1

[eluser]nZac[/eluser]
I want to check for a certain session variable (it will define the sidebar used) whenever the Template file I am using is called. I do not want to have to program this into the Controller and pass the Sidebar as a variable each time. Would this be a hook? Should I put it directly into the view? Thoughts?

Cheers,
Nick
#2

[eluser]n0xie[/eluser]
Maybe write a wrapper around your template function call which you load into MY_Controller?
#3

[eluser]wiredesignz[/eluser]
Use a Widget. (see my signature)
#4

[eluser]nZac[/eluser]
I am really confused on how a widget would help. I suppose I could hard code it right into the template because most likely the sidebar will just be an int based value to call the correct sidebar. I will see what solution I come up with and post back here.
#5

[eluser]bigtony[/eluser]
Just reference it in the view directly:
Code:
<?php if ($this->session->userdata('sidebar'): ?>
    <div class="sidebar">
       Whatever...
    </div>
&lt;?php endif: ?&gt;
#6

[eluser]nZac[/eluser]
@bigtony,

Interesting idea. I will look more into sessions.

Cheers
#7

[eluser]nZac[/eluser]
My new questions is about using sessions... is this the right place for it? Should a session store a large HTML file?
#8

[eluser]bigtony[/eluser]
[quote author="nZac" date="1257736827"]My new questions is about using sessions... is this the right place for it? Should a session store a large HTML file?[/quote]
Sessions are used to retain state (i.e. data) across multiple pages. Two typical exampes are:

(1) You have a login page, where after the user enters a valid username & password you would a session variable like this (in your controller):
Code:
$this->session->set_userdata('logged_in', TRUE);
Then other pages in your site that should only be seen when the user is logged in can check the session field and allow/disallow depending on its value.


(2) You have a complex entry form which you decide to break up into two or more pages. Then you can store each pages responses as you go:
Code:
$this->session->set_userdata('name', $this->input->post('name'));
$this->session->set_userdata('address', $this->input->post('address'));
// etc.
Which lets you effectively assemble all the data together in the session so you can process at the final step (e.g. to store in a database).


Obviously lots of other uses, but it's definitely NOT intended for storing HTML files! That is what "views" are for. The data you need for a view is usually passed into it from a controller:
Code:
// This is in a controller
$view_data['name'] = 'Fred';
$this->load->view('mypage_view', $view_data);
Code:
// This is in the view 'mypage_view'
<h1>My Page Header</h1>
<p>My name is &lt;?php echo $name; ?&gt;</p>
(The above example is not using sessions at all).
#9

[eluser]nZac[/eluser]
@bigtony,

I agree with eveything that you are saying. What I am saying is tha the sidebars I am creating are dynamic to the point that they are diffrent for every user, however once the user logs in the side bar would not change.

One possibility would be to compile a sidebar file, name it (some random string ) and then store then name in the Session. When the session is deleted the users sidebar would be deleted as well.

Thoughts?
#10

[eluser]bigtony[/eluser]
Obviously I don't know the nature of how your sidebars change, but would you be able to generate the dynamic versions in the view? For example...
Code:
<div class="sidebar">
    &lt;?php if ($someflag == 1): ?&gt;  // or maybe if ($this->session->userdata('someflag') == 1):
        <p>Version A for &lt;?php echo $someothervalue; ?&gt;</p>
    &lt;?php elseif ($someflag == 2): ?&gt;
        <p>Version B for &lt;?php echo $someothervalue; ?&gt;</p>
    &lt;?php else: ?&gt;
        <p>Default version for &lt;?php echo $someothervalue; ?&gt;</p>
    &lt;?php endif; ?&gt;
</div>
In other words to try to keep all HTML tags within the view files and use conditionals on certain data values to control the dynamic parts.




Theme © iAndrew 2016 - Forum software by © MyBB