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>';
}
}