![]() |
Functions inside views, access to "this" or other vars - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: Functions inside views, access to "this" or other vars (/showthread.php?tid=23407) |
Functions inside views, access to "this" or other vars - El Forum - 10-09-2009 [eluser]pmhart[/eluser] I am writing a view that prints out the exact same thing in two different areas, so I wrote a function that prints it out in the view. However, the function doesn't seem to have access to the "is_logged_in" variable passed to the view: $data["is_logged_in"] = true; $this->load->view("view_name", $data); I tried using "global", I tried passing $this to the function. There are a lot more than just the $is_logged_in variable, otherwise I would pass it as a param and not worry about accessing it directly from the code. Can anyone help me figure out how I can access these $data vars? Functions inside views, access to "this" or other vars - El Forum - 10-09-2009 [eluser]skunkbad[/eluser] Maybe something like this would work for you? Code: <?php Functions inside views, access to "this" or other vars - El Forum - 10-09-2009 [eluser]Colin Williams[/eluser] Show snippets of your view code and we'll have a better idea of the problem. Functions inside views, access to "this" or other vars - El Forum - 10-09-2009 [eluser]nubianxp[/eluser] How about defining Code: $this->load->vars($var) Code: function __constructor() { I'm a noob myself, but I think this would make the variable available on all the functions inside the controller. HTH Functions inside views, access to "this" or other vars - El Forum - 10-10-2009 [eluser]bigtony[/eluser] I would approach it a bit differently. Presumably you derive if someone is logged in by using a session variable, so instead of passing the flag into the view, you can do either of the following: Method 1 - using just the view: Code: <?php if ($this->session->userdata('is_logged_in') == TRUE: ?> Method 2 - using a function and a view: Code: // This is in a helper Code: <!-- This is in your view --> Choose whichever seems more useful (method 2 is better if you need to reuse in many places). It's best to avoid global variables or trying to define functions in views if at all possible. Functions inside views, access to "this" or other vars - El Forum - 10-12-2009 [eluser]pmhart[/eluser] thanks bigtony, that's what I was looking for! |