Dalma
6 months ago

Reusing codeblocks in blade

Posted 6 months ago by Dalma

I have a large blade view file where a section of code is repeated except for a variable name that is table specific.

I have a view with a parent, child, grandchild relationship and within each I have 30 line block of code that repeats except that in each it has a {{$parent->id}} or {{$child->id}} or {{$grandchild->id}} embedded.

I'm trying to pull all 30 lines of code into another blade template and then pass in a variable being the $table name.

Something like

@include('admin.project.scheduleblock', ['table' => '$parent'])

@include('admin.project.scheduleblock', [table => '$child'])

If this does in fact work I can't figure out the appropriate syntax.

Partial code in my include file

<div class="schedule-list">
    @foreach($table->schedules as $item)
        <div class="form-row">
            <div class="col-md-3">
                @if($loop->first)
                    <label class="col-form-label-sm"><small>Start Date</small></label>
                @endif
                <input type="text" class="form-control form-control-sm " name="start_dt" value="{{$item->start_dt->format('m-d-Y H:i')}}" disabled>
            </div>
            <div class="col-md-3">
                @if($loop->first)
                    <label class="col-form-label-sm"><small>End Date</small></label>
                @endif
                <input type="text" class="form-control form-control-sm " name="end_dt" value="{{$item->end_dt->format('m-d-Y H:i')}}" disabled>
            </div>

Please sign in or create an account to participate in this conversation.