act360's avatar
Level 16

Should I make different view files or add switch case in view file?

I have to show list of posts in a view. Functions in controller query posts and send them to view. Functions like drafts(), pending(), published() etc will query posts for related status. All view layout would almost be same other than page title and buttons that appear alongside each post. So my confusion is, should I make individual view file like drafts.blade.php, pending.blade.php, published.blade.php, etc and have logic less static buttons & page title or instead use one single view like posts-list.blade.php and use switch case or something to bring up case based buttons and title.

Thank you,

0 likes
7 replies
silverxjohn's avatar

Why not make a Master Page since the views has the same layout except for the title and button's text

layouts/app.blade.php

<html>
<head>
    <title>@yield('title')</title>
</head>
<body>
    <!-- Some form controls -->

    <button>@yield('buttonText')</button>
</body>
</html>

And in your posts.blade.php

@extends('layouts.app')

@section('title', 'DRAFTS') {{-- You can make Drafts a variable sent from your controller --}}

@section('buttonText', 'View more Drafts')

Read more here: https://laravel.com/docs/5.4/blade#template-inheritance

silverxjohn's avatar

You shouldn't put a lot of complicated things inside your views.

Inside your model, you can do the following:

/**
  * The accessors to append to the model's array form.
  *
  * @var array
  */
protected $appends = ['action'];

/**
  * 
  *
  * @var string
  */
public function getActionAttribute()
{
    switch ($this->attributes['status'])
    {
        case 1:
            return '<a href="/post/edit/' . $this->attributes['id'] . '" class="btn btn-warning"><i class="icon fa-pencil-square-o" aria-hidden="true"></i> Edit</a>';

        case 2:
            return '<a href="#"></a>';

        case 3:
            return '<a href="#"></a>';

        case 4:
            return '<a href="#"></a>';
    }
}

And then in your view:

@foreach($posts as $post)
<tr>
    <td>{{ $post->name }}</td>
    <td>{{ str_limit($post->content, 100) }}</td>
    <td>{{ $post->status }}</td>
    <td>{{ $post->action }}</td>
</tr>
@endforeach

Well, that would work BUT the same principle applies to your Model, you shouldn't put any view related actions.

You have two options:

  • Create a class that accepts the status parameter and returns the string containing HTML codes or a more defined way but a little complex:
  • Use a Transformer.

It is your code. Your choice. There are no law that states what you should and what you should not do.

newbie360's avatar

but as my knowledge of MVC

html code should place into view only ?

act360's avatar
Level 16

Thank you everyone for taking your time to respond. @silverxjohn I like your suggestion but as you said it won't be wise to used view html codes in model, separate class could be okay. Since the cases are just to show buttons conditionally, switch case in view should be fine I hope.

Hence, I have implemented switch case in view itself. As laravel doesn't have switch case in default for blade, I extended blade functionality in additional service provider.

Service provider here & view file here

Please or to participate in this conversation.