0

I've watched Laracasts video about presenters and I want to use them in my project.

I have a page where I display user activity log. In foreach loop I display "activity" model. Activity uses polymorphic relationship, so depending on the activity type I need to display different kind of content. For example, user may send email, add task, create something, delete something etc.

Depending on the action I should display different icons, different content itself (some activities may contain links, some may contain something else).

When I watched and read examples of presenters, I realize that all of them are quite simple and uses such things as ucfirst or date-formatting. However, should I put a lot of HTML logic in my presenters? For example, since I have different icon for different actions I put big switch which returns <i> tag with appropriate icon class. Then, when displaying description of the action I switch as well and return some html, like links, even probably <audio> tag.

Also this is not convenient to use HTML in presenters, but at the same time I want to keep my view clean, so that I can just output $activity->presenter()->description instead of a lot of @if operators.

An example of presenter method:

public function icon()
{
    switch ($this->entity->type->name) {
        case 'call': return '<i class="fa fa-phone activity-call-icon"></i>';
        case 'visit': return '<i class="fa fa-car activity-visit-icon"></i>';
        case 'email': return '<i class="fa fa-send activity-email-icon"></i>';
        case 'note': return '<i class="fa fa-pencil activity-task-icon"></i>';
        case 'create_checkin': return '<i class="flaticon-pin56"></i>';
        default: return '<i class="fa fa-dot-circle-o"></i>';
    }
}
Victor
  • 5,073
  • 15
  • 68
  • 120
  • 2
    Personally, I would avoid using HTML in presenters at all. I would only use them to present the data in a better way ready for the view. Like converting a date into a human-readable format etc. I might however return a class name or something like that in a presenter. For example I might return an icon class that I would use in my view. To keep views clean I would generally split them up into partials/sub-views to contain certain bits of logic and include them where appropriate. – Jonathon Apr 13 '15 at 09:29
  • For different types of entities I would lean towards creating a partial for each entity as well. – Jonathon Apr 13 '15 at 09:30
  • Jonathon, indeed... the only question I have: is it okay if I include like ~100 views (if I get 100 items for example)? Doesn't seem to be a problem, but still need to be sure if it's okay. – Victor Apr 13 '15 at 16:13

0 Answers0