CodeIgniter Forums
variables in functions in views - 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: variables in functions in views (/showthread.php?tid=21926)

Pages: 1 2


variables in functions in views - El Forum - 08-24-2009

[eluser]cmroanirgo[/eluser]
Is there a recommended way to use variables passed via a controller in a function declared in a view?

Eg:
The controller loads a view, with a variable 'controllervar':
Code:
$this->load->view('some_view', array('controllervar'=>'content'));

Normally, this means that the view can use this variable:
Code:
<h1>&lt;?=$controllervar?&gt;</h1>

However, this variable is not accessible when inside a function:
Code:
function write_controllervar()
{
  return $controllervar;
}
<h1>&lt;?=write_controllervar()?&gt;</h1>
...but the local variable isn't found.

I have tried using:
Code:
function write_controllervar()
{
  global $controllervar;
  return $controllervar;
}
but it just returns an empty string;


Does anyone have an idea how I could access 'controllervar' without needing to pass it in as a parameter?

Thanks for any help.
Craig.


variables in functions in views - El Forum - 08-25-2009

[eluser]bigtony[/eluser]
Well, you could put the function in a helper or library and call it that way, but your calls really ought to be done in the controller rather than the view. So your controller loads the data you need (typically from a model or sometimes from libraries/helpers) and puts that data into an array which you pass to the view.

The only programming 'logic' that should go in a view is just that required to render how it looks on the page - nothing else.


variables in functions in views - El Forum - 08-25-2009

[eluser]cmroanirgo[/eluser]
Well, there's obviously a few thoughts on the whole construct of an MVC system. However, if the function is purely provided to coerce the model data into a viewable format, then i'm afraid to say it actually belongs with the view and nowhere else.
In essence, IMHO, business logic and constructs live in the Model/Controller world. View logic lives with the view.

Is there really no way to use a controller supplied variable in a view function?


variables in functions in views - El Forum - 08-25-2009

[eluser]Jay Logan[/eluser]
Couldn't you add the function to your controller and in your view, call it like $this->write_controllervar() or put it in a helper / plugin file as suggested? Never had the need to include a function within a view. IMHO, it just doesn't belong there. When I think of "VIEW", I think of HTML / CSS with occasional single lines of PHP to echo variables or something minor.


variables in functions in views - El Forum - 08-25-2009

[eluser]cmroanirgo[/eluser]
Again, this is not really solving the issue. If a function is writing out DIV's, BR's and the like, then it doesn't belong anywhere except in a view. If your controller code is writing out naked HTML, then it's not a controller and the MVC concept is ignored.

Code:
&lt;?php
function render_info($info)
{
?&gt;
<table>
  <tr><td>&lt;?=$controllervar.$info->name?&gt;</td></tr>
  <tr><td>&lt;?=$info->description?&gt;</td></tr>
</table>
&lt;?php
}

foreach ($query1->result() as $row)
{
  render_info($row);
}

foreach ($query2->result() as $row)
{
  render_info($row);
}
?&gt;



variables in functions in views - El Forum - 08-25-2009

[eluser]renownedmedia[/eluser]
You're doing it wrong!

Code:
foreach($items as $item) {
?&gt;
<table>
  <tr><td>&lt;?=$item['name']?&gt;</td></tr>
  <tr><td>&lt;?=$item['description']?&gt;</td></tr>
</table>
&lt;?php
}



variables in functions in views - El Forum - 08-25-2009

[eluser]Jay Logan[/eluser]
Are you using a MODEL for your DB queries ($query1 and $query2)? If so, I would get your foreach loops like so:

CONTROLLER

Code:
$data['query1'] = $this->some_model->get_data();
$data['query2'] = $this->some_model->get_more_data();
$this->load->view('some_view', $data);

VIEW

Code:
&lt;?php foreach($query1 as $item1): ?&gt;
<table>
<tr><td>&lt;?= $item1['name'] ?&gt;</td></tr>
<tr><td>&lt;?= $item1['description'] ?&gt;</td></tr>
</table>
&lt;?php endforeach; ?&gt;

&lt;?php foreach($query2 as $item2): ?&gt;
<table>
<tr><td>&lt;?= $item2['name'] ?&gt;</td></tr>
<tr><td>&lt;?= $item2['description'] ?&gt;</td></tr>
</table>
&lt;?php endforeach; ?&gt;

But I really think you should try a custom helper function or library to hold all the processing code to use in a view easily like $this->table_creator($query1). Maybe the CI Table library might even work for you.


variables in functions in views - El Forum - 08-25-2009

[eluser]cmroanirgo[/eluser]
[quote author="Thomas Hunter" date="1251271541"]You're doing it wrong![/quote]
Not true. You simply fail to understand the fact that I am demonstrating an example and not an actual issue.


variables in functions in views - El Forum - 08-25-2009

[eluser]cmroanirgo[/eluser]
[quote author="J-Slim" date="1251272047"]Are you using a MODEL for your DB queries ($query1 and $query2)? If so, I would get your foreach loops like so:
...[snip]...
But I really think you should try a custom helper function or library to hold all the processing code to use in a view easily like $this->table_creator($query1). Maybe the CI Table library might even work for you.[/quote]

Ignoring the above posts, the following is the (approximate) solution I have been using in order to have use of functions within a view:

The controller defines a global array and you place items within that. You also pass that as the 2nd parameter to load->view:
Code:
$_CONTROLLERVARS = array();

class SomeController extends Controller
{
  . . .
  function index()
  {
    $_CONTROLLERVARS['controllervar'] = 'Some Content';
    $this->load->view('some_view.php', $_CONTROLLERVARS);    
  }
}

And in the view page, I access the variable directly where I can, and where I can't, I can use $_CONTROLLERVARS directly:
Code:
&lt;!-- Use Code Igniter variable as normal --&gt;
<h1>&lt;?=$controllervar?&gt;</h1>

&lt;!-- Use a custom defined global variable otherwise --&gt;
&lt;?php
function write_controllervar()
{
  global $_CONTROLLERVARS;
  $controllervar = $_CONTROLLERVARS['controllervar'];
  return $controllervar;
}
?&gt;
<h1>&lt;?=$write_controllervar()?&gt;</h1>

I was hoping for a more elegant solution and not some diatribe on why i'm doing it wrong.


variables in functions in views - El Forum - 08-26-2009

[eluser]renownedmedia[/eluser]
Well, J-Slim pretty much told you the answer to your problem. You then quoted him and said you're ignoring him.

[quote author="cmroanirgo" date="1251282391"]Not true. You simply fail to understand the fact that I am demonstrating an example and not an actual issue.[/quote]

You're really "failing to understand" the point of MVC. Your view should not have any real logic in it, save for a loop or an if statement. By separating your code in the MVC manner, you'll never have to dig around for code that you can't remember where you stuck it and you'll also not have to copy and paste a function from one part of your application to another (e.g. views).

Create a helper to pretty up your data in the way you see fit. In your controller, first execute your model, take the results and pretty it up using a helper, then send it to your view. That is the elegant solution and will save you down the road.