CodeIgniter Forums
Using a helper function in the View file - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Using a helper function in the View file (/showthread.php?tid=51256)

Pages: 1 2


Using a helper function in the View file - El Forum - 04-26-2012

[eluser]ridley1012[/eluser]
Hi, from my understanding it is bad practice to use a helper function within the view file. The problem I have is i'm not sure how to separate it and use it in the controller instead. I currently have a date stored as a timestamp in the db, I have the following controller retrieving the data from the db:

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Vehicles extends MY_Controller {

private $page = "vehicles";

  public function index()
{  
  $this->load->helper('multi_function');
  $this->load->model('vehicle_model', 'vehicles', TRUE);
  $data['page'] = $this->page;
  $data['title'] = "Administration Panel - Vehicle List";
  $data['vehicles'] = $this->vehicles->get_all_vehicles();
  $this->load->view('templates/admin/header', $data);
  $this->load->view('templates/admin/sidebar');
  $this->load->view('admin/vehicles');
  $this->load->view('templates/admin/footer');
}
}
?>

using this model file:
Code:
<?php
class Vehicle_model extends CI_Model {

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    
function get_all_vehicles()
{
  $this->db->order_by('registration ASC');
  $query = $this->db->get_where('vehicles', array('sold' => 'No'));
  return $query->result();
}
}
?>

as you can see it returns the data as an array and I use a foreach loop inside the view to display the data in a table. I would like to convert the timestamp to a date which I am currently doing using a function I have created in a custom helper but this function is been used within the view file. any suggestions on how to use the function inside the controller instead?

would I have to pull the array apart inside the controller, do the amendments and then rebuild the array?

thanks


Using a helper function in the View file - El Forum - 04-26-2012

[eluser]ojcarga[/eluser]
If you make a loop (or call a method) before loading the views and there you overwrite the value for the timestamp? It is an option, I do not know if CI has a function/method to achieve it in a very easily way, I guess it hasn't.
Smile



Using a helper function in the View file - El Forum - 04-26-2012

[eluser]vrencianz[/eluser]
Is better than way.
With a foreach copy the result from your model to another table transforming its cells content if necessary (this way you can put links, images, in your cells).

If you want you can use http://ellislab.com/codeigniter/user-guide/libraries/table.html


Using a helper function in the View file - El Forum - 04-26-2012

[eluser]Samus[/eluser]
Who lied to you and told you it's bad practice to use helpers in views lol


Using a helper function in the View file - El Forum - 04-26-2012

[eluser]Ayeyermaw[/eluser]
No idea where you got the idea that it's bad practice to use helpers in views.
I always use helpers that i've created where I would find myself repeating the same code in views.
In fact Codeigniter even provides you with helpers that are used in views. URL and FORM helpers are prime examples there.

Hope that helps clear your mind of the madness Big Grin





Using a helper function in the View file - El Forum - 04-26-2012

[eluser]vrencianz[/eluser]
It is not a lie Smile.
The view must be kept as clean as possible because as intended designers will work on it. They don't know codeigniter or advanced php features at all.

When html tables are displayed, and for the 'html table class' case, is better to have the php code on the controller side. Adding/removing columns to/from the table is much more easier and flexible.

If you are the only one developer of that site (almost all of we are Smile) then it does not matter, all the code can be moved to the controller (even models) side. I am joking, of course.


Using a helper function in the View file - El Forum - 04-26-2012

[eluser]Samus[/eluser]
[quote author="vrencianz" date="1335460700"]It is not a lie Smile.
The view must be kept as clean as possible because as intended designers will work on it. They don't know codeigniter or advanced php features at all.

When html tables are displayed, and for the 'html table class' case, is better to have the php code on the controller side. Adding/removing columns to/from the table is much more easier and flexible.

If you are the only one developer of that site (almost all of we are Smile) then it does not matter, all the code can be moved to the controller (even models) side. I am joking, of course.[/quote]
I don't think a simple helper function can be classed as 'advanced' php.


Using a helper function in the View file - El Forum - 04-26-2012

[eluser]vrencianz[/eluser]
A single function is not, but there is a <b>foreach</b> with a <b>switch</b> or <b>if</b> inside it selecting the right column to format. The column selection can be tricky because the designer must know the column names too. If he don't want to ask the developer then he will do tries with <b>print_r()</b>, and so on...

With the html table class, for example, the code is cleaner.
The following code must be on the controller side, for sure:

Code:
$table[] = array('url' => $this->lang->line('url'),
  'title' => $this->lang->line('title'),
  'description' => $this->lang->line('description'),
  'status' => $this->lang->line('status'),
  'created' => $this->lang->line('created'),
  'updated' => $this->lang->line('updated'),
  'actions' => $this->lang->line('actions'),
);

foreach($items as $item)
{
$table[] = array('url' => anchor(prep_url($item['url'])),
   'title' => $item['title'],
   'description' => $item['description'],
   'status' => $statuses[$item['status']],
   'created' => $item['created'],
   'updated' => $item['updated'],
   'actions' => anchor('links/update/'.$item['id'], $this->lang->line('message_update'))
);
}
  
$data['table'] = $this->table->generate($table);



Using a helper function in the View file - El Forum - 04-26-2012

[eluser]bretticus[/eluser]
I think the debate is valid. For me, what draws the line on whether I use a helper function (or out-right PHP functions) in a view is whether the function is used for layout purposes. I don't have a problem with converting data with a function in a view for formatting tweaks.

For example:

Code:
&lt;html&gt;
&lt;!-- ... --&gt;
&lt;?php foreach($events as $event): ?&gt;

&lt;?= $event->name ?&gt; &lt;?= date('m/d/Y g:i:s', strtotime($event->datetime)) ?&gt;

&lt;?php endforeach; ?&gt;


If the designer knows a little PHP code (the designer is often me in this scenario anyways) he or she can tweak the layout a little even.


Using a helper function in the View file - El Forum - 04-26-2012

[eluser]Ayeyermaw[/eluser]
It's right that the view should be as simple as possible to allow easy maintenance but you are already using PHP syntax to split the 'vehicles' array so using a helper to format the date is perfectly fine and does not contribute to cluttering up the views.
If you look at bretticus' example:
Code:
&lt;?= date('m/d/Y g:i:s', strtotime($event->datetime)) ?&gt;

If you had this function in mulitple views and your designers decided that they wanted the date in a different format site-wide then they'd have to go into each view and find the formatted dates to change them (assuming they know some php).
If you had a helper which used a config value to standardise the dates then this could be changed at one location (config file/database) and your whole site uses the new standard and the PHP ignorance of your designer wouldn't be an issue.

Helper functions are not to be sniffed at in my opinion.