1 year ago

Reusing codeblocks in blade

Posted 1 year 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">
                    <label class="col-form-label-sm"><small>Start Date</small></label>
                <input type="text" class="form-control form-control-sm " name="start_dt" value="{{$item->start_dt->format('m-d-Y H:i')}}" disabled>
            <div class="col-md-3">
                    <label class="col-form-label-sm"><small>End Date</small></label>
                <input type="text" class="form-control form-control-sm " name="end_dt" value="{{$item->end_dt->format('m-d-Y H:i')}}" disabled>

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