0

I'm working on figuring out how to do common things with Mustache.php. At this point, I'm trying to replace a native php switch statement in the template.

Given a "status" flag, e.g. 'A' for Active,

and a set of display options like A => Active, I => Inactive, P=>Pending, D=>Deleted,

how would I make a nice display string in Mustache.php by modifying the data in the template?

Example data for a table:

$users = array(
array('username'=>'william', 'status'=>'A', 'date_created'=>'7-01-2012'),
array('username'=>'john', 'status'=>'P', 'date_created'=>'5-17-2012')
);
Kzqai
  • 22,588
  • 25
  • 105
  • 137

1 Answers1

2

The whole point of Mustache, is having your template clean from all logic. Mustache templates should be logic-less. This is quite a different and confusing methodology for newcomers.

To solve your problem, you will need to re-process the $users array to contain everything your Mustache template will need, before-hand. e.g. if the status field switch statement was intended for displaying a human-readable status, then your View class should look something like:

class View_User {

    public $_users;

    public function users()
    {
        $users = array();
        foreach ($this->_users as $user)
        {
            $user->status_label = // Use your 'switch' statement here
            $users[] = $user;
        }
        return $users;
    }
}

Then all your Mustache template needs to do, is output {{status_label}} like so:

<ul>
    {{#users}}
        <li>Status: {{status_label}}</li>
    {{/users}}
</ul>

Contain your logic in View classes, and leave your Mustache templates logic-less. UI code that is separated this way makes it so much easier to maintain and re-factor later on.

rafi
  • 1,648
  • 2
  • 18
  • 23