Home > Blockchain >  Laravel 8 - reusable code block, change variable depending on a page for charts?
Laravel 8 - reusable code block, change variable depending on a page for charts?

Time:03-09

I want to display multiple charts in a single page or different pages. How can I reuse the blade file instead of repeating/retyping the code?

I created a plain blade file _chart-widget.blade.php and I want the variable value to change depending on the page, or depending on what I want to set the variable in each <section> of a page

<!--begin::Charts Widget 1-->
<div >
    <!--begin::Header-->
    <div >
        <!--begin::Title-->
        <h3 >
            <span >Recent Statistics</span>

            <span >More than 400 new members</span>
        </h3>
        <!--end::Title-->

        <!--begin::Toolbar-->
        <div >
            <!--begin::Menu-->
            <button type="button"  data-kt-menu-trigger="click" data-kt-menu-placement="bottom-end">
                {!! theme()->getSvgIcon("icons/duotune/general/gen024.svg", "svg-icon-2") !!}
            </button>
            {{ theme()->getView('partials/menus/_menu-1') }}
            <!--end::Menu-->
        </div>
        <!--end::Toolbar-->
    </div>
    <!--end::Header-->

    <!--begin::Body-->
    <div >
        <!--begin::Chart-->
        <div id="kt_charts_widget_1_chart" style="height: 350px"></div>
        <!--end::Chart-->
    </div>
    <!--end::Body-->
</div>
<!--end::Charts Widget 1-->

How can I make the code above dynamic and reusable when I @include it?

CodePudding user response:

You can include views in Laravel blade template.

here you can read more.

Just use like this:

<div>
    @include('_chart-widget')
</div>

If you need to pass data to your widget component, just give parameters as an array to your component:

@include('view.name', ['status' => 'complete'])

CodePudding user response:

If you want variables to be different in each page simply pass vairables from Controller.If you are on the same page and including same blade multiple times this can help you:

@include('view.name', ['code' => 'complete'])

This will set different values for $code variable in different sections.

Check out documentation here.

  • Related