CodeIgniter Forums
How to format template? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: How to format template? (/showthread.php?tid=67922)



How to format template? - PolkaDodge - 04-24-2017

Hi im new to codeigniter and im just wondering on how to format the data from a template.

I can format data coming from a row_array result since i dont need to loop it but how can i format data coming from a result_array()??


If i use foreach in controller to format the data i cant loop them in views.

Controller:
Code:
public function loadClients(){
    $clientsTotal = $this->client_list_model->getClientTotal();
    $activeCount = $this->client_list_model->activeCount();
    $inactiveCount = $this->client_list_model->inactiveCount();
    $archiveCount = $this->client_list_model->archiveCount();

    $data = array(
        'project_total' => $clientsTotal['projects_total'],
        'pending_total' => $clientsTotal['pending_total'],
        'on_going_total' => $clientsTotal['on_going_total'],
        'completed_total' => $clientsTotal['completed_total'],
        'cancelled_total' => $clientsTotal['cancelled_total'],
        'total_price' => number_format($clientsTotal['total'],2),
        'paid_total' => number_format($clientsTotal['paid_total'], 2),
        'balance_total' => number_format($clientsTotal['balance_total'],2),
        'held_price_total' => number_format($clientsTotal['held_price_total'],2),

        'client_total' => $clientsTotal['client_total'],
        'active_count' => $activeCount['active'],
        'inactive_count' => $inactiveCount['inactive'],
        'archive_count' => $archiveCount['archive']
    );

    $data['clients'] = $this->client_list_model->getClients();// I would like to format these results
    $data['title'] = 'Client List';

    $this->load->view('templates/header');
    $this->load->view('pages/activities');
    $this->parser->parse('pages/client_list', $data);
    $this->load->view('templates/footer');
}

Model:
Code:
function getClients(){
        $this->db->select("c.client_id, c.company_name, count(p.project_id) AS projects,
            count(CASE WHEN p.project_status = 'Pending' THEN p.project_status END) AS pending,
            count(CASE WHEN p.project_status = 'On Going' THEN p.project_status END) AS on_going,
            count(CASE WHEN p.project_status = 'Completed' THEN p.project_status END) AS completed,
            count(CASE WHEN p.project_status = 'Cancelled'THEN p.project_status END) AS cancelled,  
            sum(p.total_price) AS total_price, sum(p.paid) AS paid, sum(p.balance) AS balance,
            sum(p.held_price) AS held_price, c.client_status");
    $this->db->from('clients c');
    $this->db->join('projects p', 'c.client_id = p.client_id', 'LEFT');
    $this->db->group_by('c.client_id');
    $clients = $this->db->get();
    return $clients->result_array();
}


View:
Code:
{clients}
    <tr>
            <td class="left">{client_id}</td>
            <td class="left">{company_name}</td>
            <td>{projects}</td>
        <td>{pending}</td>
        <td>{on_going}</td>
        <td>{completed}</td>
        <td>{cancelled}</td>
        <td>{total_price}</td>
        <td>{paid}</td>
        <td>{balance}</td>
        <td>{held_price}</td>
        <td>
                <span>{client_status}</span>
            <a href="client_profile/{client_id}" class="view button">View</a>
        </td>
    </tr>
{/clients}

I would like to be able to number format or date format the result coming from the model in controllers which is
Code:
$data['clients'] = $this->client_list_model->getClients();



RE: How to format template? - marksman - 04-24-2017

I use blade templating engine in CI, its easier.

In your case using parser you can use php tags.


RE: How to format template? - PolkaDodge - 04-24-2017

(04-24-2017, 02:44 PM)marksman Wrote: I use blade templating engine in CI, its easier.

In your case using parser you can use php tags.

What do you mean by i can use php tags? i tried doing this in views
But this only gives me an error

An uncaught Exception was encountered
Type: ParseError
Message: syntax error, unexpected '{'


Code:
<td><?php number_format({total_price}); ?></td>


And also something like this in controller but this returns only 1 row and i cant get all the

data since its not looping

Code:
$clients = $this->client_list_model->getClients();

foreach($clients AS $clientValue){
    $data['total_price'] = number_format($clientValue['total_price'],2);
}



RE: How to format template? - PolkaDodge - 04-24-2017

I also would like to add if statements based on some of the data but i dont know how to do it in a template
Ex:

Controller:

$data['clients'] = $this->client_list_model->getClients();

if($data['client_status'] == 'Active'){ //I want to get this and add if statemens, its a data from $data['clients']
$data['filter'] = '<span class="active_filter">'
}
else if($data['client_status' == 'Inactive'){
$data['filter'] = '<span class="inactive_filter">'
}

View:
{filter}{client_status}</span>


RE: How to format template? - marksman - 04-25-2017

If you use php tags you dont need curly braces anymore. . from {var} to $var


RE: How to format template? - PolkaDodge - 04-25-2017

Cant i use only the logics in the controller only? cause i want the view for displaying only as much as possible with no php tags or i have no choice


RE: How to format template? - marksman - 04-25-2017

I knew you would say that, thats why at first ived recommend blade templating on top of CI. you can do code like

Code:
<ul>
<!-- foreach statement -->
@foreach ($datas as $data)
    <li>{{ $data['key'] }}</li>
@endforeach
</ul>

<!-- if statement -->
@if (true)
    <p>looks nice?</p>
@endif

you can benefit more of it's features like extending, yielding, etc. that would make your front looks good Smile

I'm currently using it on CI 3.1.4


RE: How to format template? - InsiteFX - 04-28-2017

You can create a method in your controller to create the html output loop through it
all assigning it to a variable then assign that variable to your $data['yourName'];


RE: How to format template? - Gustavo Martins - 05-02-2017

There is a library designed for CodeIgniter to simulate Laravel's Blade.

https://github.com/GustMartins/Slice-Library

Cool